使用Invoke-Command在远程服务器上获取应用程序池

时间:2016-12-20 15:02:54

标签: arrays powershell iis powershell-v5.0

我尝试使用Invoke-Command获取多个远程服务器上的应用程序池列表。到目前为止,我有类似的东西:

$servers = Get-Content -Path "C:\Path\to\servers.txt"

$array = New-Object -TypeName 'System.Collections.ArrayList'

foreach ($server in $servers) {
Invoke-Command -ComputerName $server -ScriptBlock {
Import-Module WebAdministration
$sites = Get-ChildItem IIS:\sites
    foreach ($site in $sites) {
        $array.Add($site.bindings)}}}

然而我收到错误:

 You cannot call a method on a null-valued expression.
    + CategoryInfo          : InvalidOperation: (Add:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
    + PSComputerName        : computername

我尝试使用常规数组而不是ArrayLists,我收到以下错误:

Method invocation failed because [Microsoft.IIs.PowerShell.Framework.ConfigurationElement] doesn't contain a method named 'op_Addition'.
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
    + PSComputerName        : computername

任何人都可以帮我指出正确的方向吗?

1 个答案:

答案 0 :(得分:0)

远程服务器上不知道您的对象$array。因此,一个建议是将您的数组发送到远程服务器,然后添加一些值并将其返回:

$servers = Get-Content -Path "C:\Path\to\servers.txt"    

$ array = New-Object -TypeName' System.Collections.ArrayList'

foreach ($server in $servers) {
    $array = Invoke-Command -ComputerName $server -ScriptBlock {
      param($array)
      Import-Module WebAdministration
      $sites = Get-ChildItem IIS:\sites
      foreach ($site in $sites) {
         [void]($array.Add($site.bindings))
      }
      $array
     } -ArgumentList $array
}