2014-05-02 68 views
0
Get-WmiObject win32_operatingsystem -computerName $svr | select csname, @{LABEL='LastBootUpTime';EXPRESSION={($now - $($_.ConverttoDateTime($_.lastbootuptime))).days}} 

這項工作完美,但我的問題是有沒有辦法讓那些計算機名稱不在管道中,我的意思是說那些計算機不能訪問或RCP錯誤或訪問被拒絕。?上次啓動時間WMI

我想知道是否有服務器關閉或刪除。

回答

0

您的樣品不表達你的多張輸入機器名。但一個解決方案是somethign這樣,使用try-catch代碼:

$pcs = Get-Content .\list-of-machines.txt 
$now = Get-Date 

$pcs | ForEach-Object {  
    $pc = $_ 

    try { 
     Get-WmiObject win32_operatingsystem -ComputerName $pc -ErrorAction Stop | ForEach-Object { 
      New-Object psobject -Property @{ 
       MachineName = $pc 
       Uptime = ($now - ($_.ConverttoDateTime($_.lastbootuptime))).days 
       Result = "SUCCESS" 
      } 
     } 
    } catch { 
     New-Object psobject -Property @{ 
      MachineName = $pc 
      Result = $_.Exception.Message 
     } 
    } 
} | 
#Export to CSV 
Select MachineName, Uptime, Result | Export-Csv .\Uptime-results.csv -NoTypeInformation 
0

可以簡化一些,如果你創建的代碼在前端的對象:

foreach ($srv in $serverlist) 
{ 
    $result = New-Object psobject -Property @{csname=$srv;LastBootUpTime=$null} 

    Try { 
     $result.LastBootUpTime = 
     Get-WmiObject Win32_OperatingSystem -ComputerName $svr -ErrorAction Stop | 
      foreach {($now - $($_.ConverttoDateTime($_.lastbootuptime))).days} 
     } 

    Catch {$result.LastBootUpTime = $_.Exception.Message} 

$result 

}