来自powershell脚本的实时表格输出

时间:2013-05-07 20:36:30

标签: powershell

以下powershell脚本在重新启动后监视服务器列表,并在可以使用Get-Service附加后立即通知。

#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 个答案:

答案 0 :(得分:1)

如果您在循环中的每个对象上调用Format-Table,它将单独显示它们。输出orginial对象和PowerShell将是神奇的。顺便说一句,你为什么要拿一个对象)作业输出,并创建一个包含它的新对象? :S

尝试替换

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

$joboutput

甚至更简单,放弃$joboutput。取代

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

$jobsdone | Receive-Job