Public/Convert-MailboxToShared.ps1

function Convert-MailboxToShared {
    <#
.SYNOPSIS
-Taylor Lee
Modified 7152019
 
.DESCRIPTION
Convert an Exchange Online Mailbox to a Shared Mailbox without the need of maintaining a license.
 
.NOTES
Requirements:
    1. This Function is used for an Active Directory Synced account that is being disabled and the mailbox converted to an Exchange Online Shared Mailbox.
    2. This Function must be run from the server that hosts Azure AD Connect and the on-premise Account must be synce to an Exchange Online Mailbox.
    3. The Exchange online Module must be imported into the console before this function is run.
    4. The account associated with the Exchange Online Mailbox must be disabled and moved to an OU that is not Azure AD Synced
#>


    [CmdletBinding()]
    Param (
    )

    #Begin Process by declaring prerequisites and providing a chance to abort the function
    begin {

        Write-Host " "
        Write-Warning "The following requirements must be met. If they are not then abort this function!"
        
        Write-Host " "
        Write-Host "
Prerequisites:
1. This Function is used for an Active Directory Synced account that is being disabled and the mailbox converted to an Exchange Online Shared Mailbox.
2. This Function must be run from the server that hosts Azure AD Connect and the on-premise Account must be synced to an Exchange Online Mailbox.
3. The Exchange online Module must be imported into the console before this function is run.
4. The account associated with the Exchange Online Mailbox must be disabled and moved to an OU that is not Azure AD Synced
5. Powershell version 5 is required
        "
 -ForegroundColor Green

        Write-Host " "
        Write-Host "If requirements are met the menu is worked through in chronological order" -ForegroundColor Yellow
        Write-Host " "

        $RequirementsMet = Read-Host "Have you Read and Completed the prerequisites? (yes/no)"

        if ($RequirementsMet -eq 'yes') {
        }
        else {
            Write-Host "Meet the prerequisites first!" -ForegroundColor Red
            break
        }
    }

    #Present a menu that steps through the process of creating a shared function
    process {
        
        #Create a Menu that leads through calling other functions and managing the shared creation.
        function Show-Menu {
            param (
                [string]$Title = 'My Menu'
            )
            Clear-Host
            Write-Host " "
            Write-Host "================ $Title ================"                       -ForegroundColor Magenta
    
            Write-Host "1: Press '1' Start Azure Sync."                                 -ForegroundColor Green
            Write-Host "2: Press '2' Check that disabled Mailbox is not active"         -ForegroundColor Green 
            Write-Host "3: Press '3' Stage Shared Mailbox"                              -ForegroundColor Green
            Write-Host "4: Press '4' Copy Disabled Mailbox to the Shared Mailbox"       -ForegroundColor Green
            Write-Host "5: Press '5' Monitor Progress of copy to shared mailbox"        -ForegroundColor Green
            Write-Host "6: Press '6' Set SMTP addresses for the Shared Mailbox"         -ForegroundColor Green
            Write-Host "Q: Press 'q' to quit."                                          -ForegroundColor Green
        }

        #Start a do-until loop for processing the creation of the shared mailbox and managing it.
        do {
            #Guiding menu that is returned to until the function is ended
            Show-Menu -Title Convert-MailboxToShared

            #Switch that runs other functions for tasks shown in the menu
            $selection = Read-Host "Please make a selection"
            switch ($selection) {
                '1' {
                    #Start and azure sync to disconnect the disabled mailbox
                    Write-Host "Syncing the disabled account to Azure AD." -ForegroundColor Yellow

                    Start-ADSyncSyncCycle -PolicyType Delta
                   
                } '2' {
                    ##Check for the disabled mailbox for moving to step 3
                    #Create a sub-menu for re-running a function
                    function Show-Menu2 {
                        param (
                            [string]$Title = 'My Menu'
                        )
                        Clear-Host
                        Write-Host " "
                        Write-Host "================ $Title ================"              -ForegroundColor Magenta
    
                        Write-Host "1: Press '1' Check for Mailbox Disabled"               -ForegroundColor Green
                        Write-Host "Q2: Press 'Q2' If the mailbox shows after pressing 1"  -ForegroundColor Green
                    }

                    #Start a do-until loop to check for the completion of a mailbox being disabled
                    do {
                        Show-Menu2 -Title "Check Mailbox Disabled"

                        $selection = Read-Host "Please make a selection"
                        switch ($selection) {
                            '1' {
                                get-mailbox -softdeletedmailbox | Sort-Object Name | Format-Table
                            } 
                        }
                        Pause 
                    } until ($selection -eq 'Q2')

                } '3' {
                    #Create a shared mailbox and grant permissions to it
                    Write-Host "Respond to Prompts to create the shared mailbox" -ForegroundColor Yellow
                    Write-Host "The Names used for the mailbox must not match the disabled mailbox" -ForegroundColor Yellow
                    
                    #Prompt variable for Creating the mailbox and asking if anyone should be granted permission to it.
                    $MailboxName = Read-Host "Type a Unique name for the Mailbox (ex: John Smith Archive)"
                    $MailboxAlias = Read-Host "Type a Unique Alias for the Mailbox (ex: JSMith)"
                    $PromptAccess = Read-Host "Do you wish to grant someone access to the mailbox? (yes/no)"
                    
                    #Creates the Shared Mailbox
                    New-Mailbox -Shared -Name "$MailboxName" -DisplayName "$MailboxName" -Alias "$MailboxAlias"

                    if ($PromptAccess -eq 'yes') {
                        ##Grant access to the shared mailbox
                        #Menu for re-running function until all users that should have access are granted it.
                        function Show-Menu3 {
                            param (
                                [string]$Title = 'My Menu'
                            )
                            Clear-Host
                            Write-Host " "
                            Write-Host "================ $Title ================"       -ForegroundColor Magenta
    
                            Write-Host "1: Press '1' Add Access for a user"             -ForegroundColor Green
                            Write-Host "Q3: Press 'Q3' to quit step 3"                  -ForegroundColor Green
                        }

                        Write-Host " "
                        Write-Host "Time to grant Full Access to the Shared Mailbox" -ForegroundColor Yellow
                        Pause

                        #Start a do-until loop to grant access
                        do {
                            Show-Menu3 -Title "Grant User Access to Shared Mailbox"

                            $selection = Read-Host "Please make a selection"
                            switch ($selection) {
                                '1' {
                                    Write-Host "When the Popup appears, select a mailbox, and hit ok." -ForegroundColor Yellow
                                    $AccessPermission = get-mailbox | Out-GridView -passthru -title "Select a Mailbox and Hit Ok"
                                    Add-MailboxPermission -Identity "$MailboxName" -User $AccessPermission.name -AccessRights FullAccess -InheritanceType All
                                } 
                            }
                            Pause 
                        } until ($selection -eq 'Q3')
                    }

                } '4' {
                    ##Perfom a mailbox copy from the disabled mailbox to the new mailbox

                    #Get GUID of new shared mailbox:
                    Write-Host "Select the Shared Mailbox and Hit Ok" -ForegroundColor Yellow
                    $SharedMailbox = Get-Mailbox | Where-Object { $_.RecipientTypeDetails -eq 'SharedMailbox' } | Out-GridView -passthru -title "Select the Shared Mailbox and Hit Ok"
                    Write-Host "The Shared Mailbox Guid is" -ForegroundColor Cyan
                    $SharedMailbox.ExchangeGuid

                    #Get GUID of old inactive mailbox:
                    Write-Host "Select the disconnected/disabled Mailbox and Hit Ok" -ForegroundColor Yellow
                    $DisconnectedMailbox = Get-Mailbox -softdeletedmailbox | Out-GridView -passthru -title "Select the disconnected/disabled Mailbox and Hit Ok"
                    Write-Host "The Disconnected/disabled Mailbox Guid is" -ForegroundColor Cyan
                    $DisconnectedMailbox.ExchangeGuid

                    $TargetMailbox = Read-Host "Paste the Shared Mailbox guid here without spaces"
                    $SourceMailbox = Read-Host "Paste the disconnected/disabled mailbox guid here without spaces"

                    #Copy inactive mailbox content to the new active or shared mailbox:
                    Write-Host "The Disconnected Mailboxes emails will be copied to the Shared Mailbox. The Job name matches the Target Guid."
                    New-MailboxRestoreRequest -SourceMailbox $SourceMailbox -TargetMailbox $TargetMailbox -TargetRootFolder "Inactive Mailbox Archive" -AllowLegacyDNMismatch -BatchName $TargetMailbox
                    
                } '5' {
                    ##Monitor progress of the mailbox copy and remove completed requests
                    function Show-Menu4 {
                        param (
                            [string]$Title = 'My Menu'
                        )
                        Clear-Host
                        Write-Host " "
                        Write-Host "================ $Title ================"       -ForegroundColor Magenta
    
                        Write-Host "1: Press '1' Check Mailbox copy Progress"       -ForegroundColor Green
                        Write-Host "2: Press '2' Kickstart Stalled Progress"        -ForegroundColor Green
                        Write-Host "3: Press '3' Remove Completed Copy Job"         -ForegroundColor Green
                        Write-Host "Q5: Press 'Q5' to quit step 5"                  -ForegroundColor Green
                    }

                    do {
                        Show-Menu4 -Title "Grant User Access to Shared Mailbox"

                        $selection = Read-Host "Please make a selection"
                        switch ($selection) {
                            '1' {
                                Get-MailboxRestoreRequest | Get-MailboxRestoreRequestStatistics | Format-Table
                            } '2' {
                                Get-MailboxRestoreRequest | Resume-MailboxRestoreRequest
                            } '3' {
                                Get-MailboxRestoreRequest | Where-Object { $_.status -eq "completed" } | Remove-MailboxRestoreRequest
                            } 
                        } Pause 
                    } until ($selection -eq 'Q5')
                    
                } '6' {
                    #Set SMTP Addresses for the Shared Mailbox

                    Write-Host "Select the Shared Mailbox and Hit Ok" -ForegroundColor Yellow
                    $SharedMailbox = Get-Mailbox | Where-Object { $_.RecipientTypeDetails -eq 'SharedMailbox' } | Out-GridView -passthru -title "Select the Shared Mailbox and Hit Ok"
                    Write-Host " "
                    Write-Host "For SMTP Addresses Write them out like so..." -ForegroundColor Yellow
                    Write-Host '"SMTP: John.Smith@company.com, John.Smith@mail.onmicrosoft.company.com, JohnS@company.com"' -ForegroundColor Cyan
                    Write-Host " "
                    $SMTPAddresses = Read-Host "Specify SMTP Address and the primary using SMTP as shown in the example"
                    Set-Mailbox $SharedMailbox.name -EmailAddresses $SMTPAddresses
                } 
            }
            Pause
        } until ($selection -eq 'q')
    }

    #Final statements and cleanup
    end {
        Write-Host "Don't Forget to tell those with permission to the Shared Mailbox. to look for it in outlook." -ForegroundColor Yellow
    }
}