我想把一个非常简单的代码输出服务器名称,最后一次重新启动日期,然后在几小时和几天的差异。我已经尝试了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