无法通过管道传递cmdlet并将其传递给另一个?

时间:2020-07-23 04:13:45

标签: powershell powershell-remoting

下面的脚本用于收集Exchange Online的邮箱大小。

    $Mailboxes = @('User1.name@domain.co.uk', 'user2.name@company.com') + 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

但是,以下错误导致结果无法正确显示。

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

我不知道该如何解决?

1 个答案:

答案 0 :(得分:1)

Get-EXOMailboxFolderStatistics每个参数的文档指出每个参数的位置为Named。因此,必须使用-Parameter Value将值绑定到参数。

Get-EXOMailboxFolderStatistcs -Identity $_.UserPrincipalName

您尝试的操作取决于-identity参数的数字位置。默认情况下,函数按声明的顺序启用位置参数。您可以使用PositionalBinding属性的CmdletBinding参数来显式禁用头寸。您也可以使用Parameter属性设置位置。参见Advanced Parameters