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
select name
gives you an object with propertyname
. If you want the string VALUE of that property, useSelect-Object -ExpandProperty name
– Theo