PowerShell メモ
目次
実行ポリシー
# 何らかのスクリプトが実行できなかった場合のコマンド
Set-ExecutionPolicy Unrestricted -Force
# プロセス限定
Set-ExecutionPolicy Bypass -Scope Process -Force
Set-ExecutionPolicy:実行ポリシーを変更Restricted:デフォルト。構成ファイルの読み込み・スクリプトの実行を行わないUnrestricted:すべての構成ファイルの読み込み・スクリプトの実行が可能。署名なしのスクリプトは確認を行う。Bypass:何もブロックせず、警告・メッセージを非表示
-Scope Process:プロセス限定で実行ポリシーを変更(管理者権限は不要)
ポートフォワーディング
# 設定
# LP=待受ポート, CP=転送先ポート, IP=転送先アドレス
netsh interface portproxy add v4tov4 listenport=LP listenaddress=0.0.0.0 connectport=CP connectaddress=IP
# 一覧
netsh interface portproxy show all
# 削除
# LP, listenaddress は設定時と同じ値を指定
netsh interface portproxy delete v4tov4 listenport=LP listenaddress=0.0.0.0
ダウンロード
.Net の WebClient を使う方法
(New-Object System.Net.WebClient).DownloadFile('URL','/path/to/save/dir')
(new-object net.webclient).DownloadString('URL')
- New-Object:.NET Frameworkのインスタンスを作成
System.Net.Webclient:データ送受信のためのクラスDownloadFile:データをファイルとしてダウンロードするメソッドDownloadString:データを String としてダウンロードするメソッド
その他
- Invoke-WebRequest/iwr (alias として wget)
- curl (Windows 10 1803 以降)
コマンド実行
-
Invoke-Expression/iex:引数の文字列をコマンドとして実行
iex ((new-object net.webclient).DownloadString('URL')) -
Invoke-Command/icm:ローカルおよびリモートコンピュータでコマンドを実行