kkamegawa's weblog

Visual Studio,TFS,ALM,VSTS,DevOps関係のことについていろいろと書いていきます。Google Analyticsで解析を行っています

今日のスクリプト(ハッシュ計算)

function GetHashSHA1([string]$filename)
{
	[Reflection.Assembly]::LoadWithPartialName("System.IO")
	[Reflection.Assembly]::LoadWithPartialName("System.Security.Cryptography")
	$hashAlgolithm = New-Object System.Security.Cryptography.HMACSHA1
	$filestream = New-Object System.IO.FileStream($filename,[System.IO.FileMode]::Open)
	$HashValue = $hashAlgolithm.ComputeHash($filestream)
	$fileStream.Close()
	$HexString = New-Object System.Text.StringBuilder
	$HashValue | ForEach-Object{[void]$HexString.Append($_.ToString("X2"))}
	
	return $HexString.ToString().ToLower()
}

今時っぽくSHA1で。5行目を変更すればSHA256とかMD5とか思いのまま。これで構成管理スクリプトの基礎編ができた。ところで…PowerShell.exeって、cdでPSドライブ変えても、絶対パスで渡さない限り、起動フォルダを補うって言う仕様なのかな?

  • c:\Documents\hogeで起動される
  • cd d:\temp
  • New-Object System.IO.FileStream("hoge.txt") //hoge.txtはd:\tempにある

と実行するとめでたく例外。d:\temp\hoge.txtとフルパスで指定すると、正常。