Skip to content

Commit c326ad0

Browse files
Merge pull request #106157 from mikejwhat/master
Sample code enhancements
2 parents 9f5b21d + 9afb832 commit c326ad0

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

articles/virtual-machines/maintenance-notifications-powershell.md

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,30 @@ You can also get the maintenance status for all VMs in a resource group by using
5555
Get-AzVM -ResourceGroupName myResourceGroup -Status
5656
```
5757

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.
5959

6060
```powershell
6161
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+
}
8180
}
81+
}
8282
}
8383
8484
```
@@ -88,7 +88,11 @@ function MaintenanceIterator
8888
Using information from the function in the previous section, the following starts maintenance on a VM if **IsCustomerInitiatedMaintenanceAllowed** is set to true.
8989

9090
```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+
9296
```
9397

9498
## Classic deployments

0 commit comments

Comments
 (0)