I am attempting to get a list of VMSS that have a specific tag and are still powered/allocated and then deallocate those VMSS.
I have not seen a property in Get-AzVmss
that shows the allocation or power state of the VMSS.
I did however find if I dig in to the instances themselves I can get the powerstate of them using Get-AzVmssVM
I am able to successfully get this to occur at the instance level and power the instances off, but I would like to deallocate the VMSS itself.
This will be part of a DevOps deployment pipeline so I need to ensure it's reliable and consistent. It will be run as an Azure Powershell Task.
Anyone able to assist in what I am missing here? I would love to do this a layer up and not even get in to the instances, but I could not see how to do that (assuming it is possible).
Here is the code I have so far:
$RedTagValue = "Red"
$RGName = "test-rg"
$Resources = Get-AzVmss -ResourceGroupName $RGName | Where-Object { $_.Tags.Values -eq $RedTagValue }
foreach ($Resource in $Resources) {
$vmss = Get-AzVmssVM -ResourceGroupName $RGName -VMScaleSetName $Resource.Name
foreach ($vm in $vmss) {
$instances = Get-AzVmssVM -ResourceGroupName $RGName -VMScaleSetName $Resource.Name -InstanceId $vm.InstanceId -InstanceView
if ($instances.Statuses[1].Code -notcontains "PowerState/deallocated") {
Write-Output "Turning off" #Need some code here to output the VMSS that are being turned off and also some logic to turn them off
}
else {
Write-Output "No Machines to turn Off"
}
}
}