- Microsoft / PowerShell (ja) / Scripting (ja)
- TECH+ / 【連載】PowerShell Core入門 - 基本コマンドの使い方
- CodeZine / 「Windows PowerShell 入門」連載一覧
- @IT / Windows Server Insider / Tech TIPS: WindowsでPowerShellスクリプトの実行セキュリティポリシーを変更する
- stofublog / 2014-01-01: Powershell起動高速化について(64bit対応版)
- rcmdnk's blog
- 2016-03-15: PowerShellをちょっと使ってみての感想
- 2016-03-17: PowerShellでbashrc的な設定ファイル
- 裏紙ノート / 20190829 Windows PowerShell ポケットリファレンス
- WIN.JUST4FUN.BIZ / PowerShell
- PowerShell 逆引きリファレンス
- Qiita
- jca02266 / 2021-02-01: powershell チートシート
- kurukurupapa@github / 2021-05-05: 普段使いのPowerShellメモ
- 日経XTECH / ソフト開発 / 連載 Windows PowerShellを使う
以下のようにすれば sha256sum 相当の digest 計算が出来る
あ、でも WILD CARD が使えない罠。orz
上記スクリプトの当該部分を変えれば、SHA256 以外(MD5, SHA1, SHA384, SHA512 等)も行けると思う。
参考:
2020-07-29:
関数化してみた
因みに、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 で
ただし hash 部分のみ取り出すのに | select -skip 1 -first 1 みたいな事が必要で地味に面倒なのがあまり嬉しくない。
/2022-03-11: 追記
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 等)も行けると思う。
参考:
- Microsoft
- TechNet Library / Scripting with Windows PowerShell / Using Windows PowerShell / PowerShell.exe Command-Line Help
- MSDN ライブラリ / .NET 開発 / .NET Framework 4.5 / .NET Framework クラス ライブラリ / System.Security 名前空間 / System.Security.Cryptography / SHA256 クラス
- yanor.net / wiki / PowerShell / UNIX系コマンド群との対応 / printf
- NKY-TECH / 2012-02-07: PowerShellでハッシュ計算
- うなの日記 / 2009-02-03: [Power Shell] 文字列内変数展開の罠
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 Cmdlet と join 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)";配列とかオブジェクトの場合、接尾子の判定が微妙なので、とりあえず $() の中に入れておく方が無難っぽい。
参考:
- うなの日記 / 2009-02-03: [Power Shell] 文字列内変数展開の罠
幾つかやり方がある模様。
以下のような感じで出来る。
参考:
- 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 の方が、保存ファイル名を省略出来るし表記も短いことに加え進捗表示があるのが嬉しいかも?
参考:
- @IT
- Windows TIPS / PowerShellを使って指定したファイルをインターネットからダウンロードする
- .NET TIPS / WebClientクラスでWebページを取得するには?
- MSDN / .NET Framework クラスライブラリ / System.Net.WebClient
- technet
- SE の雑記 / PowerShell で BITS を使用して CUI でファイルをダウンロード
- Cmdlet Reference / Background Intelligent Transfer Service (BITS) Cmdlets
- あたもこ / BITS ファイル転送 〜 ダウンロード 〜
- superuser / Native alternative to wget in Windows PowerShell?
- Windowsでwgetする色々な方法
- IEを使わずにWebブラウザをダウンロードする・PowerShell編
タグ
コメントをかく