2016-02-12 74 views
1

当我运行下面的代码时,它输出我想要的信息,但即时通讯在最终输出时遇到了问题。它显示我的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 
+0

所以前两行的文件输出,然后运行'GET-Uptime'和它管'的ConvertTo HTML的-As表-Properties正常运行时间,计算机,LastBootupTime'指定的属性,以及订购你想要他们。 – TheMadTechnician

回答

0

接着代码段给出所需的输出:示出了作为UpTime最后days.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