PowerShell using object after other pipes

321 Views Asked by At

Firstly, I've dabbled with PowerShell but wouldn't call myself fluent in anything more than simple commands.

What I am trying to achieve is to use an object from the first cmdlet where there are a few pipes in this command.

This is the command, I got this from View Mailbox Sizes and Mailbox Quotas Using Windows PowerShell, it works.

Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | where {$_.StorageLimitStatus -notlike "BelowLimit*"} | Select DisplayName,StorageLimitStatus,@{name="TotalItemSize (MB)";expression={[math]::Round((($_.TotalItemSize.Value.ToString()).Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}},@{name="TotalDeletedItemSize (MB)";expression={[math]::Round((($_.TotalDeletedItemSize.Value.ToString()).Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}},ItemCount,DeletedItemCount | Sort "TotalItemSize (MB)" -Descending | Export-CSV "C:\My Documents\Exceeded Quotas.csv" -NoTypeInformation

I am wanting to add to this the set quota's for the mailboxes that are returned, the objects are named ProhibitSendQuota, ProhibitReceiveQuota, and IssueWarningQuota. When I add these object to the select pipe the name comes out but not the value. I am able to get the values if I was to use the command

Get-Mailbox | Select ProhibitSendQuota,ProhibitReceiveQuota,IssueWarningQuota

When I use the command

Get-Mailbox -Identity domain\user | Select Identity,ProhibitSentQuota | Get-MailboxStatistics | Select *

None of the objects from the Get-Mailbox cmdlet are shown, only the objects from the Get-MailboxStatistics are present.

My question is then, how can I use an object that exists in the Get-Mailbox cmdlet after all the other pipes and cmdlets have been added?

1

There are 1 best solutions below

1
On

Use variables. PowerShell is a programming language just like any other. Don't get hung up on using one-liners for everything because you don't have to. Also, doing Select * at the end of your CmdLet is the same thing as not having it but just converts the return type from what the CmdLet makes it to a PSCustomObject.

$MailBox = Get-Mailbox -Identity "domain\user"
$MailBox | Select-Object Identity, ProhibitSentQuota | Get-MailboxStatistics

If you really insisted on keeping the variable within the context of the pipeline, use PipelineVariable.

Get-MailBox -Identity "domain\user" -PipelineVariable MailBox #Now mailbox is available for use further in the pipeline.

Further information about PipelineVariable