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'を使うと、まったく同じ効果が得られます。 –