简体   繁体   中英

Showing output of piped command in PowerShell

I am automating Office365 mailboxes. I am running the following command in PowerShell

Get-Mailbox -Filter {Name -like "T1-*"} | Get-MailboxStatistics | 
select Displayname, LastLogonTime | Export-Csv "lastlogon-all.csv"

The command works, after initiating a session. The Get-Mailbox results also have a PrimarySmtpAddress field. I would like to include that in the ouput, but because of piping, it is lost.

How do I get that field in my output?

Get-Mailbox -Filter {Name -like "T1-*"} |
  foreach {
    $address = $_.PrimarySmtpAddress;
    $_ | Get-MailboxStatistics |
    select Displayname, LastLogonTime, @{ Name = "PrimarySmtpAddress"; Expression = { $address } }
  } |
  Export-Csv "lastlogon-all.csv"

There might also be possible to access the mailbox-object trough the statistics-object, but the documentation does not mention which objects are returned by the respective commands.

Thanks to Markus Jarderot I got the solution:

$boxes = Get-Mailbox -Filter {Name -like "T1-*"} 
$boxes |  foreach {
    $address = $_.PrimarySmtpAddress; 
    $_  | Get-MailboxStatistics | 
        select Displayname, LastLogonTime, @{ Name = "PrimarySmtpAddress"; Expression = { $address } }
  } | 
  Export-Csv "lastlogon-all.csv"

Possibly the address doesn't have to be formatted, so you could suffice with select $address

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM