Need to help the modify PowerShell for Exchange server formatting to show the Delegates and AccessRights ?

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

Need to help the modify PowerShell for Exchange server formatting to show the Delegates and AccessRights ?

Post by ITEngineer » Mon Sep 10, 2018 3:43 am

Hi,

I need some assistance in modifying the script that is working but needs some reformatting:
Script purpose: Export the list of User Mailbox with other people access other than the user itself to the .CSV

Delegates column showing too many commas even when the result is empty.
AccessRights somehow the result shows no username for the (FullAccess, ReadPermission) rights.

Code: Select all

$filter = '(Enabled -eq $false) -and (msExchRecipientTypeDetails -ne 4) -and (homeMDB -ne "$null")'
$properties = @('homeMDB', 'mailNickName', 'mail', 'DisplayName', 'SamAccountName', 'ProxyAddresses')

$Users = Get-ADUser -Filter $filter -Properties $properties -ResultPageSize 512; 

$Users | ForEach-Object {
        $stat = Get-MailboxStatistics $_.SamAccountName

        $smtpAddresses = ($_.ProxyAddresses | Where-Object {$_ -match "^smtp:" }) -replace 'smtp:', ''
        $primarySmtpAddress = ($_.ProxyAddresses | Where-Object {$_ -cmatch "^SMTP:" }) -replace 'SMTP:', ''

        $delegates = @(Get-MailboxPermission -Identity $primarySmtpAddress | 
                       Where-Object { ($_.AccessRights -like "*FullAccess*") -and 
                                      (-not $_.IsInherited) -and 
                                      ($_.User -ne "NT AUTHORITY\SELF") -and 
                                      ($_.User -notlike '*Discovery Management*') } |
                       Select-Object @{Name='Delegate'; Expression={(Get-Recipient $_.User.toString()).DisplayName}}, 
                                     @{Name='AccessRights';Expression={$_.AccessRights -join ', '}})

        $access = $delegates | ForEach-Object { "{0} ({1})" -f $_.Delegate, ($_.AccessRights -join ', ') }

        New-Object -TypeName PSObject -Property ([ordered]@{
            DisplayName             = $_.DisplayName
            mailNickName            = $_.mailNickName
            SamAccountName          = $_.SamAccountName
            mail                    = $_.mail
            PrimarySMTPAddress      = $primarySmtpAddress
            ProxyAddresses          = $smtpAddresses -join ';'
            HomeMDB                 = $_.homeMDB.Split(',=')[1]
            MBytes                  = $stat.TotalItemSize.Value.ToMB()
            LastLogonTime           = $stat.LastLogonTime
            LastLoggedOnUserAccount = $stat.SamAccountName
            DisconnectDate          = $stat.DisconnectDate
            Delegates               = $delegates.Delegate -join ', '        
            AccessRights            = $access -join ', '
        })
    } | 
    Sort-Object MBytes -Descending | 
    Export-Csv C:\Results.csv -NoTypeInformation
Thank you in advance.
/* IT Engineer */

Locked