无法从PScustomobject转换为Arraylist

时间:2019-09-04 13:42:45

标签: powershell invoke-command

我正在尝试从多个服务器获取所有修补程序列表。我正在使用带有-asjob参数的invoke命令从所有服务器获取补丁程序列表。运行以下代码。 我收到以下错误。我尝试过

Get-CIMInstance -Class Win32_QuickFixEngineering

Get-WmiObject -Class Win32_QuickFixEngineering

但仍然出现相同的错误。

$Servers = Get-Content "C:\Users\Suman.Ghosh\Servers.txt"
[System.Collections.ArrayList]$All_Jobs = @()
[System.Collections.ArrayList]$Updated_Servers_List = @()
[System.Collections.ArrayList]$Jobs_Output = @()

$Patches_to_lookfor = @(
    'KB4462926',
    'KB4462941'
)

foreach ($S in $Servers) {
    if (Test-Connection -ComputerName $S -Count 1 -Quiet) {
        $Updated_Servers_List += $S
        $All_Jobs += Invoke-Command -ComputerName $S -ScriptBlock {Get-HotFix} -AsJob
    } else {
        Write-Warning "Computer $S is not running"
    }
}

Write-Host "below Jobs are running" -ForegroundColor Cyan
$All_Jobs

Write-Host "waiting for jobs to finish" -ForegroundColor Cyan
$All_Jobs | Wait-Job

$temp = @()
$flag = $false
foreach ($job in $All_Jobs) {
    $Jobs_Output += Get-Job $job.Id | Receive-Job | Select HotFixID, CSNAME
}

foreach ($Job_output in $Jobs_Output) {
    Write-Host $Job_output -ForegroundColor Green
    ### DO some stuff
}
Cannot convert value "@{HotFixID=KB4020449; CSName=mit1epxa2}" to type
"System.Collections.ArrayList".
Error: "Cannot convert the "@{HotFixID=KB4020449; CSName=mit1epxa2}" value
of type "Selected.System.Management.Automation.PSCustomObject" to type
"System.Collections.ArrayList"."
At line:1 char:10
+ foreach ($Job_output in $Jobs_Output) {
+          ~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

1 个答案:

答案 0 :(得分:0)

好的,让我们看看我的第二个答案是否被不知道主题的人删除(没有通知)。我相信您已经在$ job_output上运行了这样的命令(没有“ s”):

[collections.arraylist]$job_output = @()

然后在底部的循环中,将创建一个异常,因为无法使用$ job_output变量将pscustomobject强制转换为collections.arraylist。 $ jobs_output(带有“ s”)是由select创建的[pscustomobjects]的数组列表。

foreach ($Job_output in $Jobs_Output) {
    Write-Host $Job_output -ForegroundColor Green
    ### DO some stuff
}