@@ -55,30 +55,30 @@ You can also get the maintenance status for all VMs in a resource group by using
55
55
Get-AzVM -ResourceGroupName myResourceGroup -Status
56
56
```
57
57
58
- The following PowerShell example takes your subscription ID and returns a list of VMs that are scheduled for maintenance.
58
+ The following PowerShell example takes your subscription ID and returns a list of VMs indicating whether they are scheduled for maintenance.
59
59
60
60
``` powershell
61
61
62
- function MaintenanceIterator
63
- {
64
- Select-AzSubscription -SubscriptionId $args[0]
65
-
66
- $rgList= Get-AzResourceGroup
67
-
68
- for ($rgIdx=0; $rgIdx -lt $rgList.Length ; $rgIdx++)
69
- {
70
- $rg = $rgList[$rgIdx]
71
- $vmList = Get-AzVM -ResourceGroupName $rg.ResourceGroupName
72
- for ($vmIdx=0; $vmIdx -lt $vmList.Length ; $vmIdx++)
73
- {
74
- $vm = $vmList[$vmIdx]
75
- $vmDetails = Get-AzVM -ResourceGroupName $rg.ResourceGroupName -Name $vm.Name -Status
76
- if ($vmDetails.MaintenanceRedeployStatus )
77
- {
78
- Write-Output "VM: $($vmDetails.Name) IsCustomerInitiatedMaintenanceAllowed: $($vmDetails.MaintenanceRedeployStatus.IsCustomerInitiatedMaintenanceAllowed) $($vmDetails.MaintenanceRedeployStatus.LastOperationMessage)"
79
- }
80
- }
62
+ function MaintenanceIterator {
63
+ param (
64
+ $SubscriptionId
65
+ )
66
+
67
+ Select-AzSubscription -SubscriptionId $SubscriptionId | Out-Null
68
+
69
+ $rgList = Get-AzResourceGroup
70
+ foreach ($rg in $rgList) {
71
+ $vmList = Get-AzVM -ResourceGroupName $rg.ResourceGroupName
72
+ foreach ($vm in $vmList) {
73
+ $vmDetails = Get-AzVM -ResourceGroupName $rg.ResourceGroupName -Name $vm.Name -Status
74
+ [pscustomobject]@{
75
+ Name = $vmDetails.Name
76
+ ResourceGroupName = $rg.ResourceGroupName
77
+ IsCustomerInitiatedMaintenanceAllowed = [bool]$vmDetails.MaintenanceRedeployStatus.IsCustomerInitiatedMaintenanceAllowed
78
+ LastOperationMessage = $vmDetails.MaintenanceRedeployStatus.LastOperationMessage
79
+ }
81
80
}
81
+ }
82
82
}
83
83
84
84
```
@@ -88,7 +88,11 @@ function MaintenanceIterator
88
88
Using information from the function in the previous section, the following starts maintenance on a VM if ** IsCustomerInitiatedMaintenanceAllowed** is set to true.
89
89
90
90
``` powershell
91
- Restart-AzVM -PerformMaintenance -name $vm.Name -ResourceGroupName $rg.ResourceGroupName
91
+
92
+ MaintenanceIterator -SubscriptionId <Subscription ID> |
93
+ Where-Object -FilterScript {$_.IsCustomerMaintenanceAllowed} |
94
+ Restart-AzVM -PerformMaintenance
95
+
92
96
```
93
97
94
98
## Classic deployments
0 commit comments