Shiju Samuel
Shiju Samuel

Reputation: 1591

Access PowerShell pipeline properties

Is there a way to expose the pipeline properties of the previous command in the output of the next command. Below will loop through each SQL Server and get databases. The output only gives me the database name but I am looking for a way to get the Server Name also in the output.

Get-AzureSqlDatabaseServer | Get-AzureSqlDatabase

Upvotes: 2

Views: 290

Answers (2)

Martin Brandl
Martin Brandl

Reputation: 59001

You could use the Foreach-Object cmdlet for that:

Get-AzureSqlDatabaseServer | Foreach-Object { 
    $db = $_ | Get-AzureSqlDatabase 
    $db | Add-Member NoteProperty -name ServerName -value $_.ServerName
    $db
}

Upvotes: 1

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174900

You can combine Select-Object with the -PipelineVariable common parameter (pv) of a previous command:

Get-AzureSqlDatabaseServer -pv Server |Get-AzureSqlDatabase |Select-Object *,@{Label='ServerName';Expression={$Server.ServerName}}

Upvotes: 3

Related Questions