Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell select statement

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?

like image 708
Alex Avatar asked Apr 30 '26 09:04

Alex


1 Answers

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.

like image 80
mklement0 Avatar answered May 02 '26 10:05

mklement0