ExTools-O365Management.psm1

##################################################################################################
# Exbabylon Office 365 Related List of Functions
##################################################################################################



##################################################################################################
# Connect-ExchangeSession
# Creates a session that connects to Exchange server
##################################################################################################
Function Connect-ExchangeSession(){
    #Connecting to Exchange Online
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

    #Capture administrative credential for future connections.
    $credential = get-credential

    #Creates an Exchange Online session
    $ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $credential -Authentication  Basic -AllowRedirection

    #Import session commands
    Import-PSSession $ExchangeSession

    #Return these values so I can use the same credentials for other connections and turn off the PSSession when I am done with it
    $result = @{}
    $result.Add("Credential", $Credential)
    $result.Add("Session", $ExchangeSession)

    return $result
}



##################################################################################################
# Convert-MailboxToShared
# Converts mailboxes to shared and removes licenses if prompted in Office 365
##################################################################################################
Function Convert-MailboxToShared(){
    #Install/Import Modules
    Check-Module -ModuleToFind "MSOnline"

    #Connect to Exchange
    $result = Connect-ExchangeSession

    #Connect to MsolService to remove licenses
    Connect-MsolService -Credential $result.Credential

    #Capture Mailbox for share conversion
    $mb = read-host "Enter all mailbox alias seperated by commas (i.e. user1,user2,user3)"

    #Split the array of items that are seperated by commas.
    $mbarray = $mb -Split ','

    #Check if more than one account for better grammer in the read-host $removeLicensesCheck prompt
    $plural = "this"
    if($mbarray.Length -gt 1){
        $plural = "all of these"
    }
    
    #Check if tech wishes to remove Office 365 licenses from the accounts associated with the list of aliases
    $removeLicensesCheck = Read-Host "Do you want to remove Office 365 Licenses for $plural accounts?(y or n)"
    
    #Running the command that is between the brackets on each item ($i) in the array using each item as the variable.
    $removed = $false
    Foreach ($i in $mbarray){
        #convert mailbox to shared mailbox
        Set-Mailbox $i -Type shared
    
        #Variablize the mailbox associated with the alias
        $m = get-mailbox $i
        
        #remove office 365 licenses if desire was indicated to do so.
        if($RemoveLicensesCheck -eq "y"){
            #Remove all office 365 licenses for each account in mbarray
            (get-MsolUser -UserPrincipalName $m.UserPrincipalName).licenses.AccountSkuId |foreach{ Set-MsolUserLicense -UserPrincipalName $m.UserPrincipalName -RemoveLicenses $_ }
            $removed = $true
        }
    }
    
    #Removing the PSSession
    Remove-PSSession $result.Session
    
    #Final test output to indicate all commands processed.
    If($removed -eq $true){
        Read-Host -prompt "All requested mailboxes converted to shared and licenses have been removed unless errors are displayed. You may now exit this script."
    } else {
        Read-Host -prompt "All requested mailboxes converted to shared unless errors are displayed. You may now exit this script."
    }
}

Export-ModuleMember -Function Connect-ExchangeSession, Convert-MailboxToShared