我正在尝试获取服务器列表上的特定KBXXXXXX存在,但一旦我的脚本一个服务器,它需要时间和返回结果,然后返回,然后移动到下一个。这个脚本非常适合我。我希望我的脚本启动并获取热修复作为作业和其他进程,只是为了收集结果并显示它们。
$servers = gc .\list.txt
foreach ($server in $servers)
{
$isPatched = (Get-HotFix -ComputerName $server | where HotFixID -eq 'KBxxxxxxx') -ne $null
If ($isPatched)
{
write-host $server + "Exist">> .\patchlist.txt}
Else
{
Write-host $server +"Missing"
$server >> C:\output.txt
}
}它的目标是让列表执行得更快,而不是串行运行。
发布于 2013-03-28 09:21:59
使用Powershell answer,您可以使用@Andy V2中的作业,也可以在此链接Can Powershell Run Commands in Parallel?中详细介绍
对于PowerShell V2,您可能还希望使用runspaces查看此脚本http://gallery.technet.microsoft.com/scriptcenter/Foreach-Parallel-Parallel-a8f3d22b
对于PowerShell V3,您可以使用foreach -parallel选项。
例如(NB Measure-Command只是为了计时,所以你可以做个比较)
Workflow Test-My-WF {
param([string[]]$servers)
foreach -parallel ($server in $servers) {
$isPatched = (Get-HotFix -ComputerName $server | where {$_.HotFixID -eq 'KB9s82018'}) -ne $null
If ($isPatched)
{
$server | Out-File -FilePath "c:\temp\_patchlist.txt" -Append
}
Else
{
$server | Out-File -FilePath "c:\temp\_output.txt" -Append
}
}
}
Measure-Command -Expression { Test-My-WF $servers }发布于 2013-03-28 08:40:22
为此,请使用PowerShell作业。
cmdlets:
下面是一个未经测试的示例:
$check_hotfix = {
param ($server)
$is_patched = (Get-HotFix -ID 'KBxxxxxxx' -ComputerName $server) -ne $null
if ($is_patched) {
Write-Output ($server + " Exist")
} else {
Write-Output ($server + " Missing")
}
}
foreach ($server in $servers) {
Start-Job -ScriptBlock $check_hotfix -ArgumentList $server | Out-Null
}
Get-Job | Wait-Job | Receive-Job | Set-Content patchlist.txt发布于 2013-03-28 09:33:23
不使用作业,而是使用查询cmdlet中内置的多台计算机的功能。许多微软的cmdlet,尤其是那些用于系统管理的cmdlet,都接受字符串数组作为-Computername参数的输入。传入服务器列表,cmdlet将查询所有服务器。大多数具有此功能的cmdlet将串行查询服务器,但Invoke-Command将并行执行此操作。
我还没有测试它,因为我现在还没有启动Windows,但这应该会让你开始(按顺序)。
$servers = gc .\list.txt
$patchedServers = Get-HotFix -ComputerName $servers | where HotFixID -eq 'KBxxxxxxx'|select machinename
$unpatchedServers = compare-object -referenceobject $patchedServers -differenceobject $servers -PassThru
$unpatchedServers |out-file c:\missing.txt;
$patchedServers|out-file c:\patched.txt;同时:
$servers = gc .\list.txt
$patchedServers = invoke-command -computername $servers -scriptblock {Get-HotFix | where HotFixID -eq 'KBxxxxxxx'}|select -expandproperty pscomputername |sort -unique和以前一样,我目前没有合适的Windows版本来测试上面的内容&检查输出,但这只是一个起点。
https://stackoverflow.com/questions/15671487
复制相似问题