3
forEach($subscription in Get-AzureRmSubscription) {
Select-AzureRmSubscription -SubscriptionId $subscription.Id | Out-Null
Get-AzurermVM -Status | select $subscription.Name, ResourceGroupName, Name, 
PowerState | ConvertTo-Csv -NoTypeInformation -Delimiter "|"}

I'm trying to return the results of this query, but it does not include the subscription name in the results. It shows it in the table header, but not in each line of the results. It should show it on every line, but it is blank for the first column on each row. I'm guessing this has to do with the subscription variable not being apart of the -Status properties, but it shows it in the table header?

1 Answer 1

3

If you want to include a fixed value as a property of the custom objects output by the
Select-Object cmdlet (select is a built-in alias), you must use a calculated property:

... | Select-Object @{ n='SubscriptionName'; e={ $subscription.Name } }, 
                    ResourceGroupName, 
                    Name, 
                    PowerState | ...

By contrast, using $subscription.Name by itself is interpreted as the name of a property to access on each input object received.

Sign up to request clarification or add additional context in comments.

1 Comment

@Alex: My pleasure; glad to hear it was helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.