Created
April 29, 2022 22:32
-
-
Save celloza/4694ea6b5f9a8d38ccd3985335fb6b96 to your computer and use it in GitHub Desktop.
Azure Runbook to resize a VM
This file contains hidden or 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
param | |
( | |
[Parameter (Mandatory = $true)] | |
[string] $ResourceGroupName, | |
[Parameter (Mandatory = $true)] | |
[string] $VMName, | |
[Parameter (Mandatory = $true)] | |
[string] $NewVMSize, | |
[Parameter (Mandatory = $true)] | |
[bool] $EnableAcceleratedNetworking | |
) | |
Function Get-TimeStamp { | |
return "[{0:yyyy-MM-dd} {0:HH:mm:ss}]" -f (Get-Date) | |
} | |
Function Get-OutputObject{ | |
param ( | |
$Success, | |
$VmName, | |
$NewVMSize | |
) | |
$data = [PSCustomObject]@{ | |
success = $Success | |
vmName = $VmName | |
newSize = $NewVMSize | |
} | |
return $data | |
} | |
Write-Verbose "$(Get-TimeStamp) Importing Modules" | |
Import-Module -Name Az.Automation | |
Import-Module -Name Az.Compute | |
Import-Module -Name Az.Network | |
Import-Module -Name Az.Resources | |
Write-Verbose "$(Get-TimeStamp) Validating Arguments" | |
if (-Not $ResourceGroupName) { | |
Write-Error "$(Get-TimeStamp) Runbook was missing the expected ""ResourceGroupName"" property. Args were: $args" | |
exit 0 | |
} | |
if (-Not $VMName) { | |
Write-Error "$(Get-TimeStamp) Runbook was missing the expected ""VMName"" property. Args were: $args" | |
exit 0 | |
} | |
# Set the current automation connection's authenticated account to use for future Azure Resource Manager cmdlet requests. | |
Write-Verbose "$(Get-TimeStamp) Obtaining Service Principle Connection..." | |
$ServicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection" | |
Connect-AzAccount -ServicePrincipal -ErrorAction Stop ` | |
-TenantId $ServicePrincipalConnection.TenantId ` | |
-ApplicationId $ServicePrincipalConnection.ApplicationId ` | |
-CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint | Write-Verbose | |
Set-AzContext -Subscription $ServicePrincipalConnection.SubscriptionID -ErrorAction Stop | Write-Verbose | |
Write-Verbose "$(Get-TimeStamp) Logged in as AzureRunAsConnection Service Principle" | |
# Get the VM to be scaled up or down | |
Write-Verbose "$(Get-TimeStamp) Retrieving VM Information" | |
$vm = Get-AzVM -ResourceGroupName $ResourceGroupName -VMName $VMName -ErrorAction Stop | |
# Check if the VM already has the requested configuration | |
if ($vm.HardwareProfile.VmSize -eq $NewVMSize) { | |
Write-Error "$(Get-TimeStamp) Nothing to do! The VM already has the configuration $NewVMSize" | |
exit 1 | |
} | |
# Collect information about the VM's network configuration | |
Write-Verbose "$(Get-TimeStamp) Retrieving Network Interface Information..." | |
$nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces[0].id -ErrorAction Stop | |
try { | |
# Change the configuration | |
Write-Verbose "$(Get-TimeStamp) Changing VM Configuration..." | |
$vm.HardwareProfile.VmSize = $NewVMSize | |
$result = Update-AzVM -VM $vm -ResourceGroupName $ResourceGroupName -ErrorAction Stop | |
if($result.IsSuccessStatusCode) | |
{ | |
Write-Verbose "$(Get-TimeStamp) Successfully updated the VM configuration to $NewVMSize" | |
$output = Get-OutputObject -Success $true -VmName $vm.Name -NewVmSize $vm.HardwareProfile.VmSize | ConvertTo-Json | |
Write-Output $output | |
} | |
else | |
{ | |
Write-Verbose "$(Get-TimeStamp) Could not update the VM configuration. $ReasonPhrase" | |
$output = Get-OutputObject -Success $false -VmName $vm.Name -NewVmSize $vm.HardwareProfile.VmSize | ConvertTo-Json | |
Write-Output $output | |
} | |
} catch { | |
Write-Verbose "$(Get-TimeStamp) Handling error while changing VM Configuration..." | |
if ($_.Exception.Message.Contains("VMSizeIsNotPermittedToEnableAcceleratedNetworking")) { | |
Write-Verbose "$(Get-TimeStamp) It appears as though the new VM Size was not accepted because it does not support Accelerated Networking." | |
Write-Verbose "$(Get-TimeStamp) Attempting to disable Accelerated Networking on the VM's network interface: $($nic.Name)..." | |
$nic.EnableAcceleratedNetworking = $False | |
try { | |
$nic | Set-AzNetworkInterface -ErrorAction Stop | |
} catch { | |
Write-Error "$(Get-TimeStamp) Error while disabling accelerated networking: $_" | |
throw | |
} | |
Write-Verbose "$(Get-TimeStamp) Done. Now trying to change the VM Configuration again..." | |
try { | |
Update-AzVM -VM $vm -ResourceGroupName $ResourceGroupName -ErrorAction Stop | |
Write-Verbose "$(Get-TimeStamp) Successfully updated the VM configuration to $NewVMSize" | |
} catch { | |
Write-Error "$(Get-TimeStamp) Unexpected error while updating the VM configuration after disabling accelerated networking: $_" | |
throw | |
} | |
} else { | |
Write-Error "$(Get-TimeStamp) Unexpected error while updating the VM configuration. Error code is: $($_.Exception.ErrorCode). Full Details: $_" | |
throw | |
} | |
} finally { | |
if($EnableAcceleratedNetworking) | |
{ | |
if (-Not $nic.EnableAcceleratedNetworking) { | |
Write-Verbose "$(Get-TimeStamp) Attempting to restore accellerated networking on the VM..." | |
try { | |
$nic.EnableAcceleratedNetworking = $True | |
$nic | Set-AzNetworkInterface -ErrorAction Stop | |
Write-Verbose "$(Get-TimeStamp) Restored Accelerated Networking on the network interface: $($nic.Name)" | |
} catch { | |
# This is expected to fail if we have configured the VM to a size that does not support it. | |
Write-Verbose "$(Get-TimeStamp) Retrieving VM Information..." | |
$vm = Get-AzVM -ResourceGroupName $ResourceGroupName -VMName $VMName -ErrorAction Stop | |
Write-Verbose "$(Get-TimeStamp) Accelerated Networking not supported on the network interface $($nic.Name) with the current VM size $($vm.HardwareProfile.VmSize)" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment