【发布时间】:2014-10-07 22:30:21
【问题描述】:
我正在尝试编写一个非常简单的代码,该代码输出服务器名称、上次重新启动日期以及小时和天的差异。我已经尝试了几次 Write-Host 的迭代,但我似乎无法获得我期望的输出。输出应该是这样的:
服务器名 |重启日期 |运行时间(天、小时)
代码如下:
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参数。
标签: powershell