Skip to content

Instantly share code, notes, and snippets.

@smoonlee
Last active November 10, 2023 08:40
Show Gist options
  • Save smoonlee/fc17b2b931566160f7538957fd32e23a to your computer and use it in GitHub Desktop.
Save smoonlee/fc17b2b931566160f7538957fd32e23a to your computer and use it in GitHub Desktop.
PowerShell 5 - Get Last Reboot for Azure VM
# Function to convert seconds to a readable format
function ConvertToReadableTime {
param (
[int]$uptimeSeconds
)
$uptime = New-TimeSpan -Seconds $uptimeSeconds
"{0} days, {1} hours, {2} minutes, {3} seconds" -f $uptime.Days, $uptime.Hours, $uptime.Minutes, $uptime.Seconds
}
# Get the hostname
$hostname = [System.Net.Dns]::GetHostName()
# Get the operating system information
$operatingSystem = Get-CimInstance Win32_OperatingSystem
# Get the uptime in seconds
$uptimeSeconds = $operatingSystem.LastBootUpTime
$uptimeSeconds = (Get-Date) - $uptimeSeconds
$uptimeSeconds = $uptimeSeconds.TotalSeconds
# Convert uptime to a readable format
$uptime = ConvertToReadableTime -uptimeSeconds $uptimeSeconds
# Get the last reboot time
$lastRebootTime = $operatingSystem.LastBootUpTime
# Display the results
Write-Host "Hostname: $hostname"
Write-Host "Uptime: $uptime"
Write-Host "Last Reboot Time: $lastRebootTime"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment