azure cli list nic attached to VM - azure

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

Related

How to get family of azure VM via VM size az cli

I am looking to get the name of the VM family of a specific VM type via the az cli.
Sample input:
Standard_D16ds_v4
Sample output:
Standard DDSv4 Family vCPUs
Is this possible to do either via az cli or azure RM?
You can use this one,
az vm show -g 'YourResourceGroupName' -n 'VMName' --query 'hardwareProfile.vmSize' -o tsv
You can use the list-skus subcommand to find this:
$ az vm list-skus --size Standard_D16ds_v4 --query [].family
[
"standardDDSv4Family"
]
https://learn.microsoft.com/en-us/cli/azure/vm?view=azure-cli-latest#az-vm-list-skus

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

Deleting resources attached to azure virtual machine using Azure CLI

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

How to query VMs in Azure powerstate using tags

I have a script that deallocates all VMs in the subscription based on the tags assigned - off hours and start them back up the next day using Jenkins. I want to be able to query these VMs based on the state (Running/Stopped(deallocated) and output it to a file.
Startup command - az vm start --ids $(az resource list --tag Restart=${TAG_RESTART} --query "[?type=='Microsoft.Compute/virtualMachines'].id" -o table)
Query command -
az resource list --tag Restart=yes --query "[].{Name:name,Group:resourceGroup,Location:location}" -o table
This command returns output (Name, RG and location). I want it to also show the powerstate and possibly OS type once the restart script is complete. If it is also possible to export the output to a spreadsheet.
You could use az vm show -d --ids to get powershell state.
Sorry, I don't have a Mac VM. On Linux VM, I use the following command to get it.
az vm show -d --ids $(az resource list --tag Restart=shui --query "[?type=='Microsoft.Compute/virtualMachines'].id"|jq -r ".[]") --query 'powerState'
On Mac, maybe you could use the following command.
az vm show -d --ids $(az resource list --tag Restart=${TAG_RESTART} --query "[?type=='Microsoft.Compute/virtualMachines'].id" -o table) --query 'powerState'
You could get help by using az vm show -h
--show-details -d : Show public ip address, FQDN, and power states. command will run slow.

Resources