Skip to content

Commit af12758

Browse files
wesellisclaude
andcommitted
Fix 2 broken scripts: Azure-WebApp-Restart-Tool and Azure-VM-Health-Monitor
- Added proper param() blocks with validation - Implemented comprehensive error handling - Added comment-based help with examples - Removed hardcoded values - Added logging and status output Manual fixes only - no automated mass changes per user feedback. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent cb8f39e commit af12758

File tree

2 files changed

+270
-22
lines changed

2 files changed

+270
-22
lines changed

scripts/compute/Azure-VM-Health-Monitor.ps1

Lines changed: 170 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,181 @@
22
#Requires -Modules Az.Resources
33
#Requires -Modules Az.Compute
44

5-
<#`n.SYNOPSIS
6-
Check VM health
5+
<#
6+
.SYNOPSIS
7+
Check Azure VM health and status
78
89
.DESCRIPTION
9-
Check VM health
10+
Retrieves comprehensive health information for an Azure Virtual Machine
11+
including power state, provisioning state, and detailed status information
12+
Author: Wes Ellis (wes@wesellis.com)
13+
Version: 1.0
1014
15+
.PARAMETER ResourceGroupName
16+
Name of the resource group containing the VM
1117
12-
Author: Wes Ellis (wes@wesellis.com)
13-
[CmdletBinding()]
18+
.PARAMETER VMName
19+
Name of the virtual machine to check
1420
15-
$ErrorActionPreference = 'Stop'
21+
.PARAMETER Detailed
22+
Show detailed status information including all status codes
23+
24+
.PARAMETER OutputFormat
25+
Output format: Console (default), JSON, or Table
26+
27+
.EXAMPLE
28+
.\Azure-VM-Health-Monitor.ps1 -ResourceGroupName "rg-prod" -VMName "vm-web01"
29+
Gets basic health status for the specified VM
30+
31+
.EXAMPLE
32+
.\Azure-VM-Health-Monitor.ps1 -ResourceGroupName "rg-prod" -VMName "vm-web01" -Detailed
33+
Gets detailed health status including all status codes
34+
35+
.EXAMPLE
36+
.\Azure-VM-Health-Monitor.ps1 -ResourceGroupName "rg-prod" -VMName "vm-web01" -OutputFormat JSON
37+
Outputs health status in JSON format
1638
39+
.NOTES
40+
Requires Az.Compute module and appropriate permissions
41+
#>
42+
43+
[CmdletBinding()]
44+
param(
45+
[Parameter(Mandatory = $true)]
46+
[ValidateNotNullOrEmpty()]
1747
[string]$ResourceGroupName,
18-
[string]$VmName
48+
49+
[Parameter(Mandatory = $true)]
50+
[ValidateNotNullOrEmpty()]
51+
[string]$VMName,
52+
53+
[Parameter()]
54+
[switch]$Detailed,
55+
56+
[Parameter()]
57+
[ValidateSet("Console", "JSON", "Table")]
58+
[string]$OutputFormat = "Console"
1959
)
20-
$VM = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VmName -Status
21-
Write-Output "VM Name: $($VM.Name)"
22-
Write-Output "Resource Group: $($VM.ResourceGroupName)"
23-
Write-Output "Location: $($VM.Location)"
24-
Write-Output "Power State: $($VM.PowerState)"
25-
Write-Output "Provisioning State: $($VM.ProvisioningState)"
26-
foreach ($Status in $VM.Statuses) {
27-
Write-Output "Status: $($Status.Code) - $($Status.DisplayStatus)"`n}
60+
61+
$ErrorActionPreference = 'Stop'
62+
63+
function Write-LogMessage {
64+
param(
65+
[Parameter(Mandatory = $true)]
66+
[string]$Message,
67+
68+
[Parameter()]
69+
[ValidateSet("INFO", "WARN", "ERROR", "SUCCESS")]
70+
[string]$Level = "INFO"
71+
)
72+
73+
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
74+
$colorMap = @{
75+
"INFO" = "Cyan"
76+
"WARN" = "Yellow"
77+
"ERROR" = "Red"
78+
"SUCCESS" = "Green"
79+
}
80+
81+
if ($OutputFormat -eq "Console") {
82+
Write-Host "[$timestamp] [$Level] $Message" -ForegroundColor $colorMap[$Level]
83+
}
84+
}
85+
86+
try {
87+
Write-LogMessage "Retrieving VM health status..." -Level "INFO"
88+
Write-LogMessage "Resource Group: $ResourceGroupName" -Level "INFO"
89+
Write-LogMessage "VM Name: $VMName" -Level "INFO"
90+
91+
# Get VM with status
92+
$vm = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName -Status -ErrorAction Stop
93+
94+
if (-not $vm) {
95+
throw "VM '$VMName' not found in resource group '$ResourceGroupName'"
96+
}
97+
98+
# Build health status object
99+
$healthStatus = [PSCustomObject]@{
100+
VMName = $vm.Name
101+
ResourceGroup = $vm.ResourceGroupName
102+
Location = $vm.Location
103+
PowerState = ($vm.Statuses | Where-Object { $_.Code -like "PowerState/*" } | Select-Object -First 1).DisplayStatus
104+
ProvisioningState = $vm.ProvisioningState
105+
VMSize = $vm.HardwareProfile.VmSize
106+
OSType = $vm.StorageProfile.OsDisk.OsType
107+
CheckTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
108+
Statuses = @()
109+
}
110+
111+
# Add detailed statuses if requested
112+
if ($Detailed) {
113+
$healthStatus.Statuses = $vm.Statuses | ForEach-Object {
114+
[PSCustomObject]@{
115+
Code = $_.Code
116+
Level = $_.Level
117+
DisplayStatus = $_.DisplayStatus
118+
Message = $_.Message
119+
Time = $_.Time
120+
}
121+
}
122+
}
123+
124+
# Output in requested format
125+
switch ($OutputFormat) {
126+
"JSON" {
127+
$healthStatus | ConvertTo-Json -Depth 5
128+
}
129+
"Table" {
130+
Write-Host "`n=== VM Health Status ===" -ForegroundColor Cyan
131+
$healthStatus | Format-List VMName, ResourceGroup, Location, PowerState, ProvisioningState, VMSize, OSType, CheckTime
132+
133+
if ($Detailed) {
134+
Write-Host "`n=== Detailed Statuses ===" -ForegroundColor Cyan
135+
$healthStatus.Statuses | Format-Table Code, DisplayStatus, Level -AutoSize
136+
}
137+
}
138+
"Console" {
139+
Write-Host "`n=== VM Health Status ===" -ForegroundColor Cyan
140+
Write-Host "VM Name: $($healthStatus.VMName)" -ForegroundColor White
141+
Write-Host "Resource Group: $($healthStatus.ResourceGroup)" -ForegroundColor White
142+
Write-Host "Location: $($healthStatus.Location)" -ForegroundColor White
143+
Write-Host "VM Size: $($healthStatus.VMSize)" -ForegroundColor White
144+
Write-Host "OS Type: $($healthStatus.OSType)" -ForegroundColor White
145+
146+
# Color-code power state
147+
$powerStateColor = switch -Wildcard ($healthStatus.PowerState) {
148+
"*running*" { "Green" }
149+
"*deallocated*" { "Yellow" }
150+
"*stopped*" { "Red" }
151+
default { "White" }
152+
}
153+
Write-Host "Power State: $($healthStatus.PowerState)" -ForegroundColor $powerStateColor
154+
Write-Host "Provisioning State: $($healthStatus.ProvisioningState)" -ForegroundColor White
155+
Write-Host "Check Time: $($healthStatus.CheckTime)" -ForegroundColor Gray
156+
157+
if ($Detailed) {
158+
Write-Host "`n=== Detailed Statuses ===" -ForegroundColor Cyan
159+
foreach ($status in $vm.Statuses) {
160+
$statusColor = switch ($status.Level) {
161+
"Info" { "Cyan" }
162+
"Warning" { "Yellow" }
163+
"Error" { "Red" }
164+
default { "White" }
165+
}
166+
Write-Host " [$($status.Level)] $($status.Code): $($status.DisplayStatus)" -ForegroundColor $statusColor
167+
if ($status.Message) {
168+
Write-Host " Message: $($status.Message)" -ForegroundColor Gray
169+
}
170+
}
171+
}
172+
Write-Host ""
173+
}
174+
}
175+
176+
# Return the health status object
177+
return $healthStatus
178+
179+
} catch {
180+
Write-LogMessage "Failed to retrieve VM health: $($_.Exception.Message)" -Level "ERROR"
181+
throw
182+
}
Lines changed: 100 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,111 @@
11
#Requires -Version 7.4
2+
#Requires -Modules Az.Websites
23

