【发布时间】:2015-05-27 13:51:43
【问题描述】:
我正在尝试在 Powershell 中创建一个脚本;
获取 PC 的上次启动时间并检查它是否大于 48 小时 如果更大 重启机器 否则退出
我似乎无法正确计算。
cls
# Declaring Variables
$lbt = Get-CimInstance -ClassName win32_operatingsystem | Select LastBootUpTime
$lbt = $lbt.lastbootuptime
$ts = "{0:t}" -f $lbt
$texp = $lbt.AddHours(0)
#$ts = New-TimeSpan -Hours +3 #48 Hours Time Span from the last Boot Up Time
# Get Last Boot Up Time and compare
# Check LastBootUpTime
# If LastBootUpTime is grater than 48hrs continue executing ELSE RemoveIfExist -eq $true
Write-host "Last Boot : " $lbt
write-host "Now : " (Get-Date) `n
If ($lbt -ge $texp) {
Write-Host "Last Boot have been MORE than" ("{0:t}" -f $texp) hrs `n`n`n
}
else {
write-host "Last Boot have been LESS then" ("{0:t}" -f $texp) hrs `n`n`n
}
【问题讨论】:
标签: powershell scripting system-administration