I want to start the VM that have specific tag.
For example if my VM having the tag as Dept:Finance then by using azure cli command az vm start
I should able to start the vms which are having the above tag.
I found one CLI command that will give the list of vms that was having the same tag like below
az resource list --tag Dept=Finance
By using the above command how I can start the above tag associated VMs
Please help me with the Azure CLI command to start the vms that are having the same tag.
CodePudding user response:
Using PowerShell:
$vms = az resource list --tag Dept=Finance --resource-type=Microsoft.Compute/virtualMachines | convertfrom-json
foreach($vm in $vms)
{
az vm start -g $vm.resourceGroup -n $vm.name
}
CodePudding user response:
I have reproduced in my environment and got expected results:
In Azure CLI:
Here i have taken dept and finance
as tag when creating Vm and I followed Microsoft-Document :
$x=az graph query -q "Resources | where type == 'microsoft.compute/virtualmachines' | where tags contains 'finance'| project name, resourceGroup" |ConvertFrom-Json
foreach($emo in $x)
{
az vm start -g $emo.data.resourceGroup -n $emo.data.name
}
Output:
In Azure PowerShell:
Alternatively you can do in PowerShell to get vms started as below:
Here i have taken dept and finance
as tag when creating Vm and I followed Microsoft-Document :
Use below script to start Vms with similar tag:
$x=Get-AzVM -Name * | where {($_.Tags.dept -eq "finance")}
foreach($emo in $x)
{
Start-AzVM -ResourceGroupName $emo.ResourceGroupName -Name $emo.Name
}
Output: