RENSOL
RENSOL

Reputation: 1

Combining 2 cmdlets for one result/output using pscustomobject

Basically what i'm trying to achieve here is an output with 4 column/list (in this case i'm exporting as a text) Get-MailboxPermission gives me a property of identity, user, accessrights but it doesn't give me a property of "Manager". I need to identify where that particular user reports to. So, i tried PSCustomObject and hoping i can put the results in an array. See script below

$GETMAILBOXPERM = Get-Content C:\Users\Account\Desktop\MailboxUsers\MAILBOXESUSERS.txt | ForEach-Object {Get-MailboxPermission $_ |
    where {
    ($_.User -notlike ‘*NT AUTHORITY*’) -and
    ($_.User -notlike ‘*S-1-5-21-*’) -and
    ($_.User -notlike ‘*NAMPRD08*’) -and
    ($_.User -notlike ‘*PRDTSB01*’) -and
    ($_.User -notlike ‘*0365Admin*’) -and
    ($_.User -notlike ‘*Discovery Management*’) -and
    ($_.User -notlike ‘*NAMPR08A005*’) -and
    ($_.User -notlike ‘*NT AUTHORITY*’)
    }
    }
    $Results = foreach( $Mailbox in (get-content C:\Users\Account\Desktop\MailboxUsers\MAILBOXESUSERS.txt))
    {
    $Users = Get-User $Mailbox
    if ($Users){
    foreach ($User in $Users){
    [pscustomobject]@{
                    DisplayName       = $User.name
                    Account           = $GETMAILBOXPERM.user
                    Manager           = $User.manager
                    Access            = $GETMAILBOXPERM.accessrights
                             }
                    }
               }
    }
    $Results | Format-List -Property DisplayName, Account, Manager, Access | Out-File C:\Users\Account\Desktop\MailboxUsers\mailbox4.txt

Here's the output in text file. I get the DisplayName and Manager right but the Account and Access just doesn't seem to loop from the text file.

DisplayName : MAILBOX1

Account : {[email protected], [email protected], [email protected], [email protected]...}

Manager : MANAGER1

Access : {FullAccess, FullAccess, FullAccess, FullAccess...}

DisplayName : MAILBOX2

Account : {[email protected], [email protected], [email protected], [email protected]...}

Manager : MANAGER2

Access : {FullAccess, FullAccess, FullAccess, FullAccess...}

Upvotes: 0

Views: 102

Answers (1)

postanote
postanote

Reputation: 16096

The user manager attribute is normally in ADDS, not Exchange. Yet, that text file seems to be where you are getting this from vs dynamically from ADDS.

Why are you using Format-List? PowerShell will automatically format as a list the moment you columns exceed 5.

This is untested, since I do not have an environment to try it on, but a refactor of what you have here. Give it a shot.

$GetMailboxPerm = Get-Content -Path 'C:\Users\Account\Desktop\MailboxUsers\MAILBOXESUSERS.txt' | 
ForEach-Object {Get-MailboxPermission $PSitem |
where {
        ($PSitem.User -notlike ‘*NT AUTHORITY*|
        *S-1-5-21-*|
        *NAMPRD08*|
        *PRDTSB01*|*0365Admin*|
        *Discovery Management*|
        *NAMPR08A005*|
        *NT AUTHORITY*’)
    }
}

foreach( $Mailbox in (Get-Content -Path 'C:\Users\Account\Desktop\MailboxUsers\MailboxUsers.txt'))
{
    $Users = Get-User $Mailbox
    if ($Users)
    {
        foreach ($User in $Users)
        {
            [pscustomobject]@{
                DisplayName       = $User.name
                Account           = $GetMailboxPerm.user
                Manager           = $User.manager
                Access            = $GetMailboxPerm.accessrights
            } | Out-File -FilePath 'C:\Users\Account\Desktop\MailboxUsers\mailbox4.txt' -Append
        }
    }
}

Upvotes: 0

Related Questions