【发布时间】: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