'Uptime-Prompt.ps1'
# Get the last boot time
$server = Read-Host -prompt "What system to check Uptime for?"
$lastBootTime = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $server | Select-Object -ExpandProperty LastBootUpTime
# Output the raw last boot time for debugging
Write-Host "Raw Last Boot Time: $lastBootTime"
# Check if the last boot time is valid
if (-not [string]::IsNullOrEmpty($lastBootTime)) {
# Check if the format is in DMTF format
if ($lastBootTime.Length -eq 25) {
# Convert the last boot time to a DateTime object using the DMTF format
$lastBootDateTime = [Management.ManagementDateTimeConverter]::ToDateTime($lastBootTime)
} else {
# Manually parse the date if it's in a different format
$lastBootDateTime = [datetime]::ParseExact($lastBootTime, "MM/dd/yyyy HH:mm:ss", $null)
}
Write-Host "Converted Last Boot Time: $lastBootDateTime"
# Calculate the uptime
$uptime = (Get-Date) - $lastBootDateTime
# Get the local time zone
$timeZone = (Get-TimeZone).Id
$localTime = Get-Date
# Output the uptime in a more readable format
$uptimeFormatted = "{0} days, {1} hours, {2} minutes" -f $uptime.Days, $uptime.Hours, $uptime.Minutes
Write-Host "The server has been up for: $uptimeFormatted"
Write-Host "Current Local Time ($timeZone): $localTime"
} else {
Write-Host "Unable to retrieve the last boot time. Please check the system configuration."
}
<#
NOTE: This script assumes access to get the uptime. You can wrap the script in a 'ForEach' too, and write out to a report, but then you have to set error checking and that can get in the weeds fast.
-Patrck
#>

Get Uptime Remotely for Windows Operating System
by
Tags:

