Resize & Delete Virtual machines (VM) on Azure vendor how to execute a script in Linux

This tutorial shows steps to write and execute a script in Linux using the Terminal app.

I/ Steps resize VM by executing a script in Linux

The procedure is as follows:

1.Open the terminal app by clicking on SSH button in Connect column for each VM on page detail service VM Underutilized

Firstly, we can install azure cli at https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-linux?pivots=apt

Then, we can login to azure by command line

az login

Then, we run command line as below when we have more subscriptions

az account set --subscription <subscriptionId>

<bổ sung ảnh khi làm xong FE>

2. With OS compatibility of Instance is Linux or Suse Linux or Red hat, you create a file .sh the following code:

#!/bin/bash
diskName=$1

resourceGroup=$2

vmSize=$3

az disk update --name $diskName \

--resource-group $resourceGroup \

 --size-gb $vmSize 

3. Execute the command to support resize t using the following code:

nano <file_name>.sh

With: < file_name >: File name created in step 2

Then, we copy code in step 2 to the sh file

4. Execute resize instance with command:

sh resize_vm_azure.sh %diskName %resourceGroup %vmSize

With:

diskName: param that is the name of the disk on the VM

resourceGroup: param that is the resource contains VM

vmSize: param that is the size we want to update for VM

E.g: You can run the script to update VM as below

sh resize_vm_azure.sh vm-resize-23_disk1_0668217cb23b46eeae2df3e07a49cf13 rg-vm-resize-23 100

Then, we can check size of the VM

II/ Steps delete VM by executing a script in Linux

The procedure is as follows:

1.You create a file .sh the following code:

#!/bin/bash
resourceGroup=$1

vmName=$2

az vm delete --resource-group $resourceGroup --name $vmName --yes

2. Run the script with the format:

nano <file_name>.sh (COPY CODE IN STEP 1)

then, press Enter

Run command delete VM with format:

sh <file_name>.sh %resourceGroup %vmName

With:

file_name: File created in step 1

resourceGroup: param that is the resource contains VM

vmSize: param that is the size we want to update for our VM

For example:

sh delete_vm_linux_window_azure.sh rg-vm-resize-23 vm-resize-23

Note: If we have multi-subscription, we should set the default subscription by command line as below

az account set --subscription <subscriptionId>

Leave a Reply