Powershell v3: How to check if a PC is up for > 30 days
step 1- Check uptime. If > 30 days, then go for step 2; else exit step 2- Check if desktop is unlocked. If yes, popup saying it will be rebooted in next 24 hours. If locked, exit step 3- reboot
Is that accurate? Can be do in powershell.
$computer = "<Computer name>"
if (Test-Connection $computer -Count 2 -Quiet) {
try {
$user = $null
$user = gwmi -Class win32_computersystem -ComputerName $computer | select -ExpandProperty username -ErrorAction Stop
}
catch { Write-Host "Not logged on"; return }
try {
if ((Get-Process logonui -ComputerName $computer -ErrorAction Stop) -and ($user)) {
Write-Host "Workstation locked by $user"
}
}
catch { if ($user)
{
Write-Host "$user logged on"
$wmi = Get-WmiObject -ComputerName $computer -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem"
$now = Get-Date
$boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
$uptime = $now - $boottime
$d =$uptime.days
$h =$uptime.hours
$m =$uptime.Minutes
$s = $uptime.Seconds
# above h/m/s are optional, but good to have!
if ($d -gt 30) {
Write-Host "Greather than 30 days"
# call shutdown tool here or do other activities
}
else { Write-Host "Less than 30 days" }
} }
}
else { Write-Host "$computer Offline" }
Is that accurate? Can be do in powershell.
$computer = "<Computer name>"
if (Test-Connection $computer -Count 2 -Quiet) {
try {
$user = $null
$user = gwmi -Class win32_computersystem -ComputerName $computer | select -ExpandProperty username -ErrorAction Stop
}
catch { Write-Host "Not logged on"; return }
try {
if ((Get-Process logonui -ComputerName $computer -ErrorAction Stop) -and ($user)) {
Write-Host "Workstation locked by $user"
}
}
catch { if ($user)
{
Write-Host "$user logged on"
$wmi = Get-WmiObject -ComputerName $computer -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem"
$now = Get-Date
$boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
$uptime = $now - $boottime
$d =$uptime.days
$h =$uptime.hours
$m =$uptime.Minutes
$s = $uptime.Seconds
# above h/m/s are optional, but good to have!
if ($d -gt 30) {
Write-Host "Greather than 30 days"
# call shutdown tool here or do other activities
}
else { Write-Host "Less than 30 days" }
} }
}
else { Write-Host "$computer Offline" }
0 Comments:
Post a Comment
<< Home