从PowerShell

时间:2017-07-06 13:05:23

标签: powershell

如何排序"在线"列和正常运行时间列?我想在顶部显示True,最长的正常运行时间在顶部,然后是FALSE。

    <#

    .Synopsis 
        Query Uptime Details of servers.

    .Description
        This script helps you to get the uptime details of the servers. It also generates a HTML report
        when -HTMLReport switch is used. The report contains the uptime details and a summary of how many
        computers reachable and how many are not

    .Parameter ComputerName    
        Computer name(s) for which you want to get the uptime details.

    .Parameter HTMLReport
        Generates a HTML report in c:\ drive with name uptimereport.html by default. You can override this by
        specifying -HTMLFile parameter

    .Parameter HTMLFile
        Name of the file path where you want to store the report

    .Example
        Get-UptimeOfServers.ps1 -ComputerName Comp1, Comp2

        Gets the Uptime of Comp1 and Comp2
    .Example
        Get-UptimeOfServers.ps1 -ComputerName Comp1, Comp2 -HTMLReport

        Get the uptime of Comp1 and Comp2 and saves the report in HTML format

    .Example
        Get-Content c:\servers.txt | Get-UptimeOfServers.ps1 -HTMLReport

        Get the uptime of computers listed in servers.txt and saves the report in HTML format   

    .Notes
        NAME:      Get-UptimeOfServers.ps1
        AUTHOR:    Sitaram Pamarthi
        WEBSITE:   http://techibee.com

#>

[cmdletbinding()]

param(
    [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [string[]]$ComputerName = $env:computername,
    [switch]$HTMLReport,
    [string]$HTMLFile = "c:\Temp\Uptimereport.html"
)

begin{
if($HTMLReport) {
    $Report = "

            <html>
                <head>
                    <title> Server Uptime Report </title>
                </head>
                <body> 

                    <H1 Align=`"Center`"> <B>Server Uptime Report </B></H1>
                    <br>
                    <H3 Align=`"Center`"> Report Generated at $(Get-Date)</H3>
                    <table BORDER=`"1`" CELLPADDING=`"5`" Align=`"Center`">
                    <tr> 
                        <td font-family: 'Calibri' BGColor=#98C6F3 Align=center><b>Server Name</b></td>
                        <td font-family: 'Calibri' BGColor=#98C6F3 Align=center><b>Online</b></td>
                        <td font-family: 'Calibri' BGColor=#98C6F3 Align=center><b>Uptime</b></td>
                    </tr>"

    }
}   
process {
    $Count=0
    $SuccessComps = 0
    $UnreachableComps = 0
    $FailedComps = 0
    $FinalOutput = @()
    foreach($Computer in $ComputerName) {
        $Count++
        $Computer = $Computer.Toupper()
        $OutputObj  = New-Object -TypeName PSobject
        $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer 
        $Status = 0
        if(Test-Connection -Computer $Computer -count 1 -ea 0) {
            $OutputObj | Add-Member -MemberType NoteProperty -Name IsOnline -Value "TRUE"  
            try {
                $Boottime = (Get-WmiObject win32_operatingSystem -computer $Computer -ErrorAction stop).lastbootuptime
                $Boottime = [System.Management.ManagementDateTimeconverter]::ToDateTime($BootTIme)
                $Now = Get-Date
                $span = New-TimeSpan $BootTime $Now 
                $Uptime = "{0} day(s), {1} hour(s), {2} min(s), {3} second(s)" -f $span.days, $span.hours, $span.minutes, $span.seconds
                $OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value $Uptime   
                $Status=1
                $SuccessComps++
            } catch {
                $OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value "FAILED TO GET" 
                $FailedComps++
            }

        } else {
            $OutputObj | Add-Member -MemberType NoteProperty -Name IsOnline -Value "FALSE" 
            $OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value "" 

            $UnreachableComps++
        }

        $FinalOutput +=$OutputObj
        $OutputObj = $OutputObj 


        if($HTMLReport) {
            if($Status) {
                $BGColor="GreenYellow"
            } else {
                $BGColor="OrangeRed"
            }
            $IsOnline 
            $Report += " <TR>
                        <TD BGColor=$BGColor>$($OutputObj.ComputerName)</TD>
                        <TD BGColor=$BGColor>$($OutputObj.IsOnline)</TD>
                        <TD BGColor=$BGColor>$($OutputObj.Uptime)</TD> 
                        </TR>"

        } else {
            $OutputObj

        }

    }


}
end{
    if($HTMLReport) {
        $Report +=  
        $Report | Out-File $HTMLFile -Force
    }
}

1 个答案:

答案 0 :(得分:-1)

我认为Sort-Object会帮助你。 $ FinalOutput | Sort-Object IsOnline,uptime -decending

版本2你可以将span字段放入

$FinalOutput | sort-Object IsOnline,span -descending | Select-Object ComputerName,IsOnline,Uptime