目次
はじめに
先月につづき、PowerShell(ver5系と7系)で学んだことのメモを書き留めます。 PowerShell とは - PowerShell | Microsoft Learn
スクリプトのコマンド引数について
- RubyのOptionParser みたいな仕組みはなさげ
- 起動するスクリプト(.ps1)に、オプションやキーワード引数の指定はできなさそう
- args[0] で配列を順序正しく指定する(引数は少なくいのがよさげ)
PS > . get_server_params.ps1 XXX △△△
- スクリプト内の function をCLIから直接呼べない。
# スクリプト(.ps1) のなか function Main($password, $ouptputKind) { ... } Main -password $args[0] -outputKind $args[1]
認証情報オブジェクトを取得する
- スクリプトでWindowsクライアントから、Windowsサーバをリモート操作するに使う(かも)。
$PWord = ConvertTo-SecureString -String "P@sSwOrd" -AsPlainText -Force
- Get-Credential
- Invoke-Commandで認証エラー(身に覚えのないkerberos)でたときは認証情報オブジェクトを
-Credential
に渡すと通る
- パスワードは、Read-Host とかからユーザに直接入力してもらうのが基本っぽい。
Windowsサーバをリモート操作する
- WinRM(Windows Remote Management ) を使う
- 5985番ポートを使う。
netstat -ano | findstr LISTEN | findstr 5985
で確認できる。 - WSManでリモート処理を行う。
- ただし2023年時点で、Windows 以外のプラットフォームではサポートされてなさげ😢
- なので、Linuxサーバ(のPowerShell)でリモート操作したければ、pythonとかのライブラリを入れる必要アリ
- diyan/pywinrm の小ネタ。
run_cmd
: Windowsコマンドrun_ps
: PowerShellコマンド
- Windows PowerShell を使用してリモート コンピューターを管理する
- Enter-PSSession < 1対1のリモート処理
- Invoke-Command < 1対多のリモート処理
- 5985番ポートを使う。
以上