2016-05-20 94 views
-1

我有一個腳本功能的方式,我想要它,但它很慢。我嘗試使用foreach並行工作流中的相同方法,但set-variable命令不是可以在工作流中使用的東西。我想看看我這樣做的方式是不正確的,如果有更好的方式來獲得我正在做的事情。我想要做並行請求的原因是因爲當擴展到20多臺服務器時,腳本可能需要相當長的時間才能完成,因爲每臺服務器反過來又能夠一次完成所有操作,而且速度更快。Powershell並行設置變量

下面是腳本(即不平行的foreach作品)的簡單化版本,但它是有效的什麼,我需要得到工作:

$servers = @("server1", "server2"); 
foreach ($s in $servers) { 
    $counter_value = get-counter "\\$s\counter_name" 
    Set-Variable -name "{s}counter" -value $counter_value 
    write-host ${server1counter} 
+0

上面的示例不起作用.. –

回答

0

命令不工作流支持,需要在將InlineScript執行。嘗試(未經測試):

workflow t { 
    $servers = @("server1", "server2"); 
    foreach -parallel ($s in $servers) { 
     inlinescript { 
      $counter_value = get-counter "\\$using:s\counter_name" 
      Set-Variable -name "$($using:s)counter" -value $counter_value 

      #write-host with a PerformanceCounterSampleSet isn't a good combination. You'll only get the typename since it's a complex type (multiple properties etc.) 
      write-host (Get-Variable "$($using:s)counter" -ValueOnly) 
     } 
    } 
} 

t