使用 PowerShell 在多台服务器上运行病毒扫描 [英] Use PowerShell to run virus scan on multiple servers
问题描述
我正在尝试对我们环境中的服务器列表运行病毒扫描.有数百台机器,因此我们希望一次运行大约 10 台扫描(使用我们已有的命令行提示符).我们对 PowerShell 完全陌生,因此我们将不胜感激.我们对需要使用的命令有一个大致的了解——这是我们认为它现在可能起作用的方式:
I'm trying to run a virus scan on a list of servers in our environment. There are hundreds of machines, so we'd like to run the scan (using a command line prompt that we already have) around 10 at a time. We're totally new to PowerShell so any help would be really appreciated. We have a general idea of what commands we need to use -- here's how we think it might work for now:
$server = Get-Content "serverlist.txt"
$server | % {
$VirusScan = { Scan32.exe }
Invoke-Command -ScriptBlock { $VirusScan } -computerName $server -ThrottleLimit 10 -Authentication domain/admin
}
有人对我们如何协调这件事有任何建议吗?
Does anyone have any suggestions on how we might orchestrate this?
推荐答案
我正在使用类似这样的东西在远程主机上并行运行任务:
I'm using something like this for running tasks in parallel on remote hosts:
$maxSlots = 10
$hosts = "foo", "bar", "baz", ...
$job = {
Invoke-Command -ScriptBlock { Scan32.exe } -Computer $ARGV[0] -ThrottleLimit 10 -Authentication domain/admin
}
$queue = [System.Collections.Queue]::Synchronized((New-Object System.Collections.Queue))
$hosts | ForEach-Object { $queue.Enqueue($_) }
while ( $queue.Count -gt 0 -or @(Get-Job -State Running).Count -gt 0 ) {
$freeSlots = $maxSlots - @(Get-Job -State Running).Count
for ( $i = $freeSlots; $i -gt 0 -and $queue.Count -gt 0; $i-- ) {
Start-Job -ScriptBlock $job -ArgumentList $queue.Dequeue() | Out-Null
}
Get-Job -State Completed | ForEach-Object {
Receive-Job -Id $_.Id
Remove-Job -Id $_.Id
}
Sleep -Milliseconds 100
}
# Remove all remaining jobs.
Get-Job | ForEach-Object {
Receive-Job -Id $_.Id
Remove-Job -Id $_.Id
}
这篇关于使用 PowerShell 在多台服务器上运行病毒扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!