2014-10-07 67 views
1

我想把一個非常簡單的代碼輸出服務器名稱,最後一次重新啓動日期,然後在幾小時和幾天的差異。我已經嘗試了Write-Host的幾次迭代,但似乎無法獲得我期望的輸出。輸出應該是這樣的:
ServerName |重新啓動日期|運行時間(日,小時)Powershell:組合輸出中的變量

下面是代碼:

begin {} 
process { 
    foreach($server in (gc d:\win_IP.txt)){ 
    if(Test-Connection -ComputerName $server -Count 1 -ea 0) { 
     $strFQDN = [System.Net.Dns]::GetHostbyAddress($server) | Select-Object HostName -ErrorAction "SilentlyContinue" 
     $wmi = Get-WmiObject -Class Win32_OperatingSystem -Computer $server 
     $strDate = $wmi.ConvertToDateTime($wmi.LastBootUpTime) 
     $strDiff = [DateTime]::Now - $wmi.ConvertToDateTime($wmi.LastBootUpTime) | Format-Table Days, Hours 
    } else { 
     Write-Verbose "$server is offline" 
    }    
    } 
}    

end {} 

如果有人能解釋是如何結合變量的工作原理,以及如何格式化輸出,我會很感激。

在此先感謝。

+1

你得到什麼輸出?如果在'if'語句中使用'Test-Connection',則應該使用'-Quiet'參數。 – 2014-10-07 23:05:46

回答

6

試試這個:

foreach ($server in (Get-Content "d:\win_IP.txt")){ 
    if (Test-Connection -ComputerName $server -Count 1 -Quiet) { 
     $strFQDN = [System.Net.Dns]::GetHostbyAddress($server) 
     $wmi = Get-WmiObject -Class Win32_OperatingSystem -Computer $server 
     $strDate = $wmi.ConvertToDateTime($wmi.LastBootUpTime) 
     $strDiff = [DateTime]::Now - $strDate 

     [PSCustomObject]@{ 
      "ServerName" = $strFQDN.HostName 
      "Reboot Date" = $strDate 
      "Time Running" = "$($strDiff.Days) days, $($strDiff.Hours) hours" 
     } 

    } else { 
     Write-Verbose "$server is offline" 
    }    
} 

我所做的是店內的每個字段的一個對象,那麼就輸出該對象,而無需進行格式化。通常不是格式化對象的好主意,因爲它們被轉換爲字符串,除了輸出到主機/文件外,不能用於任何其他用途。

+0

這完美,Entbark!非常感謝你解釋它是如何工作的;我一定會更好地理解我的下一個腳本任務的對象! – user2521736 2014-10-08 15:37:25

2

如果您使用的是PowerShell 3.0,Entbark的答案可能就是您要做的。 HashTable到Object的表示法在PowerShell 2.0中不起作用。

相反,請參閱下面的更多選項。但主要的一點是,您可以通過創建新對象並將變量作爲屬性添加到該對象來組合變量。

(另外,我不知道試連接的,只是我還是超慢?)

簡介:

ForEach ($server in (gc 'D:\win_IP.txt')) { 
    $Pinger = New-Object System.Net.NetworkInformation.Ping 
    if($Pinger.Send($server, 500).Status -eq "Success") { 
     $strFQDN = [System.Net.Dns]::GetHostbyAddress($server) | 
      Select HostName -ErrorAction "SilentlyContinue" 
     $wmi = Get-WmiObject -Class Win32_OperatingSystem -Computer $server 
     $strDate = $wmi.ConvertToDateTime($wmi.LastBootUpTime) 
     $strDiff = [DateTime]::Now - $strDate 

聯合變量選項1:

 $OutObject = New-Object PSObject 
     $OutObject | Add-Member NoteProperty 'FQDN' ($strFQDN) 
     $OutObject | Add-Member NoteProperty 'Date' ($strDate) 
     $OutObject | Add-Member NoteProperty 'Diff' ($strDiff) 

     # 1 A: 
     Write-Output $OutObject 

     # 1 B: 
     $OutObject | Format-Table 

聯合變量選項2:

 Write-Output ((New-Object PSObject) | 
      Add-Member NoteProperty 'FQDN' ($strFQDN) -PassThru | 
      Add-Member NoteProperty 'Date' ($strDate) -PassThru | 
      Add-Member NoteProperty 'Diff' ($strDiff) -PassThru) 

結合的變量選擇2B:

 ((New-Object PSObject) | 
      Add-Member NoteProperty 'FQDN' ($strFQDN) -PassThru | 
      Add-Member NoteProperty 'Date' ($strDate) -PassThru | 
      Add-Member NoteProperty 'Diff' ($strDiff) -PassThru) | Format-Table 

結尾:

} else { 
     Write-Verbose "$server is offline" 
    } 
} 
+0

Sam,非常感謝您的幫助 - 編碼時總會有十幾種不同的方式獲得相同的結果。我相信我會在我的下一個腳本中使用你的例子。感謝您花時間來解釋和展示! – user2521736 2014-10-08 15:39:35