Quick PowerShell Stuff 16

For this quick post on Powershell, I will tag a previous post for a complimenting pair of scripts. The script in this article will check to see who will be affected when databases are put back in order of Activation Preference. In a previous script – Quick PowerShell Stuff 13
– we were able to see which databases were not mounted correctly in terms of Activation Preference. Now if I need to tell the Help Desk who might be affected by a database activation move, I simply use this script to create the list. AP-InsertThe script will parse out who’s primary mailbox will be affected and who’s archive will be affected. If both archive and primary mailbox exist on the same database, this will be noted as well. Now on to the script.

The Script
The script itself is not too complicated:

  • Lines 4 – 6 break down each database to get their “MasterServerOrAvailabilityGroup” which is the DAG they belong to.
  • Then for each DAG (Line 8) we perform a set of actions.
  • Lines 9 – 11 set up variables for use in the PowerShell loop.
  • Lines 12 – 15 – determine which database copy should be primary (AP 1)
  • Lines 16 – 19 – determine if the mounted copy is the one with an AP of 1 (or primary)
  • Lines 20 and 21 – determine if there are any mailboxes or archive mailboxes present
  • Line 22 checks to see if no archives are present
    – Branching here so I can report on mailboxes and archives together

  • Lines 23 – 25 – determine who has either both mailbox and archive, or just mailbox or just an archive mailbox in the database
  • Lines 26 – 30 – Report users with mailboxes and archives in the database
  • Lines 31 – 35 – Report users with just mailboxes, no archive mailbox in database
  • Lines 36 – 40 – Report users with just an archive mailbox, no user mailbox in database
  • Lines 41 – 44 – Report that only user mailboxes exist, no archive mailboxes
  • Line 49 – formatting only


That is it. Relatively easy to follow (I hope).

Here is the code:

cls
$dag = Get-DatabaseAvailabilityGroup
foreach ($line in $dag) {
    $dagname = $line.name
    write-host "Examining the Database Availability Group $dagname" -ForegroundColor Yellow
    $ap = (Get-MailboxDatabase | where {$_.MasterServerOrAvailabilityGroup -eq $dagname})
         
    foreach ($line in $ap) {
        $server = $line.server
        $database = $line.name
        $set = $line.ActivationPreference
        foreach ($line2 in $set) {
            $value = $line2.value
            $server = $line2.key
            if ($line2.value -eq "1") {
                # write "server $server with database $database has the activation preference of $value"
                $mounted = get-mailboxdatabase $database
                $currentserver = $mounted.server.name
                if ($server -ne $currentserver) {
                    $mbx = get-mailbox -database $database
                    $archive = get-mailbox -database $database -archive
                    if ($archive -ne $null) {
                        $diff = diff $mbx $archive -includeequal | where {$_.sideindicator -eq "=="}
                        $mbxonly = diff $mbx $archive -includeequal | where {$_.sideindicator -eq "<="}
                        $archiveonly = diff $mbx $archive -includeequal | where {$_.sideindicator -eq "=>"}
                        if ($diff -ne $null) {
                            write-host "Users with mailboxes AND archives affected on the $database database are:"
                            foreach ($line in $diff) {$line.inputobject}
                            write-host " "
                        }
                        if ($mbxonly -ne $null) {
                            write-host "Users with just mailboxes affected on the $database database are:"
                            foreach ($line in $mbxonly) {$line.inputobject}
                            write-host " "
                        }
                        if ($archiveonly -ne $null) {
                            write-host "Archive Mailboxes just affected on the $database database are:"
                            foreach ($line in $archiveonly) {$line.inputobject}
                            write-host " "
                        } 
                    } else {
                            write-host "Users with mailboxes affected on the $database database are:"
                            $mbx;write-host " "
                    }
                }
            }
        }
    }
    write-host " "
} 

Quick Run Through

When this script was run, several databases were moved around and thus there were multiple users affected. As you can see by the screenshot, there are users with mailboxes and archives that were affected. After the script it run you can either copy and paste the names into an email and send them to your help desk staff or provide them with a screenshot of the results.

Users affected

Further Reading

Compare-Object – AKA ‘Diff’ – How to use this
Mailbox Database Copies

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s