hack のためのネタ帳, etc,,,

公式ページ等

以下のようにすれば sha256sum 相当の digest 計算が出来る
powershell -Command "&{$hasher=[Security.Cryptography.SHA256]::Create();foreach($fn in $Args){$hash=$hasher.ComputeHash([IO.File]::ReadAllBytes($fn));$hex=\"\";foreach($c in $hash){$hex+=($c).ToString(\"x2\")}echo \"$hex $fn\"}}" FILES ...
なお「FILES ...」 の部分は任意の数のファイル名に置き換える事
あ、でも WILD CARD が使えない罠。orz
上記スクリプトの当該部分を変えれば、SHA256 以外(MD5, SHA1, SHA384, SHA512 等)も行けると思う。

参考:
2020-07-29:
関数化してみた
function SHA256($file) {
    $hash = [Security.Cryptography.SHA256]::Create().ComputeHash([IO.File]::ReadAllBytes($file))
    $hex=""
    foreach($c in $hash){
        $hex+=($c).ToString("x2")
    }
    echo "`n`n$hex`t$file`n`n"
}
Foreach statement じゃなくて ForEach-Object Cmdletjoin operator 使うと以下のように。
function SHA256($file) {
    $hash = [Security.Cryptography.SHA256]::Create().ComputeHash([IO.File]::ReadAllBytes($file))
    $hex = ($hash |% { $_.ToString("x2") }) -join ""
    echo "`n`n$hex`t$file`n`n"
}

因みに、help foreach すると Alias foreach -> ForEach-Object のせいで ForEach-Object Cmdlet に誘導される。
Foreach statement を引くには help about_foreach
join operator も help about_join なのだが
なぜか about_* は -online が引けない。

2022-03-11: 追記
digest は標準添付コマンドの certutil.exe で
certutil.exe [options...] -hashfile <InFile> [<HashAlgorithm>]
としたほうが手早いかも?
ただし hash 部分のみ取り出すのに | select -skip 1 -first 1 みたいな事が必要で地味に面倒なのがあまり嬉しくない。
/2022-03-11: 追記
以下のような書式で出来る模様
echo "$foo bar";
echo "$($foo)bar";
echo "$($Args[0])";
echo "$($Args.length)";
配列とかオブジェクトの場合、接尾子の判定が微妙なので、とりあえず $() の中に入れておく方が無難っぽい。
参考:
幾つかやり方がある模様。
  • System.Net.WebClient クラスを使う
  • BITS (Background Intelligent Transfer Service) を使う
  • Invoke-WebRequest を使う (PowerShell3.0 以上?)

以下のような感じで出来る。
powershell -Command "$wc=New-Object System.Net.WebClient; $wc.DownloadFile('http://some.where/file_name', 'file_name')"
powershell -Command "(New-Object System.Net.WebClient).DownloadFile('http://some.where/file_name', 'file_name')"
powershell -Command "Import-Module BitsTransfer; Start-BitsTransfer http://some.where/file_name file_name"
powershell -Command "Import-Module BitsTransfer; Start-BitsTransfer http://some.where/file_name"
BITS の方が、保存ファイル名を省略出来るし表記も短いことに加え進捗表示があるのが嬉しいかも?

参考:

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Wiki内検索

フリーエリア

管理人/副管理人のみ編集できます