get-wmiobject并行处理以在所有域计算机上查找设备

时间:2014-05-19 16:08:35

标签: powershell scripting

我有一个域名,某些笔记本电脑中安装了M2 SATA驱动器。我们想找到并删除M2。我有一个脚本,它将使用get-content获取* .csv中的IP地址列表,并输出我们正在寻找的驱动器的计算机名称。

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
}}

这可行,但在必须等待非响应主机上的超时时需要永久​​。我认为并行运行许多后台进程可能是有益的,但我不知道如何做到这一点。扫描超过1000个地址时是否有任何想法加快速度?

如果我能想出如何做到这一点作为一个foreach循环我会尝试。也许更像是:

$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
}}

感谢那些可能会帮助我的人。

1 个答案:

答案 0 :(得分:0)

您可以将它们作为作业运行,然后在此之后进入Do / While循环以获得已完成的作业(例如,每半分钟),只要有剩余的作业。类似的东西:

$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)