%3CLINGO-SUB%20id%3D%22lingo-sub-1469747%22%20slang%3D%22en-US%22%3EResize%20Azure%20Virtual%20Machines%20in%20Parallel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1469747%22%20slang%3D%22en-US%22%3E%3CP%3EImagine%20you%20had%20to%20resize%20a%20whole%20bunch%20of%20VMs%20in%20Azure%2C%20rather%20than%20doing%20a%20resize%20one%20after%20the%20other%20in%20serial%2C%20you%20can%20do%20this%20in%20parallel%20with%20the%20new%26nbsp%3B%3CSTRONG%3EForEach-Object%20Parallel%3C%2FSTRONG%3E%20feature%20in%20PowerShell%20v7.%20More%20details%20on%20this%20feature%20here%20-%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdevblogs.microsoft.com%2Fpowershell%2Fpowershell-foreach-object-parallel-feature%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdevblogs.microsoft.com%2Fpowershell%2Fpowershell-foreach-object-parallel-feature%2F%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDoing%20this%20in%20%3CSTRONG%3Eparallel%3C%2FSTRONG%3E%20will%20greatly%20speed%20up%20the%20process%20by%20resizing%20any%20number%20of%20VMs%20all%20at%20once.%20To%20start%20with%2C%20you%20will%20need%20a%20CSV%20file%20with%20two%20columns%2C%26nbsp%3B%3CSTRONG%3Evmname%3C%2FSTRONG%3E%2C%3CSTRONG%3Etargetsize%3C%2FSTRONG%3E.%20Enter%20all%20the%20VM%20names%20of%20the%20VMs%20that%20you%20want%20to%20resize%2C%20also%20the%20correct%20VM%20size.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CU%3E%3CSTRONG%3ESample%20CSV%20file%3C%2FSTRONG%3E%3C%2FU%3E%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-basic%22%3E%3CCODE%3Evmname%2Ctargetsize%0AVM01%2CStandard_DS14_v2%0AVM02%2CStandard_DS14_v2%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20get%20a%20list%20of%20Azure%20VM%20sizes%20and%20the%20correct%20format%20for%20the%20name%2C%20run%20the%20following%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E(Get-AzVMSize%20-Location%20'Australia%20East').Name%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20script%20will%20loop%20through%20all%20the%20VMs%20in%20the%20CSV%20file%2C%20shutting%20down%20the%20VM%20first%20before%20the%20resize%20happens.%20A%20full%20shutdown%20of%20the%20VM%20ensures%20that%20all%20target%20VM%20sizes%20are%20on%20offer%20across%20all%20Azure%20clusters%2C%20not%20just%20VM%20sizes%20of%20the%20current%20cluster%20in%20which%20your%20VM%20is%20a%20member%20of.%20First%20the%20script%20will%20get%20the%20status%20of%20the%20%3CSTRONG%3EPrivate%26nbsp%3BIP%26nbsp%3BAllocation%26nbsp%3BMethod%3C%2FSTRONG%3E%20for%20the%20VM%20(Static%20or%20Dynamic)%20and%20if%20the%20private%20IP%20address%20is%20Dynamic%2C%20it%20will%20change%20it%20to%20a%20Static%20private%20address%20temporarily%20to%20make%20sure%20that%20the%20VM's%20private%20address%20stays%20the%20same%20while%20it's%20in%20a%20shut%20down%20state.%20Then%20after%20the%20resize%20happens%2C%20the%20VM%20is%20powered%20back%20up%20and%20%3CU%3Eif%3C%2FU%3E%20the%26nbsp%3B%3CSTRONG%3EPrivate%26nbsp%3BIP%26nbsp%3BAllocation%26nbsp%3BMethod%3C%2FSTRONG%3E%20was%20changed%20to%20Static%20part%20of%20this%20process%2C%20then%20it%20changed%20back%20to%20Dynamic%2C%20to%20keep%20the%20VM%20the%20same%20as%20it%20was%20originally.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20full%20script%20can%20be%20found%20below%20and%20-ERR%3AREF-NOT-FOUND-here%2C%20be%20sure%20to%20read%20the%20lines%20at%20the%20top%20to%20make%20sure%20you%20gave%20the%20correct%20version%20of%20PowerShell%20v7%20and%20the%20correct%20version%20of%20the%20Az%20(Azure)%20PowerShell%20module.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%26lt%3B%23%0AWritten%20with%20PowerShell%20v7.0.1%20-%20install%20available%20here%20https%3A%2F%2Fgithub.com%2FPowerShell%2Fpowershell%2Freleases%0AWritten%20with%20version%204.2.0%20of%20the%20Az%20PowerShell%20Module%20%0Aavailable%20from%20here%20https%3A%2F%2Fgithub.com%2FAzure%2Fazure-powershell%2Freleases%2Ftag%2Fv4.2.0-June2020%20or%20run%3A%20Install-Module%20-Name%20Az%20-RequiredVersion%204.2.0%20-AllowClobber%0AMigration%20instructions%20Azure.RM%20to%20Az%20-%20https%3A%2F%2Fazure.microsoft.com%2Fen-au%2Fblog%2Fhow-to-migrate-from-azurerm-to-az-in-azure-powershell%2F%0A%23%26gt%3B%0A%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%20%20%20%20Logon%20to%20Microsoft%20Azure%20%20%20%20%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%0A%24AzContextPath%20%3D%20%22%24(%24env%3ATEMP)%5C%24(Get-Date%20-Format%20yyyyMMdd)AzEnv.json%22%0A%0A%23region%20Logon%20to%20Azure%20%7C%20%40marckean%0AConnect-AzAccount%20-Tenant%20'72f988bf-86f1-41af-91ab-2d7cd011db47'%0A%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%20Subscription%20Select%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%24subscriptions%20%3D%20Get-AzSubscription%0A%24cnt%20%3D%201%0AWrite-Host%20%22ID%20%20%20%20Subscription%20Name%22%0AWrite-Host%20%22-----------------------%22%0A%0Aforeach%20(%24sub%20in%20%24subscriptions)%7B%0A%20%20%20%20Write-Host%20%22%24cnt%20%20%20%24(%24sub.name)%22%0A%20%20%20%20%24cnt%2B%2B%0A%7D%0A%24selection%20%3D%20Read-Host%20-Prompt%20%22Select%20a%20subscription%20to%20deploy%20to%22%0A%24subSelect%20%3D%20%24subscriptions%5B%24selection%20-%201%5D%20%23%20Revert%201-index%20to%200-index%0A%0ASelect-AzSubscription%20%24subSelect.SubscriptionId%0ASave-AzContext%20-Path%20%24AzContextPath%20-Force%0A%0A%23endregion%0A%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%20%20%20%20List%20of%20VM%20sizes%20%20%20%20%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%0A%23%20Get%20a%20list%20of%20VM%20sizes%2C%20so%20you%20know%20exactly%20the%20format%20of%20the%20name%3A%0A(Get-AzVMSize%20-Location%20'Australia%20East').Name%20%7C%20Select-String%20'DS14'%0A%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%20%20%20%20List%20of%20VMs%20to%20re-size%20%20%20%20%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%23region%20List%20of%20VMs%20to%20re-size%20%7C%20%40marckean%0A%0A%23%23%23%23%23%23%23%23%23%23%23%20%20%20%20%20%20%20Only%20do%20one%20of%20two%20options%20here%20%20%20%20%20%20%20%23%23%23%23%23%23%23%23%23%23%23%0A%0A%23%20Option%201%3A%20Pull%20VMs%20from%20a%20.CSV%20file%20-%20two%20columns%2C%20headers%20(vmname%2Ctargetsize)%20%7C%20%40marckean%0A%24VMs_to_resize%20%3D%20Import-Csv%20-Path%20'W%3A%5CVMs_to_resize.csv'%0A%0A%23%20Option%202%3A%20Pull%20VMs%20from%20a%20comma%20separated%20here%20string%20-%20this%20is%20practically%20a%20mini%20CSV%20file%20anyway%20%7C%20%40marckean%0A%24VMs%20%3D%20%40'%0Avmname%2Ctargetsize%0AHadoop01%2CStandard_DS14_v2%0AHadoop02%2CStandard_DS14_v2%0A'%40%0A%24VMs_to_resize%20%3D%20%24VMs%20%7C%20ConvertFrom-Csv%0A%0A%23endregion%0A%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%20%20%20%20%20Resize%20the%20VMs%20%20%20%20%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%24%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%23%0A%0A%24VMs_to_resize%20%7C%20ForEach-Object%20-Parallel%20%7B%0A%0A%20%20%20%20Import-AzContext%20-Path%20%24using%3AAzContextPath%0A%0A%20%20%20%20%23%20Set%20private%20IP%20address%20to%20static%20to%20not%20loose%20it%0A%20%20%20%20%24NicID%20%3D%20((Get-AzVM%20-Name%20%24_.vmname).NetworkProfile.NetworkInterfaces%5B0%5D.Id)%0A%20%20%20%20%24Nic%20%3D%20Get-AzNetworkInterface%20-ResourceId%20%24NicID%0A%20%20%20%20%24PrivateIpAllocationMethod%20%3D%20%24Nic.IpConfigurations%5B0%5D.PrivateIpAllocationMethod%20%23%20Remember%20original%20Private%20IP%20Allocation%20Method%0A%20%20%20%20If(%24PrivateIpAllocationMethod%20-eq%20'Dynamic')%7B%20%23%20only%20change%20the%20IP%20addess%20to%20static%20it's%20Dynamic%0A%20%20%20%20%24Nic.IpConfigurations%5B0%5D.PrivateIpAllocationMethod%20%3D%20'Static'%0A%20%20%20%20Set-AzNetworkInterface%20-NetworkInterface%20%24Nic%7D%0A%20%20%20%20Write-Output%20%22%24(%24_.vmname)%20is%20currently%20set%20as%20%24PrivateIpAllocationMethod%22%0A%0A%20%20%20%20%23%20Stop%20deallocate%2C%20resize%20the%20VM%0A%20%20%20%20%24vm%20%3D%20Get-AzVM%20-Name%20%24_.vmname%0A%20%20%20%20%24VMsize%20%3D%20%24_.targetsize%0A%20%20%20%20Write-Output%20%22Change%20%24(%24_.vmname)%20to%20%24VMsize%22%0A%20%20%20%20%24vm%20%7C%20Stop-AzVM%20-Force%0A%20%20%20%20%24vm.HardwareProfile.VmSize%20%3D%20%24VMsize%0A%20%20%20%20Update-AzVM%20-VM%20%24vm%20-ResourceGroupName%20%24vm.ResourceGroupName%0A%20%20%20%20%24vm%20%7C%20Start-AzVM%0A%0A%20%20%20%20%23%20Changing%20the%20Private%20IP%20address%20allocation%20method%20of%20a%20running%20VM%20to%20Dynamic%20using%20PowerShell%20doesn't%20cause%20a%20restart%0A%20%20%20%20If(%24PrivateIpAllocationMethod%20-eq%20'Dynamic')%7B%20%23%20only%20change%20the%20IP%20addess%20back%20to%20Dynamic%20it%20was%20Dynamic%20in%20the%20first%20instance%0A%20%20%20%20%24Nic%20%3D%20Get-AzNetworkInterface%20-ResourceId%20((Get-AzVM%20-Name%20%24_.vmname).NetworkProfile.NetworkInterfaces%5B0%5D.Id)%0A%20%20%20%20%24Nic.IpConfigurations%5B0%5D.PrivateIpAllocationMethod%20%3D%20'Dynamic'%0A%20%20%20%20Set-AzNetworkInterface%20-NetworkInterface%20%24Nic%7D%0A%0A%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fgist.github.com%2Fmarckean%2F41c35f4160bdc1c40fb0865fbcdbe638%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgist.github.com%2Fmarckean%2F41c35f4160bdc1c40fb0865fbcdbe638%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1469747%22%20slang%3D%22en-US%22%3E%3CP%3EImagine%20you%20had%20to%20resize%20a%20whole%20bunch%20of%20VMs%20in%20Azure%2C%20rather%20than%20doing%20a%20resize%20one%20after%20the%20other%20in%20serial%2C%20you%20can%20do%20this%20in%20parallel%20with%20the%20new%20ForEach-Object%20Parallel%20feature%20in%20PowerShell%20v7%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1469747%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EInfrastructure%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Imagine you had to resize a whole bunch of VMs in Azure, rather than doing a resize one after the other in serial, you can do this in parallel with the new ForEach-Object Parallel feature in PowerShell v7. More details on this feature here - https://devblogs.microsoft.com/powershell/powershell-foreach-object-parallel-feature/

 

Doing this in parallel will greatly speed up the process by resizing any number of VMs all at once. To start with, you will need a CSV file with two columns, vmname,targetsize. Enter all the VM names of the VMs that you want to resize, also the correct VM size. 

 

Sample CSV file:

 

 

vmname,targetsize
VM01,Standard_DS14_v2
VM02,Standard_DS14_v2

 

 

To get a list of Azure VM sizes and the correct format for the name, run the following:

 

 

(Get-AzVMSize -Location 'Australia East').Name

 

 

This script will loop through all the VMs in the CSV file, shutting down the VM first before the resize happens. A full shutdown of the VM ensures that all target VM sizes are on offer across all Azure clusters, not just VM sizes of the current cluster in which your VM is a member of. First the script will get the status of the Private IP Allocation Method for the VM (Static or Dynamic) and if the private IP address is Dynamic, it will change it to a Static private address temporarily to make sure that the VM's private address stays the same while it's in a shut down state. Then after the resize happens, the VM is powered back up and if the Private IP Allocation Method was changed to Static part of this process, then it changed back to Dynamic, to keep the VM the same as it was originally. 

 

The full script can be found below and here, be sure to read the lines at the top to make sure you gave the correct version of PowerShell v7 and the correct version of the Az (Azure) PowerShell module. 

 

 

 

<#
Written with PowerShell v7.0.1 - install available here https://github.com/PowerShell/powershell/releases
Written with version 4.2.0 of the Az PowerShell Module 
available from here https://github.com/Azure/azure-powershell/releases/tag/v4.2.0-June2020 or run: Install-Module -Name Az -RequiredVersion 4.2.0 -AllowClobber
Migration instructions Azure.RM to Az - https://azure.microsoft.com/en-au/blog/how-to-migrate-from-azurerm-to-az-in-azure-powershell/
#>

##########################################################################################
############################    Logon to Microsoft Azure     #############################
##########################################################################################

$AzContextPath = "$($env:TEMP)\$(Get-Date -Format yyyyMMdd)AzEnv.json"

#region Logon to Azure | @marckean
Connect-AzAccount -Tenant '72f988bf-86f1-41af-91ab-2d7cd011db47'

##################### Subscription Select #####################
$subscriptions = Get-AzSubscription
$cnt = 1
Write-Host "ID    Subscription Name"
Write-Host "-----------------------"

foreach ($sub in $subscriptions){
    Write-Host "$cnt   $($sub.name)"
    $cnt++
}
$selection = Read-Host -Prompt "Select a subscription to deploy to"
$subSelect = $subscriptions[$selection - 1] # Revert 1-index to 0-index

Select-AzSubscription $subSelect.SubscriptionId
Save-AzContext -Path $AzContextPath -Force

#endregion

##########################################################################################
################################    List of VM sizes     #################################
##########################################################################################

# Get a list of VM sizes, so you know exactly the format of the name:
(Get-AzVMSize -Location 'Australia East').Name | Select-String 'DS14'

##########################################################################################
#############################    List of VMs to re-size     ##############################
##########################################################################################
#region List of VMs to re-size | @marckean

###########       Only do one of two options here       ###########

# Option 1: Pull VMs from a .CSV file - two columns, headers (vmname,targetsize) | @marckean
$VMs_to_resize = Import-Csv -Path 'W:\VMs_to_resize.csv'

# Option 2: Pull VMs from a comma separated here string - this is practically a mini CSV file anyway | @marckean
$VMs = @'
vmname,targetsize
Hadoop01,Standard_DS14_v2
Hadoop02,Standard_DS14_v2
'@
$VMs_to_resize = $VMs | ConvertFrom-Csv

#endregion

##########################################################################################
################################     Resize the VMs     ###################$##############
##########################################################################################

$VMs_to_resize | ForEach-Object -Parallel {

    Import-AzContext -Path $using:AzContextPath

    # Set private IP address to static to not loose it
    $NicID = ((Get-AzVM -Name $_.vmname).NetworkProfile.NetworkInterfaces[0].Id)
    $Nic = Get-AzNetworkInterface -ResourceId $NicID
    $PrivateIpAllocationMethod = $Nic.IpConfigurations[0].PrivateIpAllocationMethod # Remember original Private IP Allocation Method
    If($PrivateIpAllocationMethod -eq 'Dynamic'){ # only change the IP addess to static it's Dynamic
    $Nic.IpConfigurations[0].PrivateIpAllocationMethod = 'Static'
    Set-AzNetworkInterface -NetworkInterface $Nic}
    Write-Output "$($_.vmname) is currently set as $PrivateIpAllocationMethod"

    # Stop deallocate, resize the VM
    $vm = Get-AzVM -Name $_.vmname
    $VMsize = $_.targetsize
    Write-Output "Change $($_.vmname) to $VMsize"
    $vm | Stop-AzVM -Force
    $vm.HardwareProfile.VmSize = $VMsize
    Update-AzVM -VM $vm -ResourceGroupName $vm.ResourceGroupName
    $vm | Start-AzVM

    # Changing the Private IP address allocation method of a running VM to Dynamic using PowerShell doesn't cause a restart
    If($PrivateIpAllocationMethod -eq 'Dynamic'){ # only change the IP addess back to Dynamic it was Dynamic in the first instance
    $Nic = Get-AzNetworkInterface -ResourceId ((Get-AzVM -Name $_.vmname).NetworkProfile.NetworkInterfaces[0].Id)
    $Nic.IpConfigurations[0].PrivateIpAllocationMethod = 'Dynamic'
    Set-AzNetworkInterface -NetworkInterface $Nic}

  }

 

 

 

https://gist.github.com/marckean/41c35f4160bdc1c40fb0865fbcdbe638