Ref.Ref
Ref.Ref

Reputation: 43

Get value from previous Cmdlet in the pipeline

I got the following command: Get-Mailbox | Get-MailboxPermission | Select-Object Identity,User,AccessRights | Format-Table -AutoSize. I want to be able to get the PrimarySMTPAddress value from the previous pipe where I got the results for the Get-Mailbox. At the moment when I add the property PrimarySMTPAddress I receive nothing in the column.

The final result should like this:

Identity                       User     AccessRights     PrimarySMTPAddress
--------                       ------   ------------     ------------------
Domain.local/Users/Mailbox1    User1    {FullAccess}     [email protected]
Domain.local/Users/Mailbox2    User2    {FullAccess}     [email protected]
Domain.local/Users/Mailbox3    User3    {FullAccess}     [email protected]

Upvotes: 4

Views: 6325

Answers (2)

js2010
js2010

Reputation: 27606

There's a common parameter "pipelinevariable" for that purpose.

Get-Mailbox -pipelinevariable mail | Get-MailboxPermission | 
  Select-Object Identity,User,AccessRights,
  @{n='PrimarySMTPAddress';e={$mail.PrimarySMTPAddress}} | 
  Format-Table -AutoSize

Upvotes: 4

dugas
dugas

Reputation: 12493

You can use the ForEach-Object cmdlet, assign the value to a variable, then use Write-Output to send the pipeline value to the next cmdlet. When you use Select-Object, you can access the variable value with a calculated property.

Get-Mailbox | 
ForEach-Object { $primarySmtpAddress = $_.PrimarySMTPAddress; Write-Output $_; |
    Get-MailboxPermission | 
    Select-Object Identity,User,AccessRights, @{n='PrimarySMTPAddress';e={$primarySmtpAddress}}} | 
Format-Table -AutoSize

Upvotes: 8

Related Questions