PowerShell

Contents

文法


Hello World


echo "Hello World"
OR
Write-Host "Hello World"

プロンプト


コメント


# comment
OR
<# comment1
commnet2
comment3 #>

変数宣言


宣言は不要で、いきなり代入できる
$var = 123

行分割


数値演算


除算


整数除算


整数剰余


累乗


インクリメント


$i++

比較演算


同値


数値も文字列も同じ
暗黙の型変換が行われるので、数値型と文字列型を直接比較可能。
$a -eq $b

異値


$a -ne $b

大なり


$a -gt $b

以上


$a -ge $b

小なり


$a -lt $b

以上


$a -le $b

ワイルドカード可


$a -like $b

完全一致が必要なので、比較対象の前後に*を付ける
"1234567890" -like "567"
# False

"1234567890" -like "*567*"
# True


正規表現


部分一致でマッチする
$a -match $b


条件


if


ブレース({}のこと)は必須
if($True)
{
	echo 123
}

if not


if(! $True)
{
	echo 123
}
OR
if(-not $True)
{
	echo 123
}

if else


if($var -eq 1)
{
	echo 1
}
elseif($var -eq 2)
{
	echo 2
}
else
{
	echo 3
}

AND / OR


if(($str1 -eq "abc") -And ($str2 -eq "def") )
{
	echo 123
}

if(($str1 -eq "abc") -Or($str2 -eq "def") )
{
	echo 123
}

ループ


for


for($i=0; $i -lt 5; $i++)
{
	echo $i
}

ループ脱出


break

continue


continue

for each


$array=@(100,120,300)
foreach($next in $array)
{
	Write-Output $next
}


while


文字列


連結


"abc" + "def"
※標準出力に出力時は+は不要
Write-Host "abc" $cde
or
echo "abc" $cde

文字列長


"abc".lenght

エスケープ


バッククォートを使用する。
"aaa`""

配列


宣言





代入


$array[0] = 1

可変長配列



連想配列


宣言


$map = @{one = 1; two = 2; three = 3}

参照


$map["two"]

追加


$map.Add("four", 4)
OR
$map.five = 5

削除


$map.Remove("three")

サブルーチン


呼び出し



返り値無



途中終了



返り値有



その他


コマンドライン引数


Param( [string]$path1, [string]$path2 )
if(($path1 -eq "") -Or ($path2 -eq "") )
{
	echo "No argument"
	exit 1
}


Tips


SQL Server


# サーバパラメータ
$server   = '192.168.XXX.XXX';
$database = 'TestDB';
$userId   = 'SA';
$password = 'password';

$connectionString = @"
Data Source=$server;
Initial Catalog=$database;
User ID=$userId;
Password=$password;
"@ 

# SQL
$sql = 'select ****;';

$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection;
$connection.ConnectionString = $connectionString;
$command = $connection.CreateCommand();
$command.CommandText = $sql;

$adapter = New-Object -TypeName System.Data.SqlClient.SqlDataAdapter $command;
$dataset = New-Object -TypeName System.Data.DataSet;

[void]$adapter.Fill($dataset);

$table = $dataset.Tables[0];

# この時点で全てのデータを取得し終えているのでクローズする
$connection.Close();
$connection.Dispose();
$command.Dispose();
$dataset.Dispose();
$adapter.Dispose();

分割


$splited = $string -Split " "

if($splited [1] -eq "MB")
{
	Write-Host(([double]$splited [0])/1024);
}
elseif($splited [1] -eq "KB")
{

}



ファイル確認


Test-Path .\Users
!(Test-Path ***)

追記


Add-Content .\aaam.txt $aaa

ファイル比較


Param( [string]$path1, [string]$path2 )

$logpath = "comp.log"

if(($path1 -eq "") -Or ($path2 -eq "") )
{
	echo "No argument : path1 path2"
	exit 1
}

$list1 = @(Get-ChildItem $path1 -Recurse)
$list2 = @(Get-ChildItem $path2 -Recurse)

for($i=0; ($i -lt $list1.length) -And ($i -lt $list2.length); $i++)
{
	if($list1[$i].PSIsContainer)
	{
		if($list2[$i].PSIsContainer)
		{
			if($list1[$i].Name -ne $list2[$i].Name)
			{
				echo "Dir Not Match : " $list1[$i].FullName " != " $list2[$i].FullName "`r`n" >> $logpath
				Write-Host "Dir Not Match : " $list1[$i].FullName " != " $list2[$i].FullName
				Write-Host ""
			}
		}
		else
		{
			echo "Dir: " $list1[$i].FullName "; File: " $list2[$i].FullName "`r`n" >> $logpath
			Write-Host "Dir: " $list1[$i].FullName "; File: " $list2[$i].FullName
			Write-Host ""
		}
	}
	else
	{
		if($list1[$i].Name -ne $list2[$i].Name)
		{
			echo "File Not Match : " $list1[$i].FullName " != " $list2[$i].FullName "`r`n" >> $logpath
			Write-Host "File Not Match : " $list1[$i].FullName " != " $list2[$i].FullName
			Write-Host ""
		}
		else
		{
			$hashval1 = Get-FileHash $list1[$i].FullName
			$hashval2 = Get-FileHash $list2[$i].FullName
			if($hashval1.Hash -ne $hashval2.Hash)
			{
				echo "Hash Not Match : " $list1[$i].FullName " != " $list2[$i].FullName "`r`n" >> $logpath
				Write-Host "Hash Not Match : " $list1[$i].FullName " != " $list2[$i].FullName
				Write-Host ""
			}
		}
	}
}

Notice: Trying to get property 'queue' of non-object in /usr/local/wordpress/wp-includes/script-loader.php on line 2876

Warning: Invalid argument supplied for foreach() in /usr/local/wordpress/wp-includes/script-loader.php on line 2876