How to get resource name from resource Id in azure cli - azure

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

Related

Why Get-AzPublicIPAddress works with name of resource but dont work with parameter

In my script i'm using the Get-AzPublicIpAddress cmdlet, and it's working perfectly with a VM name:
(Get-AzPublicIpAddress -ResourceName "vm-ubuntu-test2311").IpAddress
But it doesn't work with a variable as the parameter argument:
$vmname = "vm-ubuntu-test2311"
(Get-AzPublicIpAddress -ResourceName $vmname ).IpAddress
It passes but the value is empty
The way to access and get the VM public IP you can follow the below way:
Here, I am using Resource Group Name and VM name to fetch the exact VM Resource Public IP to Avoid the conflict.
$name = "<Your VM Name>"
$rname = "<Your Resource Group Name>"
Get-AzPublicIpAddress -ResourceGroupName $rname -Name $name | Select-Object {$_.IpAddress}
Results:

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

Convert Azure linux OS disk from normal disk (hdd) to standard ssd disk (preview mode)

Azure has recently introduced a new type of disks as standard SSD disk (different than the premium SSD). I was wondering if it is possible to change the OS disk type from standard HDD disk to standard SSD disk?
Can I use the same instruction as this to convert os disk to standard SSD?
I believe the question was more related to Standard HDD to Standard SSD.
$diskName = 'yourDiskName'
# resource group that contains the managed disk
$rgName = 'yourResourceGroupName'
# Choose between Standard_LRS and StandardSSD_LRS based on your scenario
$storageType = 'StandardSSD_LRS'
$disk = Get-AzureRmDisk -DiskName $diskName -ResourceGroupName $rgName
# Get parent VM resource
$vmResource = Get-AzureRmResource -ResourceId $disk.ManagedBy
# Stop and deallocate the VM before changing the storage type
Stop-AzureRmVM -ResourceGroupName $vmResource.ResourceGroupName -Name $vmResource.Name -Force
$vm = Get-AzureRmVM $vmResource.ResourceGroupName -Name $vmResource.ResourceName
# Update the storage type
$diskUpdateConfig = New-AzureRmDiskUpdateConfig -AccountType $storageType -DiskSizeGB $disk.DiskSizeGB
Update-AzureRmDisk -DiskUpdate $diskUpdateConfig -ResourceGroupName $rgName `
-DiskName $disk.Name
Start-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name
Key line in the cmdlets is setting the storage account type.
$storageType = 'StandardSSD_LRS'
Article Link: https://learn.microsoft.com/en-us/azure/virtual-machines/windows/convert-disk-storage#convert-a-managed-disk-from-standard-hdd-to-standard-ssd-and-vice-versa
The following example shows how to switch all the disks of a VM from standard to premium storage. To use premium managed disks, your VM must use a VM size that supports premium storage. This example also switches to a size that supports premium storage.
#resource group that contains the virtual machine
rgName='yourResourceGroup'
#Name of the virtual machine
vmName='yourVM'
#Premium capable size
#Required only if converting from standard to premium
size='Standard_DS2_v2'
#Choose between Standard_LRS and Premium_LRS based on your scenario
sku='Premium_LRS'
#Deallocate the VM before changing the size of the VM
az vm deallocate --name $vmName --resource-group $rgName
#Change the VM size to a size that supports premium storage
#Skip this step if converting storage from premium to standard
az vm resize --resource-group $rgName --name $vmName --size $size
#Update the sku of all the data disks
az vm show -n $vmName -g $rgName --query storageProfile.dataDisks[*].managedDisk -o tsv \
| awk -v sku=$sku '{system("az disk update --sku "sku" --ids "$1)}'
#Update the sku of the OS disk
az vm show -n $vmName -g $rgName --query storageProfile.osDisk.managedDisk -o tsv \
| awk -v sku=$sku '{system("az disk update --sku "sku" --ids "$1)}'
az vm start --name $vmName --resource-group $rgName
For more details, refer "Convert Azure managed disks storage from standard to premium, and vice versa".

How to get subscription id from the VM in AZURE

Is there a way to get the subscription id from the running (LINUX)VM instance in AZURE?
Can WALinuxAgent read the subscription ID from the internal server ?
This can be achieved using the Azure Instance Metadata Service. Calling this service from your VM will return a JSON with SubscriptionId among other useful data. Sample Microsoft bash script for calling the metadata service (with updated version in the request):
sudo apt-get install curl
sudo apt-get install jq
curl -H Metadata:True "http://169.254.169.254/metadata/instance?api-version=2017-08-01&format=json" | jq .
See "Response" section in provided link for sample response, with subscriptionId.
You can use powershell to achieve this.
First of all.
What kind of VM deployment model?
ARM
In this case it very simple.
$vm = Get-AzureRmVM -ResourceGroupName $resourceGroupName -Name $vmName
$vm.Id
You'll see - "/subscriptions/{subscriptionId}/..."
Classic
If you know resource group VM was deployed to, use following:
$resource = Get-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.ClassicCompute/virtualMachines -Name $vmName
$resource.ResourceId
Same - you"ll see "/subscriptions/{subscriptionId}/..."
Way to find resourceGroupName, if unknown (in case you write some automative script):
$vm = Get-AzureVM | Where {$_.Name -eq $vmName}
$service = Get-AzureService -ServiceName $vm.ServiceName
$service.ExtendedProperties.ResourceGroup
Hope it helps

Resources