【问题标题】:Unable to pipe and pass through cmdlets to another?无法通过管道将 cmdlet 传递给另一个?
【发布时间】:2020-07-23 04:13:45
【问题描述】:

以下脚本旨在收集 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

我想知道如何解决这个问题?

【问题讨论】:

  • 调用命令时使用-identity参数。该文档没有提到任何参数都按位置编号接受值(您尝试的位置为 0)

标签: powershell powershell-remoting


【解决方案1】:

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

Get-EXOMailboxFolderStatistcs -Identity $_.UserPrincipalName

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

【讨论】:

  • 嗨@admin,错误还是一样。代码有效,直到: $Mailboxes |获取-EXO邮箱 | Select-Object -ExpandProperty Name 我已经更新了代码,目前没有报错,但是控制台只显示Processing User DisplayName only?
  • 您的第一个 where-object 使用 () 而不是 {}
  • 是的,这是一个很好的发现 :-) 但是,问题仍然存在吗?。
  • $mailboxes 中,您将字符串与同一数组中的邮箱对象混合在一起。那不是最佳做法。也许您应该考虑为您的get-exomailbox 命令选择userprincipalname
  • 应该是e={$_.Identity -replace '\\Top of Information Store'}}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多