invoke-commandのcomputernameにpowershellの配列を渡してみた!ただ、Get-Hostの挙動がちょっと変?
invoke-commandはcomputernameを配列で指定できる。
以下のリンクの「例 7: 複数のコンピューター上のホスト プログラムのバージョンを取得する」を試してみた。
$version = Invoke-Command -ComputerName (Get-Content Machines.txt) -ScriptBlock {(Get-Host).Version}
invoke-commandの-computernameは配列を受け取ってくれるのか試してみたら、本当に受け取ってくれた。
(credencialは一致している前提で)
ただ、Get-Hostの結果がlocalhostで実行した場合と異なっていた。
invoke-commandだとversionが全て1で同じになってしまう。
なぜだろう?
’[system.environment]::osversion’で試してみたら異なる結果が得られた。
PS >(get-host).version Major Minor Build Revision ----- ----- ----- -------- 5 1 14409 1027 PS >$h = ( get-content .\hosts.txt ) PS >$h PCXX PCYY localhost PS >invoke-command -computername $h -scriptblock { (get-host).version } Major Minor Build Revision PSComputerName ----- ----- ----- -------- -------------- 1 0 0 0 localhost 1 0 0 0 PCXX 1 0 0 0 PCYY PS >invoke-command -computername $h -scriptblock { [system.environment]::osversion } PSComputerName : PCXX RunspaceId : ********** Platform : Win32NT ServicePack : Version : 10.0.22621.0 VersionString : Microsoft Windows NT 10.0.22621.0 PSComputerName : localhost RunspaceId : *********** Platform : Win32NT ServicePack : Version : 6.3.9600.0 VersionString : Microsoft Windows NT 6.3.9600.0 PSComputerName : PCYY RunspaceId : ********** Platform : Win32NT ServicePack : Version : 10.0.19044.0 VersionString : Microsoft Windows NT 10.0.19044.0
PSRemotingでget-hostを実行しても同様だったので、実行するcmdletによっては注意が必要ということだろうか。
それよりも、powershell自体が古いバージョンだということが問題なのかもしれないが、もうすぐ退役なので・・・。
Name Value ---- ----- PSVersion 5.1.14409.1027
了