3

I am trying to figure out how to get a value from one command as a parameter to another command, and use the output of both in a single table. Specifically I am using two cmdlets, Get-Mailbox and Get-MailboxStatistics (this is against an Exchange 2010 server).

From Get-Mailbox I need the DisplayName, UseDatabaseQuotaDefaults and Database fields. Added to this, I need to get from Get-MailboxStatistics the TotalItemSize and StorageLimitStatus fields.

I can run each of these commands individually, but cannot figure out how to use the DisplayName value from Get-Mailbox fed into the Identity value for Get-MailboxStatistics command and then output the whole lot into a single table.

I was trying something along these lines:

get-mailbox | ForEach-Object {write-host $_.DisplayName, $_.UseDatabaseQuotaDefaults, $_.Database, Get-MailboxStatistics $_.SamAccountName}

Instead of actually processing the Get-MailboxStatistics as a command it just display it as text. How can I get PS to treat this as a command and not text for the write-host cmdlet?

zdan
  • 28,667
  • 7
  • 60
  • 71
Peter Bako
  • 93
  • 2
  • 7

3 Answers3

3

You need to use parentheses, something along these lines:

get-mailbox | ForEach-Object { Write-Host ` 
    $_.DisplayName, `
    $_.UseDatabaseQuotaDefaults, `
    $_.Database, `
    (Get-MailboxStatistics $.SamAccountName) }
#   ^------- note the parentheses ---------^
Igor Korkhov
  • 8,283
  • 1
  • 26
  • 31
  • I wish that would work, as it appears to make sense and I can understand it. However PS just gives me this error: Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently. + CategoryInfo : OperationStopped: (Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [], PSInvalidOperationException + FullyQualifiedErrorId : RemotePipelineExecutionFailed – Peter Bako Apr 11 '13 at 16:24
1

Using bits of info from the previous answers, combined with some Google searching and more than my fair share of cursing, came up with the following script that actually works:

$mbx = Get-Mailbox

ForEach ($cur in $mbx)
{
  $stat = (Get-MailboxStatistics $cur.DisplayName)

  New-Object PSObject -Property @{
    DisplayName = $cur.DisplayName
    UseDatabaseQuotaDefaults = $cur.UseDatabaseQuotaDefaults
    SamAccountName = $cur.SamAccountName
    StorageLimitStatus = $stat.StorageLimitStatus
    TotalItemSize = $stat.TotalItemSize
    Database = $stat.Database
  }

}

thanks everyone!

Peter Bako
  • 93
  • 2
  • 7
0

Get a list of all mailboxes, for each one assign its statistics to a variable, then create a custom object with properties from both objects:

Get-Mailbox | ForEach-Object {

    $stats = $_ | Get-MailboxStatistics

    New-Object PSObject -Property @{
        DisplayName = $_.DisplayName
        UseDatabaseQuotaDefaults = $_.UseDatabaseQuotaDefaults
        Database = $_.Database
        SamAccountName = $_.SamAccountName
        TotalItemSize  = $stats.TotalItemSize
        StorageLimitStatus = $stats.StorageLimitStatus
    }
}
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Ok, I put this into a script and tried to run it. The entries from Get-Mailbox are showing up just fine, but others are blank and PS gives me a pipeline error: Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently. + CategoryInfo : OperationStopped: (Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [], PSInvalidOperationException + FullyQualifiedErrorId : RemotePipelineExecutionFailed – Peter Bako Apr 11 '13 at 16:32