Azure CLI Command to display Instance Name and Resource group - azure

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

Related

How to get Private Ips of Azure Vm's

#!/bin/bash
echo "Enter the names of the resource groups separated by spaces: "
read -a resource_groups
# Loop through each resource group and retrieve information about its Azure VMs
for resource_group in "${resource_groups[#]}"; do
echo "Getting information for resource group: $resource_group"
vm_info=$(az vm list --resource-group $resource_group --query "[].{Name: name, ResourceGroup: resourceGroup, ID: id}" -o table)
echo "Here is the information about the Azure VMs in resource group '$resource_group':"
echo "$vm_info"
echo ""
done
I need private ips of azure virtual machines but this bash script not fetching
use this:
az vm list -d --query "[].{ResourceGroup:resourceGroup, Name:name, PrivateIP:privateIps}" -o table
to filter by resource group:
az vm list -d --resource-group $resource_group --query "[].{ResourceGroup:resourceGroup, Name:name, PrivateIP:privateIps}" -o table

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

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

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

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