【问题标题】:Powershell's Invoke-Command won't take in a variable for -ComputerName parameter?Powershell 的 Invoke-Command 不会接受 -ComputerName 参数的变量?
【发布时间】:2019-04-19 22:02:54
【问题描述】:

我把头发拉出来了,因为我似乎无法让它工作,而且我不知道如何用谷歌搜索这个问题。我正在运行 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 将接受的内容?提前致谢!

【问题讨论】:

    标签: powershell powershell-2.0 powershell-remoting


    【解决方案1】:

    Jamey 和 user983965 是正确的,因为您的声明是错误的。但是 foreach 这里不是强制性的。如果你只是像这样修复你的数组声明,它将起作用:

    $computer_names = "server1","server2"
    Invoke-Command -ComputerName $computer_names -ScriptBlock { 
        Get-WmiObject -Class Win32_LogicalDisk | 
        sort deviceid | 
        Format-Table -AutoSize deviceid, freespace 
    }
    

    【讨论】:

    • 如果您已经使用计算机对象填充了名为$computers 的变量(例如,使用get-adcomputer$computer_names = $computers.name,则另一种填充数组的方法以这种方式工作。
    【解决方案2】:

    您错误地声明了您的数组。在字符串之间放置一个逗号并将其通过管道传递给 for-each,例如:

    $computer_names = "server1", "server2";
    
    $computer_names | %{
       Write-Output "Invoke-Command -ComputerName $_ -ScriptBlock {
    
        ...snip
    

    【讨论】:

    • 成功了,谢谢!我不认为只运行一个 for-each 就可以了。这是我没见过的速记。但是,我避免声明一个数组,因为我不认为 Invoke-Command -ComputerName 需要一个 - 相反,它需要一个用逗号分隔的计算机名称列表,没有空格。我错了吗?
    • 回应自己:看起来是这样。我刚刚阅读了该命令的帮助,它说它需要一个字符串 []。我只是从示例中脱离出来并做出了错误的假设。
    【解决方案3】:

    如果您也从活动目录中获取一组计算机 - 像这样:

    $computers = Get-ADComputer -filter {whatever}

    确保您记得选择/展开结果.. 像这样:

    $Computers= 获取-ADComputer -filter * | Select-Object -ExpandProperty 名称

    那么……

    Invoke-Command -ComputerName $Computers -ScriptBlock {Do Stuff}

    【讨论】:

      【解决方案4】:

      你试过了吗:

      $computer_names = "server1" , "server2"
      
      foreach ($computer in $computer_names)
      {
      Write-Output "Invoke-Command -ComputerName $computer -ScriptBlock { 
          Get-WmiObject -Class Win32_LogicalDisk | 
          sort deviceid | 
          Format-Table -AutoSize deviceid, freespace 
      }"
      Invoke-Command -ComputerName $computer -ScriptBlock { 
          Get-WmiObject -Class Win32_LogicalDisk | 
          sort deviceid | 
          Format-Table -AutoSize deviceid, freespace 
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-21
        • 2014-10-13
        相关资源
        最近更新 更多