-
Notifications
You must be signed in to change notification settings - Fork 62
/
Handling_planned_maintenance.ps1
52 lines (42 loc) · 1.98 KB
/
Handling_planned_maintenance.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Set-Location c:\
Clear-Host
Install-Module -Name Az -Force -AllowClobber -Verbose
#Log into Azure
Connect-AzAccount
#Select the correct subscription
Get-AzSubscription -SubscriptionName "MSDN Platforms" | Select-AzSubscription
Get-AzContext
#Maintenance information is returned only if there is maintenance planned
Get-AzVM -ResourceGroupName tw-rg01 -Name tw-winsrv -Status
#Example output
MaintenanceRedeployStatus :
IsCustomerInitiatedMaintenanceAllowed : True
PreMaintenanceWindowStartTime : 5/14/2020 12:30:00 PM
PreMaintenanceWindowEndTime : 5/19/2020 12:30:00 PM
MaintenanceWindowStartTime : 5/21/2020 4:30:00 PM
MaintenanceWindowEndTime : 6/4/2020 4:30
LastOperationResultCode : None
#The following example takes your subscription ID and returns a list of
#VMs that are scheduled for maintenance.
function MaintenanceIterator
{
Select-AzSubscription -SubscriptionId $args[0]
$rgList= Get-AzResourceGroup
for ($rgIdx=0; $rgIdx -lt $rgList.Length ; $rgIdx++)
{
$rg = $rgList[$rgIdx]
$vmList = Get-AzVM -ResourceGroupName $rg.ResourceGroupName
for ($vmIdx=0; $vmIdx -lt $vmList.Length ; $vmIdx++)
{
$vm = $vmList[$vmIdx]
$vmDetails = Get-AzVM -ResourceGroupName $rg.ResourceGroupName -Name $vm.Name -Status
if ($vmDetails.MaintenanceRedeployStatus )
{
Write-Output "VM: $($vmDetails.Name) IsCustomerInitiatedMaintenanceAllowed: $($vmDetails.MaintenanceRedeployStatus.IsCustomerInitiatedMaintenanceAllowed) $($vmDetails.MaintenanceRedeployStatus.LastOperationMessage)"
}
}
}
}
#Using information from the function in the previous section, the following starts maintenance
#on a VM if IsCustomerInitiatedMaintenanceAllowed is set to true
Restart-AzVM -PerformMaintenance -Name $vm.Name -ResourceGroupName $rg.ResourceGroupName