Page 1 of 1

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

Posted: Mon Sep 10, 2018 3:43 am
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.