Reputation: 763
I wonder how I can filter by the 'State' of the container group from the command line (Get-AzContainerGroup
or az container list
).
In azure portal this field is reported as 'Status'.
But I can't get it from the command line, it seems this field is not provisioned.
Get-AzContainerGroup | fl
ResourceGroupName : rg-foo
Id : /subscriptions/foo/resourceGroups/foo/providers/Microsoft.ContainerInstance/containerGroups/test-01
Name : test-01
Type : Microsoft.ContainerInstance/containerGroups
Location : westeurope
Tags : {}
ProvisioningState : Succeeded
Containers : {test-01}
ImageRegistryCredentials : {}
RestartPolicy : OnFailure
IpAddress : 20.82.63.136
DnsNameLabel :
Fqdn :
Ports : {80}
OsType : Linux
Volumes : {}
State :
Events : {}
Identity :
I've tried :
Get-AzContainerGroup | Where-Object {$_.State -eq "Succeeded"}
But as field seems was reported, it didn't work.
Upvotes: 0
Views: 1274
Reputation: 5512
So, the Status column/property that you see on the Azure Portal actually translates to properties.instanceView.state
for an Azure Container Instance.
It seems like this property isn't populated although present in the ouput of Get-AzContainerGroup | fl *
. However, when the ResourceGroupName
and Name
parameters are passed along, you'd see it shows up!
Digging a little deeper, this is because the Get-AzContainerGroup
cmdlet invokes the Container Groups - List REST API under the hood, that does not have the instanceView
property in the response.
Whereas, Get-AzContainerGroup -ResourceGroupName <Resource-Group-Name> -Name <ContainerGroup-Name>
calls the Container Groups - Get REST API that returns all the extended properties as well.
So, to work around this behavior, you can run the following snippet:
# List all container groups in the current subscription
# Equivalent to [Container Groups - List]
$AllContainerGroups = Get-AzContainerGroup
# Initialize an empty array to hold all container group objects with their extended properties
$AllContainerGroupsExtended = @()
foreach($ContainerGroup in $AllContainerGroups)
{
# Gets a specific container group
# Equivalent to [Container Groups - Get]
$ContainerGroupExtended = Get-AzContainerGroup -ResourceGroupName $ContainerGroup.Id.Split('/')[4] -Name $ContainerGroup.Name
$AllContainerGroupsExtended += $ContainerGroupExtended
}
# You can now filter the result as needed
$AllContainerGroupsExtended | Where-Object {$_.InstanceViewState -eq "Stopped"}
Upvotes: 1