Reputation: 4736
The below checks for uptime. One condition I did not tink of (except for now) is if the server can be pinged BUT CANNOT GET THE UPTIME - I need the below script to error out if that is the case. I cannot think of how to do this - any ideas?
CODE:
#clear
$Computers = Get-Content "E:\DATA\PS_Jobs\Patching_Uptime\Saturday_Servers.txt"
Foreach($computer in $Computers)
{
if (Test-Connection -ComputerName $computer -Quiet)
{
$LastBoot = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime
$sysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($LastBoot)
$days = $sysuptime.Days
$DaystoHours = ($sysuptime.Days)*24
$hours = $sysuptime.hours
$TotalHours = $DaystoHours + $hours
if($TotalHours -gt '12')
{
Write-EventLog -LogName WinLondonUptime -Source Uptime -EntryType Error -EventId 5 -Message "$computer - FAILED - Servers Uptime is GREATER then 12 hours or not contactable - Uptime is $days Days and $hours Hours - This is the Saturday patching run"
}
else
{
Write-EventLog -LogName WinLondonUptime -Source Uptime -EntryType Information -EventId 4 -Message "$computer - SUCCESS - Servers uptime is less than 12 hours - Uptime is $days Days and $hours Hours - This is the Saturday patching run"
}
}
else
{
Write-EventLog -LogName WinLondonUptime -Source Uptime -EntryType Error -EventId 5 -Message "$computer - FAILED - Server is not contactable - This is the Saturday patching run"
}
}
Upvotes: 0
Views: 2306
Reputation: 365
My solution is as follows. I'm pinging the hostname and getting the boot time if it does not get the error.
[System.Net.Dns]::GetHostAddresses("hostname").ipaddresstostring
if($? -eq $true){
Get-CimInstance -ClassName win32_operatingsystem | select lastbootuptime
}
Upvotes: 0
Reputation: 3419
I can't test this from here at the moment but perhaps try nesting another if statement to enumerate the success or failure of the call to WMI?
if (Test-Connection -ComputerName $computer -Quiet)
{
if((Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime){
$LastBoot = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime
$sysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($LastBoot)
Upvotes: 0
Reputation: 2166
This person sort of has the same problem with WMI not having a timeout feature. What you might try is creating a job to query WMI and get the LBUT. Then just set a timer on the job. But if you want to just do some error handling you should use try{}
and catch{}
blocks.
try{
$lbut = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime
}
catch{
"FAILED"
continue
}
You might want to look at using CIM instead of WMI.
Upvotes: 1
Reputation: 43499
You can encapsulate your code in a try/catch and use a timespan object. Something like this (not tested, but it gives you the idea):
try
{
Test-Connection -ComputerName $computer -Count 1 -ErrorAction Stop
$wmi = Get-WmiObject Win32_OperatingSystem -computer $computer
$time = $wmi.ConvertToDateTime($wmi.Lastbootuptime)
[TimeSpan] $uptime = New-TimeSpan $time $(get-date)
if ($uptime.Hours -gt 12)
{
Write-EventLog -LogName WinLondonUptime -Source Uptime -EntryType Error -EventId 5 -Message "$computer - FAILED - Servers Uptime is GREATER then 12 hours or not contactable - Uptime is $days Days and $hours Hours - This is the Saturday patching run"
}
else
{
Write-EventLog -LogName WinLondonUptime -Source Uptime -EntryType Information -EventId 4 -Message "$computer - SUCCESS - Servers uptime is less than 12 hours - Uptime is $($uptime.Days) Days and $($uptime.Hours) Hours - This is the Saturday patching run"
}
}
catch [System.Management.Automation.ActionPreferenceStopException]
{
Write-EventLog -LogName WinLondonUptime -Source Uptime -EntryType Error -EventId 5 -Message "$computer - FAILED - Server is not contactable - This is the Saturday patching run"
}
Upvotes: 1
Reputation: 3311
Erroring Out is typically defined as returning a non zero exit code, this can be done by
Exit 1
or
Exit -1
Or whatever number you prefer.
You can also throw exceptions as well, if you want to send a specific error message and a really nasty error, there's an answer here.
Upvotes: 0
Reputation: 24575
You can get also get a computer's last bootup time using WMI:
$wmi = [WMI] ""
$operatingSystem = get-wmiobject Win32_OperatingSystem -computername "."
$wmi.ConvertToDateTime($operatingSystem.LastBootUpTime)
Bill
Upvotes: 1