我会将foreach($computer in $computers)
中的所有内容都包含在Start-Job
中,以使它们同时运行。唯一的问题是,我需要等待所有的工作完成,然后才能完成底部的ConvertTo-Json
。等到所有线程完成后再运行下一任务
$sb = "OU=some,OU=ou,DC=some,DC=domain"
$computers = Get-ADComputer -Filter {(Enabled -eq $true)} -SearchBase "$sb" -Properties *
$hasmanufacturer = New-Object System.Collections.Generic.List[System.Object]
foreach($computer in $computers)
{
$drives = try{@(Get-WMIObject -Class Win32_CDROMDrive -Property * -ComputerName $computer.Name -ErrorAction Stop)} catch {$null}
foreach($drive in $drives)
{
if($drive.Manufacturer)
{
$hasmanufacturer.Add($computer)
continue
}
} # inner foreach
}
ConvertTo-Json $hasmanufacturer
不相关,但是......为什么在出现错误时返回'$ null'时为了使用'-ErrorAction Stop'?删除'try..catch'和'@()'并使用'-ErrorAction SilentlyContinue'应该具有完全相同的效果。 –