powershellメモ

powershellメモ

################################################################################
# mytest.ps1
################################################################################
########################################
# スクリプトの引数を受け取る
########################################
$param1 = $args[0]
$param2 = $args[1]
write $param1
write $param2

########################################
# 文字列の置換
########################################
$data = "abcdefg" -replace "abc","xyz"
write $data

########################################
# 正規表現
########################################
write ("123abc" -match "^123")

########################################
# 実行しているスクリプトのパスから、相対のファイルを読み込む
########################################
. (@(Split-Path $myInvocation.MyCommand.path) + '\func.ps1')
funcA ("相対のファイルの関数呼び出し")

########################################
# ファイルを読み込む(utf8)
########################################
$lines = gc -Encoding UTF8 "mydata.txt"
foreach($line in $lines){
	Write-Host $line
}

########################################
# ファイル/ディレクトリの存在確認
########################################
if(Test-Path "./new"){
    write "[new]dir存在します"
}

########################################
# ファイル一覧の取得
########################################
$files = gci -File -Path "./"
foreach($file in $files){
    $file | ac "./file-list.txt"
}

########################################
# ファイル一覧の取得(拡張子指定)
########################################
$files = gci -File -Path "./" | where {$_.Extension.ToLower() -eq ".ps1"}
write ($files -join ",")

########################################
# ディレクトリ作成
########################################
#ni -Path D:\Documents\script\powershell -name new_dir -ItemType directory
#ni -Path ./ -name old_dir -ItemType directory

########################################
# 配列の作成
########################################
$data_array = @()
$data_array += 100
$data_array += 22
$data_array += 33
$data_array = $data_array | sort
write ($data_array -join "-")

########################################
# ハッシュの作成
########################################
$data_hash = @{}
$data_hash["c"] = 999
$data_hash["a"] = 111
$data_hash["b"] = 333

########################################
# キーによるソート
########################################
foreach($dd in $data_hash.GetEnumerator() | sort Key ){
	Write-Host ($dd.Key + " -> " + $dd.Value)
}

########################################
# 値によるソート
########################################
foreach($dd in $data_hash.GetEnumerator() | sort -Descending Value){
	Write-Host ($dd.Key + " -> " + $dd.Value)
}

########################################
# 関数
########################################
function t_func($x){
    return "call t_func:" + $x
}

$ret = t_func("aaa")
write $ret

########################################
# CSVファイルを手動で処理
########################################
$lines = gc ".\test.csv" -Encoding UTF8
foreach ($line in $lines){
	$datas = @($line.Split(','))
	foreach($data in $datas){
		write $data
	}
}

########################################
# ファイルを作成
########################################
ni "./data1.txt" -ItemType File -Force

########################################
# ファイルの結合
########################################
$files = gci -File -Path "./" | where {$_.Extension.ToLower() -eq ".ps1"}
foreach($file in $files){
    gc $file | ac "matomt.txt"
}