0

我想创建一个完整的邮箱权限报告并将其导出到 PowerShell 中的 CSV。

$report = @()
$mailboxes = Get-Mailbox -ResultSize unlimited -RecipientTypeDetails sharedmailbox

foreach ($mailbox in $mailboxes)
{ 
    $permissions = Get-Mailbox $mailbox.PrimarySmtpAddress | Get-MailboxPermission | Select-Object user, accessrights
    $users = $permissions.user
    $accessRights = $permissions.accessrights
    $sendAsPermissions = Get-Mailbox $mailbox.PrimarySmtpAddress | Get-ADPermission | Where-Object {$_.extendedrights -eq "send-as"} | select -ExpandProperty user
    $sendOnBehalfPermissions = Get-Mailbox $mailbox.PrimarySmtpAddress | select -ExpandProperty grantsendonbehalfto

    $properties = @{
    Identity = $mailbox.PrimarySmtpAddress
    Users = ($permissions.user -join "`n")
    AccessRights = ($permissions.accessrights -join "`n")
    SendAs = ($sendAsPermissions -join "`n")  
    SendOnBehalf = ($sendOnBehalfPermissions -join "`n")
    }

    $mailboxObject = New-Object -TypeName psobject -Property $properties

    #$mailboxObject | select identity, users, accessrights | Export-Csv -Append -NoTypeInformation -Path c:\temp\test.csv
    $mailboxObject | select identity, users, accessrights, sendas, sendonbehalf | out-gridview
}

gridview 准确地显示了我想要的内容,但我很难将其正确导出到 CSV。

Out-Gridview 示例

4

4 回答 4

1

我知道事实上这可能会更好,更适合您的具体情况,但我认为它仍然值得分享。

您可以获得该Import-Excel模块,该模块可轻松创建和格式化 Excel 文件。您可以从位于此处的 Powershell Gallery 安装它。这基本上是假设您的床单仅供人眼使用,并且考虑到格式,我想我可以在一定程度上自信地说这是真的。

首先,我首先在追加之前收集所有对象,我认为这是一个很常见的错误。只需直接输出对象并将其收集在变量中即可。

$report = @()
$mailboxes = Get-Mailbox -ResultSize unlimited -RecipientTypeDetails sharedmailbox

$collectOutput = foreach ($mailbox in $mailboxes)
{ 
  $permissions = Get-Mailbox $mailbox.PrimarySmtpAddress | Get-MailboxPermission | Select-Object user, accessrights
  $users = $permissions.user
  $accessRights = $permissions.accessrights
  $sendAsPermissions = Get-Mailbox $mailbox.PrimarySmtpAddress | Get-ADPermission | 
    Where-Object {
      $_.extendedrights -eq 'send-as'
    } | Select-Object -ExpandProperty user
  $sendOnBehalfPermissions = Get-Mailbox $mailbox.PrimarySmtpAddress | Select-Object -ExpandProperty grantsendonbehalfto

  [pscustomobject]@{
    Identity     = $mailbox.PrimarySmtpAddress
    Users        = ($permissions.user -join "`r`n")
    AccessRights = ($permissions.accessrights -join "`r`n")
    SendAs       = ($sendAsPermissions -join "`r`n")
    SendOnBehalf = ($sendOnBehalfPermissions -join "`r`n")
  }
}

现在我将使用Export-Excelcmdlet 生成一个 OfficeOpenXml.ExcelPackage 对象,然后我们可以使用一些辅助函数对其进行操作。我发现这几乎是模仿Out-Gridview的。标题包装只是有点偏离,但欢迎您尝试不同的参数,直到找到适合您的东西。

$sheet = $collectOutput | Export-Excel -Path 'C:\Users\username\Desktop\testing.xlsx' -PassThru -AutoSize

$sheet.Sheet1.Column(1) | Set-ExcelRange -VerticalAlignment Center
$sheet.Sheet1.Column(2) | Set-ExcelRange -WrapText
$sheet.Sheet1.Column(3) | Set-ExcelRange -WrapText
$sheet.Sheet1.Column(4) | Set-ExcelRange -WrapText

$sheet.Save()
$sheet.Dispose()

在此处输入图像描述

于 2018-10-09T15:58:27.303 回答
0

你能试试这个

Identity = $mailbox.PrimarySmtpAddress
Users = (@($permissions.user -join "`n") |Out-String).Trim()
AccessRights = (@($permissions.accessrights -join "`n")|Out-String).Trim()
SendAs = (@($sendAsPermissions -join "`n") |Out-String).Trim() 
SendOnBehalf =(@($sendOnBehalfPermissions -join "`n")|Out-String).Trim()

**你可以看看这个网站** https://learn-powershell.net/2014/01/24/avoiding-system-object-or-similar-output-when-using-export-csv/

于 2018-10-17T14:18:30.993 回答
0

Export-Csv就是为此而设计的。

这是一个工作示例:

$o = [PSCustomObject]@{a = 1 } ,
     [PSCustomObject]@{a = 2 }

# Export     
$o | Export-Csv -Path a.csv

# check the file
Get-Content ./a.csv

要查找使用 csv 的命令,您可以使用

Get-Command *csv*
于 2018-10-14T08:14:07.513 回答
0

要将对象转换为可以在选择结果上使用管道转换为 ConvertTo-Csv,如下所示:

  $selectResult = $mailboxObject | select identity, users, accessrights, sendas, sendonbehalf
  $selectResult | out-gridview
  $selectResult | ConvertTo-Csv > .\mailbox.csv
于 2018-10-15T19:03:38.723 回答