Deleting resources attached to azure virtual machine using Azure CLI - azure

I'm writing a shell script for deleting azure virtual machine and its associated resources, but having issues getting vm's network security group name/ids and vm's public ip name/ids.
I have the name of my resource group and the name of the machine itself. Moreover, I've found vm's NIC using the command:
vmNIC=$(az vm nic list --resource-group $rgName --vm-name $vmName --query [].id -o tsv);
And found vm's disks (OS and data) using the commands:
vmOSDisk=$(az vm show -d -g $rgName -n $vmName --query "storageProfile.osDisk.managedDisk.id" -o tsv);
vmDataDisks=$(az vm show -d -g $rgName -n $vmName --query "storageProfile.dataDisks[].managedDisk.id" -o tsv);
Does anyone know how can I retrieve the name/ids of my virtual machine's NSG and my virtual machine's Public IP?
Thank you for your help.

To retrieve the VM's public IP, you can use
vmNIC=$(az vm nic list --resource-group $rgName --vm-name $vmName --query [].id -o tsv)
az network nic show --ids $vmNIC --query "ipConfigurations[].publicIpAddress.id" -o tsv
To retrieve the name/ids of the virtual machine's NSG, you can use
The nic level NSG,
az network nic show --ids $vmNIC --query "networkSecurityGroup.id" -o tsv
The subnet level NSG,
subnetID=$(az network nic show --ids $vmNIC --query "ipConfigurations[].subnet.id" -o tsv)
az network vnet subnet show --ids $subnetID --query "networkSecurityGroup.id" -o tsv

Related

How to get resource name from resource Id in azure cli

I need to get the network interface card name from the resource Id.
vmsList=$(az vm list --show-details --query '[?name!=`null`].[name]' -o tsv)
for vm in ${vmsList[#]}
do
nics="$(az vm nic list --vm-name $vm --query "[].{id:id}" --output tsv)"
for nic in ${nics[#]}
do
az vm nic show --vm-name $vm --nic $nic --query '{Name:name,Location:location}' -o json
done
done
But it is giving us this error.
'/subscriptions/subscriptionName/resourceGroups/resourceGroupName/providers/Microsoft.Network/networkInterfaces/networkInterfaceCardName' not found on VM 'VMName'
ERROR: Operation returned an invalid status 'Bad Request'
I know this error is because we just need to pass only network interface name to az vm nic show command. But I am stuck to get the resource name only from resource Id.
We got the below error when we tried to get NetworkInterface Name with Resource ID
To get the NIC details of particular VM, use the below command
$virtualmachine = Get-AzVM -VMName "VMName"
$virtualmachine.NetworkProfile
$nic = $virtualmachine.NetworkProfile.NetworkInterfaces
foreach($nics in $nic) {
($nics.Id -split '/')[-1]
}
To get the NIC details of all the VM's in a ResourceGroup ,use
$virtualmachine= Get-AzVM -ResourceGroupName "YourRGName"
$virtualmachine.NetworkProfile
$nic = $virtualmachine.NetworkProfile.NetworkInterfaces
foreach($nics in $nic) {
($nics.Id -split '/')[-1]
}
References taken from :
Azure VM NIC name using PowerShell
Network interface object for an Azure VM

Azure CLI Command to display Instance Name and Resource group

I am writing a script and I would have the private IP address, is their way to fetch Instance Name and resource group from az cli
az vm list-ip-addresses --output table
VirtualMachine PublicIPAddresses PrivateIPAddresses
-------------------- ------------------- --------------------
Test1 10.0.0.4
Test2 10.0.0.3
Test3 10.0.0.2
Test4 10.0.0.1
I tried using query, something like this but no luck az vm list-ip-addresses --output json --query 'virtualMachine.name[]'
To fetch Instance Name and resource group from az CLI, you can use
az vm list-ip-addresses --query "[].virtualMachine" -o table
Or with the command, you can get the VM name, private IP address, resource group.
az vm list-ip-addresses --query "[].virtualMachine.[name,network.privateIpAddresses,resourceGroup]" -o table

How to get the value of subnet ref using azure cli based on a subnet name

I want to put the value of the subnet id of a subnet name i provide in a variable. I run the command below and it shows the id and all the other details for the subnet
az network vnet subnet show -g vnetrg01 -n subnet01 --vnet-name vnet01
So i used the below to place the ref in a variable but it doesnt work?
$subnetref = az network vnet subnet show -g vnetrg01 -n subnet01 --vnet-name vnet01 --query "[].id" -o tsv
Below worked for me to get subnet id.
SUBNET=$(az network vnet subnet list --subscription test-subscription --resource-group test-RG --vnet-name test-vnet --query "[?name=='test-subnet'].id" --output tsv)
Ok, found out the solution:
$subnetref = az network vnet subnet list --resource-group vnetrg01 --vnet-name vnet01 --query "[?name=='SUBNET01'].id"

How can i pass a variable between tasks in Azure Devops/VSTS

For Task 1 I have a CLI task which simply gets the subnet name and subnet ref as below
$subnetname1 = az network vnet subnet list --resource-group vnetrg01 --vnet-name vnet01 --query "[].name" -o tsv
$subnetref1 = az network vnet subnet list --resource-group vnetrg01 --vnet-name vnet01 --query "[].id" -o tsv
For task 2 I want to deploy an arm template which will use parameters from the pipleine variables in Azure Devops
So for example the result of $subnetref1 in Task 1 above needs to populate the pipleline variable for subnetref (which is setup as a variable in the pipleine) which will then be passed to the arm template override parameters
cant seem to get this working
You can do it with Powershell command,
In the first PowerShell task set the variable as environment variable:
$subnetname1 = az network vnet subnet list --resource-group vnetrg01 --vnet-name vnet01 --query "[].name" -o tsv
Write-Host $subnetname1
Write-Host ("##vso[task.setvariable variable=subnetname1;]$subnetname1")
In the second task read the variable in this way:
$subnetname1 = $env:subnetname1
Write-Host $subnetname1

azure cli list nic attached to VM

I am working on bash shell. I need az cli or unix script to find out NIC name attached to particular VM. I know VM name and VM Resource Group Name and my Target is to findout out which NIC is attached to this VM and which resouce group this NIC belongs to?
Please follow this line of azure cli code:
Step 1: Define a variable, like a. Note that there is no whiteSpace around the chars = :
a="$(az vm nic list --resource-group "your_resource_group" --vm-name "your_vm_name" --query "[].{id:id}" --output tsv)"
Step 2: Just get the nic name and it's resource group:
az vm nic show -g "your_resource_group" --vm-name "your_vm_name" --nic $a --query "{name:name,resourceGroup:resourceGroup}" --output table
Step 3: If you want get all the information of nic, please use the code below:
az vm nic show -g "your_resource_group" --vm-name "your_vm_name" --nic $a
az vm nic list --resource-group
--vm-name
[--subscription]
This will list all nics on a vm.
eg. az vm nic list -g MyResourceGroup --vm-name MyVm

Resources