格式化我的日期时间

时间:2016-02-12 17:52:27

标签: powershell

当我运行以下代码时,它会输出我想要的信息,但我最终输出有问题。它将我的UpTime显示为天,小时,分钟,秒。我想要几天和几小时。我想将Uptime移到我列表的最后一个位置。现在的出局是Uptime,Computer,LastBootupTime。

Function Get-UpTime 
{ Param ([string[]]$servers) 
   Foreach ($s in $servers)  
   {  
     $os = Get-WmiObject -class win32_OperatingSystem -cn $s -ErrorAction SilentlyContinue  
     New-Object psobject -Property @{computer=$s; 
     LastBootUpTime = [Management.ManagementDateTimeConverter]::ToDateTime( (Get-WmiObject - Class Win32_OperatingSystem -Computer $s | Select -Exp LastBootUpTime) )
     UpTime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject - Class Win32_OperatingSystem -Computer $s | Select -Exp LastBootUpTime)) 
   }
}}
Get-UpTime -servers $servers| 
ConvertTo-Html -As Table -body " 
<h1>Server Uptime Report</h1> 
The following report was run on $(get-date)" >> $path  
Invoke-Item $path 

1 个答案:

答案 0 :(得分:0)

下一个代码段提供了所需的输出:在列表中最后显示UpTimedays.hours:minutes;此外,显示所有经过测试的服务器(如果RPC服务器不可用,则显示N/A代替LastBootUpTimeUpTime

Function Get-UpTime 
{   Param ([string[]]$servers) 
    Foreach ($s in $servers) {
        Try 
        {   $os = Get-WmiObject -class win32_OperatingSystem -cn $s
            $ST = [System.Management.ManagementDateTimeconverter]::
                       ToDateTime(($os | Select -Exp LastBootUpTime))
            $UT = (Get-Date) – $ST  # Days.Hours:Minutes:Seconds.MillisecondsFraction
                                    # e.g. 114.12:32:42.6025504
            New-Object psobject -Property @{
                Computer = $s
               #UpTime   = $UT.ToString().Substring(0, $UT.ToString().Length - 11)
                UpTime   = '' + $UT.Days + '.' + $UT.Hours + ':' + $UT.Minutes
                LastBootUpTime = $ST
            }
        } 
        Catch 
        {   New-Object psobject -Property @{
                Computer = $s
                UpTime   = "N/A"
                LastBootUpTime = "N/A"
            }
        }    
    }
}

Get-UpTime -servers $servers |
ConvertTo-Html -As Table -Property Computer, LastBootUpTime, UpTime -body " 
<h1>Server Uptime Report</h1> 
The following report was run on $(get-date)" >> $path
Invoke-Item $path
相关问题