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

Ask your 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.
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post by ITEngineer »

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 */
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked