2013-05-07 76 views
1

以下PowerShell腳本在重新啓動後監視服務器列表,並在它可以附加到Get-Service後立即通知服務器。PowerShell腳本的實時表格輸出

#start all jobs here 
get-job | stop-job 
get-job | remove-job 

$servers = ("localhost", "127.0.0.1", "badserver") 

function Ping-Service($myservers,[int]$timeout=60, [int]$waitbetweentrys=1){ 
    $myservers| %{ 
    $computername = $_ 
    start-job -scriptblock { 
        param(
       [string]$computername, 
       [int]$maxwait, 
       [int]$waitbetween 
       ) 
       $status = "Fail" 
       $StartTime = Get-Date 
       $duration = 0 
       do 
       { 
       #if successful, break out 
       try {$gs = Get-Service -ComputerName $computername -ErrorAction Stop;$status = "Success";break} catch {start-sleep $waitbetween}        
       $duration = (New-TimeSpan $StartTime $(Get-Date)).Seconds 
       } while ($duration -lt $maxwait) 
       $hash = [ordered]@{"Computername" = $computername; "Type" = "Service";"Status"=$status;"Done"=$(Get-Date);"Duration"=$duration}  
       $hash 
      } -ArgumentList ($computername, $timeout, $waitbetweentrys) 
} 
} 
Ping-Service -myservers $servers -timeout 10 

#loops through jobs and check for completed ones 
do 
    { 
     #process jobs that are done 
     $jobsdone = Get-Job | where State -eq 'Completed' 
     $joboutput = $jobsdone | Receive-Job 

     $joboutput | %{New-Object PSObject -Property $_ } |ft -AutoSize 

     #remove jobs once we get the output 
     $jobsdone | Remove-job 

     #See if there are any jobs left  
     $jobsleft = Get-Job 

     #wait 5 seconds before checking jobs 
     if ($jobsleft -ne $null) {start-sleep 5} 

    } while ($jobsleft -ne $null) #continue loop while there are jobs left 

它運作良好,但輸出不格式化我怎麼會喜歡它:

Duration Status Computername Done    Type 
-------- ------ ------------ ----    ---- 
     0 Success localhost 5/7/2013 4:09:30 PM Service 



Duration Status Computername Done    Type 
-------- ------ ------------ ----    ---- 
     0 Success 127.0.0.1 5/7/2013 4:09:31 PM Service 



Duration Status Computername Done    Type 
-------- ------ ------------ ----    ---- 
     10 Fail badserver 5/7/2013 4:09:42 PM Service 

我想它看起來就像這樣:

Duration Status Computername Done    Type 
-------- ------ ------------ ----    ---- 
     0 Success localhost 5/7/2013 4:09:30 PM Service 
     0 Success 127.0.0.1 5/7/2013 4:09:31 PM Service 
     10 Fail badserver 5/7/2013 4:09:42 PM Service 

但是,關鍵是要實時顯示每個結果。具有諷刺意味的是,腳本頂部的開始輸出正是我想要的。

回答

1

如果您在循環中的每個對象上調用Format-Table,它將單獨顯示它們。輸出orginial對象和PowerShell將做到這一點。順便說一句,你爲什麼要一個對象)作業輸出),並創建一個包含它的新對象? :S

嘗試用

$joboutput 

或者也可以簡單更換

$joboutput | %{New-Object PSObject -Property $_ } |ft -AutoSize 

,下降$joboutput。更換

$joboutput = $jobsdone | Receive-Job 
$joboutput | %{New-Object PSObject -Property $_ } |ft -AutoSize 

$jobsdone | Receive-Job 
+0

我想加在一起所有的輸出爲對象,所以我可以將該值傳遞給另一個腳本。如果我按照你的建議修改輸出,它將輸出一個帶有兩列的列表視圖。 – user2359792 2013-05-08 14:01:33

+0

我沒有看到問題。樣本是否不完整(在其他地方是否使用了$ jobsdone)?如果是這樣,請使用我答案的第一部分來僅修復可見輸出。我刪除的唯一東西是不必要的對象創建和'format-table'(BY THE WAY,銷燬你剛剛創建的對象)。 – 2013-05-08 16:02:28

+0

只是爲了澄清更多。如果將代碼保存爲腳本並運行它,它將返回對象的ARRAY,您可以將其存儲和/或傳遞給另一個cmdlet/function/script。因此,如果默認視圖是列表視圖,請運行'myscript.ps1 |「之類的東西format-table -autosize'。只記得不要在代碼中使用'format -...'cmdlet,因爲它返回一個特殊的formatobject,而不是原始的數據對象本身。 – 2013-05-08 18:02:10