简体   繁体   中英

get-wmiobject parallel processing to find device on all domain computers

I have a domain where there are M2 SATA drives installed in some of the laptops. We would like to locate and remove the M2s. I have a script that will take a list of IP addresses in a *.csv using get-content and output a computer name with the drive we are looking for.

function FindM2 {
process {
$dd = gwmi win32_diskdrive -comp $_ | Where-Object {$_.model -eq 'SC2 M2 SSD'} -    ErrorAction SilentlyContinue
$cs = gwmi win32_computersystem -comp $_ -ErrorAction SilentlyContinue
$obj = new-object psobject
$obj | add-member noteproperty CompName $cs.name
$obj | add-member noteproperty SSD $dd.model
write-output $obj
}}

This works, but takes forever when it has to wait for a timeout on a non-responsive host. I was thinking it may be beneficial to run many background processes in parallel, but I don't know how to do this. Any thoughts for speeding this up when scanning over 1000 addresses?

If I could figure how to do this as a foreach loop I would try that. Maybe something more like:

$cn = Get-ADComputer -Filter * -SearchBase "OU=Computers,DC=MyDomain,DC=local" | Select Name
foreach ($name in $cn){
process {
$dd = gwmi win32_diskdrive -comp $_ | Where-Object {$_.model -eq 'SC2 M2 SSD'} -ErrorAction SilentlyContinue
$cs = gwmi win32_computersystem -comp $_ -ErrorAction SilentlyContinue
$obj = new-object psobject
$obj | add-member noteproperty CompName $cs.name
$obj | add-member noteproperty SSD $dd.model
write-output $obj
}}

Thank you to those out there who might assist me with this.

You could run them as jobs, and then toss in a Do/While loop after that to get the jobs that are done (say, every half minute) so long as there are jobs left. Something like:

$cn = Get-ADComputer -Filter * -SearchBase "OU=Computers,DC=MyDomain,DC=local" | Select Name
foreach ($name in $cn){
Start-Job -ScriptBlock {
    $dd = gwmi win32_diskdrive -comp $Name.name | Where-Object {$_.model -eq 'SC2 M2 SSD'} -ErrorAction SilentlyContinue
    $cs = gwmi win32_computersystem -comp $Name.name -ErrorAction SilentlyContinue
    $obj = new-object psobject
    $obj | add-member noteproperty CompName $cs.name
    $obj | add-member noteproperty SSD $dd.model
    $obj
}
$Results = @()
Do{$Results += Get-Job -State Completed; $results | FT -Auto; start-sleep 30}while(Get-Job)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM