2013-03-27 88 views
2

我想在服務器列表中獲取特定的KBXXXXXX存在,但是一旦我的腳本需要一個服務器,它需要時間並返回結果並返回,然後轉到下一個一個。這個腳本對我來說工作得很好。 我希望我的腳本能夠啓動並獲取修補程序作爲工作和其他過程來收集結果並顯示它們。start-job並行運行命令並輸出結果

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

} 

它使列表執行更快而不是連續運行的目標。

+0

看看類似的問題http://stackoverflow.com/questions/15580105/powershell-run-multiple-jobs-in-parralel-and-view-streaming-results-from-backgr – 2013-03-28 11:42:37

回答

0

爲此使用PowerShell作業。

的cmdlet:

  • 獲取在職
  • 接收在職
  • 刪除-工作
  • 啓動工作
  • 停止,工作
  • 等待工作

這是一個未經測試的例子:

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

使用PowerShell V2,你可以在這個環節使用工作在@Andy答案,或者還進一步詳細Can Powershell Run Commands in Parallel?

使用PowerShell V2也可能想看看這個腳本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 } 
0

不是使用作業相反,用它來查詢該內置到該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的正確版本可在瞬間以測試上述&檢查輸出,但它是一個起點。

+0

@alroc這就是有趣的,你能鏈接到一些關於這種並行處理類型的在線文章嗎?謝謝 – 2013-03-28 01:52:12

+0

@PaulRowland,不幸的是我很難找到關於「如何」工作的良好文檔。查看['Get-Hotfix'](http://technet.microsoft.com/en-us/library/hh849836.aspx)和[前幾個命中](https:// www。 google.com/search?q=powershell+computername+parameter+site:microsoft.com)。當你需要他們時,工作很好,但是他們會引入不必要的複雜性。使用PowerShell爲您提供的內容 - MS可能會比您更好地完成並行性。 – alroc 2013-03-28 02:13:28

+0

@alroc - 謝謝你,但我沒有看到它表明它並行地做它們。知道它是否與使用foreach-parallel選項一樣,也是很有趣的。我現在沒有測試服務器,但我可以運行一個腳本來比較兩者。 – 2013-03-28 03:38:52

相關問題