【发布时间】:2017-07-18 16:08:03
【问题描述】:
我会将 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应该具有完全相同的效果。
标签: multithreading powershell jobs start-job