我正在尝试在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
}
答案 0 :(得分:0)
这可行:
$lbt = Get-CimInstance -ClassName win32_operatingsystem | Select LastBootUpTime
$now = get-date
if ( [math]::abs(($lbt.lastbootuptime - $now).totalhours) -gt 48 )
{
"Last Boot have been MORE than 48 hrs ago"
}
else
{
"Last Boot have been LESS then 48 hrs ago"
}
答案 1 :(得分:0)
让我们使用这个功能:
function LastBootUpMoreThan ($hour){
$dt = Get-CimInstance -ClassName win32_operatingsystem | Select LastBootUpTime
if (($dt.LastBootUpTime).AddHours($hour) -gt (get-date)) {$false}else {$true}
}
你可以测试:
if ((LastBootUpMoreThan 48)){"Reboot"}else{"Don't reboot"}
答案 2 :(得分:0)
使用SWbemDateTime
对象将WMI时间字符串转换为可与DateTime
值进行比较的Get-Date
对象:
$hours = 48
$lbt = Get-WmiObject -Class Win32_OperatingSystem |
select -Expand LastBootUpTime
$convert = New-Object -COM 'WbemScripting.SWbemDateTime'
$convert.Value = $lbt
if ($convert.GetVarDate() -ge (Get-Date).AddHours(-$hours)) {
Write-Host "Last Boot have been MORE than $hours hours."
} else {
Write-Host "Last Boot have been LESS than $hours hours."
}
答案 3 :(得分:0)
以下是使用.imageWrapper {
display:block;
overflow:hidden;
}
进行比较的单行内容
ticks