2016-12-20 5 views
0

Invoke-Commandを使用して、複数のリモートサーバー上のアプリケーションプールの一覧を取得しようとしています。これまでのところ私のようなものがあります:私はエラーを取得しかし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 

を私は通常の配列の代わりのArrayListを使用してみました、私は次のエラーを取得する:

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 

誰かが正しい方向に向けるのを助けることができますか?

+2

リモートセッションのオブジェクトをローカルコンピュータ上のアレイに追加することはできません。 'Invoke-Command'ブロックがバインディングを返してから、外側のforeachループで結果を受け取るようにします。 –

答えて

0

オブジェクト$arrayは、リモートサーバーでは認識されていません。だから、1つの命題は、いくつかの値を追加し、それを返し、その後、リモートサーバーに配列を送信することです:

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

$配列=新オブジェクト-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 
} 
+0

" Null値の式でメソッドを呼び出すことはできません。 "エラーは残念です。しかし、返信いただきありがとうございます。 – Keefer

+0

私は自分の答えを編集しましたが、私は今テストすることはできませんが、そのアイデアはそこにあります。 – TWEESTY

関連する問題