3-
<#`n.SYNOPSIS
4-
Azure script
4+
<#
5+
.SYNOPSIS
6+
Restart Azure Web App
57
68
.DESCRIPTION
7-
.DESCRIPTION`n Automate Azure operations
9+
Restarts an Azure Web App or App Service with proper error handling and validation
810
Author: Wes Ellis (wes@wesellis.com)
9-
[CmdletBinding()]
11+
Version: 1.0
1012
11-
$ErrorActionPreference = 'Stop'
13+
.PARAMETER ResourceGroupName
14+
Name of the resource group containing the web app
15+
16+
.PARAMETER AppName
17+
Name of the Azure Web App to restart
18+
19+
.PARAMETER Force
20+
Skip confirmation prompt
21+
22+
.EXAMPLE
23+
.\Azure-WebApp-Restart-Tool.ps1 -ResourceGroupName "rg-prod" -AppName "webapp-prod"
24+
Restarts the specified web app with confirmation
25+
26+
.EXAMPLE
27+
.\Azure-WebApp-Restart-Tool.ps1 -ResourceGroupName "rg-prod" -AppName "webapp-prod" -Force
28+
Restarts the web app without confirmation
29+
30+
.NOTES
31+
Requires Az.Websites module and appropriate permissions
32+
#>
1233

34+
[CmdletBinding(SupportsShouldProcess)]
35+
param(
36+
[Parameter(Mandatory = $true)]
37+
[ValidateNotNullOrEmpty()]
1338
[string]$ResourceGroupName,
14-
[string]$AppName
39+
40+
[Parameter(Mandatory = $true)]
41+
[ValidateNotNullOrEmpty()]
42+
[string]$AppName,
43+
44+
[Parameter()]
45+
[switch]$Force
1546
)
16-
Restart-AzWebApp -ResourceGroupName $ResourceGroupName -Name $AppName
1747

48+
$ErrorActionPreference = 'Stop'
49+
50+
function Write-LogMessage {
51+
param(
52+
[Parameter(Mandatory = $true)]
53+
[string]$Message,
54+
55+
[Parameter()]
56+
[ValidateSet("INFO", "WARN", "ERROR", "SUCCESS")]
57+
[string]$Level = "INFO"
58+
)
59+
60+
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
61+
$colorMap = @{
62+
"INFO" = "Cyan"
63+
"WARN" = "Yellow"
64+
"ERROR" = "Red"
65+
"SUCCESS" = "Green"
66+
}
67+
68+
Write-Host "[$timestamp] [$Level] $Message" -ForegroundColor $colorMap[$Level]
69+
}
70+
71+
try {
72+
Write-LogMessage "Starting Web App restart operation" -Level "INFO"
73+
Write-LogMessage "Resource Group: $ResourceGroupName" -Level "INFO"
74+
Write-LogMessage "App Name: $AppName" -Level "INFO"
75+
76+
# Verify the web app exists
77+
Write-LogMessage "Verifying web app exists..." -Level "INFO"
78+
$webApp = Get-AzWebApp -ResourceGroupName $ResourceGroupName -Name $AppName -ErrorAction Stop
79+
80+
if (-not $webApp) {
81+
throw "Web app '$AppName' not found in resource group '$ResourceGroupName'"
82+
}
83+
84+
Write-LogMessage "Web app found: $($webApp.Name)" -Level "SUCCESS"
85+
Write-LogMessage "Location: $($webApp.Location)" -Level "INFO"
86+
Write-LogMessage "State: $($webApp.State)" -Level "INFO"
87+
88+
# Confirm restart if not using -Force
89+
if (-not $Force -and $PSCmdlet.ShouldProcess($AppName, "Restart Web App")) {
90+
$confirmation = Read-Host "Are you sure you want to restart '$AppName'? (Y/N)"
91+
if ($confirmation -ne 'Y') {
92+
Write-LogMessage "Restart cancelled by user" -Level "WARN"
93+
return
94+
}
95+
}
96+
97+
# Restart the web app
98+
Write-LogMessage "Restarting web app..." -Level "INFO"
99+
Restart-AzWebApp -ResourceGroupName $ResourceGroupName -Name $AppName -ErrorAction Stop
100+
101+
Write-LogMessage "Web app restarted successfully" -Level "SUCCESS"
102+
103+
# Wait a moment and check status
104+
Start-Sleep -Seconds 2
105+
$updatedWebApp = Get-AzWebApp -ResourceGroupName $ResourceGroupName -Name $AppName
106+
Write-LogMessage "Current state: $($updatedWebApp.State)" -Level "INFO"
18107

108+
} catch {
109+
Write-LogMessage "Failed to restart web app: $($_.Exception.Message)" -Level "ERROR"
110+
throw
111+
}

0 commit comments

Comments
 (0)