【问题标题】:PSCustomObject out-gridview incorrect formatingPSCustomObject out-gridview 格式不正确
【发布时间】:2020-08-13 21:27:31
【问题描述】:

我在将一个属性的输出作为逗号分隔值而不是 out-gridview 中的列表时遇到问题。有没有办法将值作为列表而不是单行添加到输出中?

.'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
Connect-ExchangeServer -Auto -AllowClobber

do {
Write-Host
Write-Host
Write-Host
$name = Read-Host "What is the user's first name or letter?"
Write-Host
Write-Host
Write-Host
$list = Get-ADUser -Filter * | ? {$_.SamAccountName -match $name} |
 select @{N="Highlight a User & Press Ctrl+C then Ctrl+V"; E={$_.SamAccountName}} |
 sort SamAccountName |
 Out-String
Write-Host -ForegroundColor Green $list

$box = Read-Host "Copy and paste the mailbox you want to see?"
$user = $box


$mailbox= Get-Mailbox -Identity $user  | Get-MailboxStatistics |
    Sort totalitemsize -desc | 
    select @{Name="User"; Expression={$_.DisplayName}},
    @{Expression={"{0:N2}" -f($_.TotalItemSize.Value.ToMb()/1024)};label=”Mailbox Size in GB”},
    @{Expression={"{0:N0}" -f($_.TotalItemSize.Value.ToMb())};label=”Mailbox Size in MB”},
    @{Name="Message Count"; Expression={"{0:N0}" -f($_.itemcount)}},
    @{Name="Database"; Expression={$_.DatabaseName}}


$folders= Get-MailboxFolderStatistics $user |
    ? {$_.ItemsInFolder -gt 0} |
    Sort ItemsInFolder -Descending |
    Select Name,
    @{N="Items in Folder"; E={"{0:N0}" -f($_.ItemsInFolder)}},
    @{N=”Folder Size in MB”;E={"{0:N0}" -f($_.FolderSize.ToMb())}}

 
$object= [PSCustomObject]@{
    
    User =                   $mailbox.'User'
    'Mailbox Size in MB'=    $mailbox.'Mailbox Size in MB'
    'Message Count' =        $mailbox.'Message Count'
    Database =               $mailbox.Database
    Name =                   $folders.Name 

}

$object | Out-GridView

   
Write-Host
Write-Host
Write-Host
 $runagain = Read-Host "Would you like to get another user's folder size?" | Out-String
Write-Host
Write-Host  

    }


while($runagain -like "y*")

让 $folders.Name 在同一个 out-Gridview 中显示为列表的任何帮助都会很棒。

谢谢

【问题讨论】:

  • 您的意思是改用$object.Name | Out-GridView 吗?
  • 不,我希望在新列中添加文件夹名称以及将来每个文件夹列中的数量,以便在一个输出中。

标签: powershell pscustomobject


【解决方案1】:

您可能正在寻找:

Name =                   $($folders.Name -join [Environment]::Newline)

这样您不再使用对象,而是通过将元素与新行连接来手动创建列表。

【讨论】:

  • 顺便说一句(我没有读过这个问题):在你的哈希表条目定义中不需要$(...)。由于this bug 已在 PowerShell Core 中修复,因此仅在 Windows PowerShell情景 需要此上下文中的$(...)
  • 谢谢,您现在确实创建了列表,但仅作为输出中的一个单元格。我希望新的 Name 列对每个值也有一个单独的行。
猜你喜欢
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
  • 2016-11-03
  • 2014-05-10
  • 1970-01-01
  • 2019-09-26
相关资源
最近更新 更多