我想把一個非常簡單的代碼輸出服務器名稱,最後一次重新啓動日期,然後在幾小時和幾天的差異。我已經嘗試了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 {}
如果有人能解釋是如何結合變量的工作原理,以及如何格式化輸出,我會很感激。
在此先感謝。
你得到什麼輸出?如果在'if'語句中使用'Test-Connection',則應該使用'-Quiet'參數。 – 2014-10-07 23:05:46