1
votes

I am trying to write a PowerShell script that will look for shutdown VMs in my Resource Group and deallocate them. The output of the below script does not give me the VM name "clean" when I attempt tp assign the below as a variable. The end result is to execute the Stop-AzureRmVM -ResourceGroupName LAB -Name $VM -force

So for more context, lets say AVGJOE-DC1 is in a stopped state and I run the below line in Azure Powershell it will display

Name
----
AVGJOE-DC1

If I then if I tried to use $VM to call AVGJOE-DC1 in the

Stop-AzureRmVM -ResourceGroupName LAB -Name $VM -force 

it fails due to the variable being set to a longer string something like MicroSoftComputerResource\Resourcegroup[@Name=AVGJOE-DC1].
Hopefully that makes sense.

  $VM = Get-AzureRmVM -ResourceGroupName LAB | get-azurermvm -Status | ?{$_.statuses.displaystatus -eq "VM stopped"} | select name 
1
Can you clarify, and give examples of what you mean that it doesn't give you the VM name "clean"?HAL9256
Added a little more context hopefully that helps.zGeekDiver
select name gives you an object with property name. If you want the string VALUE of that property, use Select-Object -ExpandProperty nameTheo

1 Answers

1
votes

Just like @Theo said in the comment, select name gives you an object with property name. If you want the string value of the name property, you can use Select-Object -ExpandProperty name instead of select name.

enter image description here