我在这里拉头发,因为我似乎无法使它正常工作,而且我不知道如何用Google搜索这个问题。我正在运行Powershell 2.0。这是我的脚本:
$computer_names = "server1,server2"
Write-Output "Invoke-Command -ComputerName $computer_names -ScriptBlock {
Get-WmiObject -Class Win32_LogicalDisk |
sort deviceid |
Format-Table -AutoSize deviceid, freespace
}"
Invoke-Command -ComputerName $computer_names -ScriptBlock {
Get-WmiObject -Class Win32_LogicalDisk |
sort deviceid |
Format-Table -AutoSize deviceid, freespace
}
最后一条命令给出错误:
Invoke-Command : One or more computer names is not valid. If you are trying to
pass a Uri, use the -ConnectionUri parameter or pass Uri objects instead of
strings.
但是,当我将Write-Output命令的输出复制到shell并运行该命令时,它就可以正常工作。如何将字符串变量转换为Invoke-Command将接受的内容?提前致谢!
最佳答案
您对数组的声明不正确。在字符串之间放置一个逗号,并将其通过管道传递给每个字符串,例如:
$computer_names = "server1", "server2";
$computer_names | %{
Write-Output "Invoke-Command -ComputerName $_ -ScriptBlock {
...snip
关于powershell - Powershell的Invoke-Command不会接受-ComputerName参数的变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10096336/