Search code examples
powershellpowershell-remoting

Unable to pipe and pass through cmdlets to another?


The script below was meant to be working to gather the mailbox size of Exchange Online.

    $Mailboxes = @('[email protected]', '[email protected]') + GetEXOMailbox | Where-Object {$_.Name -like '*User*'}
        
    $folderAndSubfolderSize = @{n='FolderAndSubfolderSize'; e={[math]::Round(($_.FolderAndSubfolderSize.Replace(',', '') -replace '.*?\((\d+)\s+bytes\)', '$1') / 1MB, 1)}}
$DisplayName = @{n='Display Name'; e='Identity'}

$Mailboxes | Get-EXOMailbox | Select-Object -ExpandProperty Name | ForEach-Object {
    Write-Host "Processing $_ ..." -ForegroundColor Yellow
   Get-EXOMailboxFolderStatistics -Identity $_ |
      Where-Object {$_.Name -like "Top of Information Store"} |
      Select-Object -Property $DisplayName, $folderAndSubfolderSize, ItemsInFolderAndSubfolders |
      Where-Object {$_.FolderAndSubfolderSize -gt 1 }
} | Out-GridView

However, the error below is stopping the result to be displayed properly.

Get-EXOMailboxFolderStatistics : Identity is a mandatory value to provide for running Get-ExoMailboxFolderStatistics. You can specify identity by using either of the following
Identity, ExternalDirectoryObjectId, UserPrincipalName.
At line:6 char:2
+     Get-EXOMailboxFolderStatistics $_.Name |
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ProtocolError: (:) [Get-EXOMailboxFolderStatistics], RestClientException
    + FullyQualifiedErrorId : Identity is a mandatory value to provide for running Get-ExoMailboxFolderStatistics. You can specify identity by using either of the following
Identity, ExternalDirectoryObjectId, UserPrincipalName.
,Microsoft.Exchange.Management.RestApiClient.GetExoMailboxFolderStatistics

I wonder how can that be fixed?


Solution

  • The documentation for each parameter of Get-EXOMailboxFolderStatistics states that the position of each parameter is Named. Therefore you must use -Parameter Value to bind a value to a parameter.

    Get-EXOMailboxFolderStatistcs -Identity $_.UserPrincipalName
    

    What you tried relies on a numeric position for the -identity parameter. By default, functions enable positional parameters in the order they are declared. You can explicitly disable positions using PositionalBinding argument of the CmdletBinding attribute. You can also set positions using the Parameter attribute. See Advanced Parameters.