【问题标题】:Using Invoke-Command to Get Application Pools on Remote Servers使用 Invoke-Command 获取远程服务器上的应用程序池
【发布时间】:2016-12-20 15:02:54
【问题描述】:

我正在尝试使用 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

谁能帮我指出正确的方向?

【问题讨论】:

  • 您无法将远程会话中的对象添加到本地计算机上存在的数组中。让Invoke-Command 块返回绑定,然后在外部 foreach 循环中获取结果

标签: arrays powershell iis powershell-5.0


【解决方案1】:

您的对象$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
} 

【讨论】:

  • 我仍然收到“您不能在空值表达式上调用方法”。不幸的是错误。不过感谢您的回复。
  • 我编辑了我的答案,但我现在无法测试,但想法就在那里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-30
相关资源
最近更新 更多