Skip to content

Commit 4c493eb

Browse files
Merge pull request #259904 from ericd-mst-github/patch-49
Update resize-vm.md
2 parents fba3f56 + 1d655b1 commit 4c493eb

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

articles/virtual-machines/resize-vm.md

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,31 +124,45 @@ 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-
}
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
140+
```
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.
140142

141-
# Deallocate the VM
142-
az vm deallocate --resource-group $resourceGroup --name $vm
143+
> [!NOTE]
144+
> The local PowerShell may require the VM to restart to take effect.
143145
144-
# Resize the VM
145-
az vm resize --resource-group $resourceGroup --name $vm --size $size
146146

147-
# Start the VM
148-
az vm start --resource-group $resourceGroup --name $vm
147+
```powershell
148+
# Import the Azure module
149+
Import-Module Az
150+
# Login to your Azure account
151+
Connect-AzAccount
152+
# Set variables
153+
$resourceGroup = 'myResourceGroup'
154+
$vmName = 'myVM'
155+
$size = 'Standard_DS3_v2'
156+
# Select the subscription
157+
Select-AzSubscription -SubscriptionId '<subscriptionID>'
158+
# Get the VM
159+
$vm = Get-AzVM -ResourceGroupName $resourceGroup -Name $vmName
160+
# Change the VM size
161+
$vm.HardwareProfile.VmSize = $size
162+
# Update the VM
163+
Update-AzVM -ResourceGroupName $resourceGroup -VM $vm
149164
```
150165

151-
152166
> [!WARNING]
153167
> Deallocating the VM also releases any dynamic IP addresses assigned to the VM. The OS and data disks are not affected.
154168
>

0 commit comments

Comments
 (0)