Last active
May 6, 2024 20:09
-
-
Save smoonlee/0f400230e8fa876ad54bed5bc50feff9 to your computer and use it in GitHub Desktop.
Get Uptime for Linux and Windows machines in Azure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function - Get Azure Virtual Machine System Uptime | |
function Get-AzSystemUptime { | |
param ( | |
[string] $subscriptionId, | |
[string] $resourceGroup, | |
[string] $vmName | |
) | |
if ($subscriptionId) { | |
Set-AzContext -SubscriptionId $subscriptionId | Out-Null | |
$subFriendlyName = (Get-AzContext).Subscription.Name | |
Write-Output "[Azure] :: Setting Azure Subscription to $subFriendlyName " | |
} | |
$vmState = (Get-AzVM -ResourceGroupName $resourceGroup -Name $vmName -Status).Statuses.DisplayStatus[1] | |
if ($vmState -ne 'VM running') { | |
Write-Warning "[Azure] :: $vmName is not running. Please start the VM and try again." | |
return | |
} | |
$osType = (Get-AzVM -ResourceGroupName $resourceGroup -Name $vmName).StorageProfile.OsDisk.OsType | |
if ($osType -eq 'Windows') { | |
Write-Output "[Azure] :: Getting System Uptime for $vmName in $resourceGroup..." | |
Write-Warning "This may take up to 35 seconds" | |
$response = Invoke-AzVMRunCommand -ResourceGroupName $resourceGroup -Name $vmName -CommandId 'RunPowerShellScript' -ScriptString ' | |
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 = (Get-Date) - $operatingSystem.LastBootUpTime | |
$uptimeSeconds = $uptimeSeconds.TotalSeconds | |
# Convert uptime to a readable format | |
$uptime = ConvertToReadableTime -uptimeSeconds $uptimeSeconds | |
# Get the last reboot time | |
$lastRebootTime = $operatingSystem.LastBootUpTime | |
$lastRebootTime = $lastRebootTime.ToString("dd/MM/yyyy HH:mm:ss") | |
# Display the results | |
Write-Output " " # Required for script spacing | |
Write-Output "[Azure] :: Hostname: $hostname" | |
Write-Output "[Azure] :: Uptime: $uptime" | |
Write-Output "[Azure] :: Last Reboot Time: $lastRebootTime" | |
' | |
$response.Value[0].Message | |
} | |
if ($osType -eq 'Linux') { | |
Write-Output "[Azure] :: Getting System Uptime for $vmName in $resourceGroup..." | |
Write-Warning "This may take up to 35 seconds" | |
$response = Invoke-AzVMRunCommand -ResourceGroupName $resourceGroup -Name $vmName -CommandId 'RunShellScript' -ScriptString ' | |
echo "[Azure] :: Hostname: $(hostname)" | |
echo "[Azure] :: Uptime: $(uptime -p )" | |
echo "[Azure] :: Last Reboot Time: $(uptime -s)" | |
' | |
$pattern = '\[stdout\]([\s\S]*?)\[stderr\]' | |
if ($response.value[0].Message -match $pattern) { | |
$stdoutText = $matches[1].Trim() | |
Write-Output `r $stdoutText | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment