Skip to content

Commit a1bc4f3

Browse files
Update resize-vm.md
updating cloud shell and adding local PS sample for resizing VM
1 parent f2b82a0 commit a1bc4f3

1 file changed

Lines changed: 29 additions & 19 deletions

File tree

articles/virtual-machines/resize-vm.md

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -124,30 +124,40 @@ az vm resize \
124124

125125
**Use PowerShell to resize a VM not in an availability set.**
126126

127-
This script sets the variables `$resourceGroup`, `$vm`, and `$size`. It then checks if the desired VM size is available by using `az vm list-vm-resize-options` and checking if the output contains the desired size. If the desired size isn't available, the script exits with an error message. If the desired size is available, the script deallocates the VM, resizes it, and starts it again.
127+
This Cloud shell PowerShell script initializes the variables `$resourceGroup`, `$vm`, and `$size` with the resource group name, VM name, and desired VM size respectively. It then retrieves the VM object from Azure using the `Get-AzVM` cmdlet. The script modifies the `VmSize` property of the VM's hardware profile to the desired size. Finally, it applies these changes to the VM in Azure using the `Update-AzVM` cmdlet.
128128

129129
```azurepowershell-interactive
130130
# Set variables
131-
$resourceGroup = "myResourceGroup"
132-
$vm = "myVM"
133-
$size = "Standard_DS3_v2"
134-
135-
# Check if the desired VM size is available
136-
if ((az vm list-vm-resize-options --resource-group $resourceGroup --name $vm --query "[].name" | ConvertFrom-Json) -notcontains $size) {
137-
Write-Host "The desired VM size is not available."
138-
exit 1
139-
}
140-
141-
# Deallocate the VM
142-
az vm deallocate --resource-group $resourceGroup --name $vm
143-
144-
# Resize the VM
145-
az vm resize --resource-group $resourceGroup --name $vm --size $size
146-
147-
# Start the VM
148-
az vm start --resource-group $resourceGroup --name $vm
131+
$resourceGroup = 'myResourceGroup'
132+
$vm = 'myVM'
133+
$size = 'Standard_DS3_v2'
134+
# Get the VM
135+
$vm = Get-AzVM -ResourceGroupName $resourceGroup -Name $vmName
136+
# Change the VM size
137+
$vm.HardwareProfile.VmSize = $size
138+
# Update the VM
139+
Update-AzVM -ResourceGroupName $resourceGroup -VM $vm
149140
```
141+
As an alternative to running the script in Azure Cloud Shell, you can also execute it locally on your machine. This local version of the PowerShell script includes additional steps to import the Azure module and authenticate your Azure account.
150142

143+
```powershell
144+
# Import the Azure module
145+
Import-Module Az
146+
# Login to your Azure account
147+
Connect-AzAccount
148+
# Set variables
149+
$resourceGroup = 'myResourceGroup'
150+
$vmName = 'myVM'
151+
$size = 'Standard_DS3_v2'
152+
# Select the subscription
153+
Select-AzSubscription -SubscriptionId '<subscriptionID>'
154+
# Get the VM
155+
$vm = Get-AzVM -ResourceGroupName $resourceGroup -Name $vmName
156+
# Change the VM size
157+
$vm.HardwareProfile.VmSize = $size
158+
# Update the VM
159+
Update-AzVM -ResourceGroupName $resourceGroup -VM $vm
160+
```
151161

152162
> [!WARNING]
153163
> Deallocating the VM also releases any dynamic IP addresses assigned to the VM. The OS and data disks are not affected.

0 commit comments

Comments
 (0)