With a client we had an issue where the 1st Whiners were having issues enabling UM Mailboxes for new users. This was due to the extension being already allocated to another User. So I created the below script for them.

There was a bit of a challenge to the script as I wanted to make it as simple and comprehensive as possible for them to run.

The script basically runs through the following steps:

  • Launch Powershell as Administrator
  • Install any missing required Powershell modules (MSOnline)
  • Connect to Exchange Online and Office 365 Powershell
  • Find which users has been allocated a UM Mailbox with X extension
  • Checks what O365 license the above user has
    • If the user doesn’t have a E3 or E5 license, then it prompts you if you want to allocate a E3 or E5 license
  • Prompts if you want to disable the UM Mailbox for the user
  • Disables UM Mailbox
  • Removes O365 License if previously added by the script
  • Script Ends with a countdown.

#####################
#
# Powershell Script
#
# Author - KumoInsight
# Email - contact@kumoinsight.co.uk
# Date - 01/04/2019
# Description - Script to find UM Mailbox assigned to a particular extension and disables UM for the user.
# 
# Version History
# 1.0 - 01/04/2019 Initial Version
#
#####################
#
# Requirements:
# - Run PowerShell as administrator with command "Set-ExecutionPolicy unrestricted" on the host running the script
#
#####################
#
# Legal Disclaimer:
# This script is written by Patrick Carder is not supported under any support program or service. 
# All scripts are provided AS IS without warranty of any kind. 
# The author further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. 
# The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. 
# In no event shall its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if the author has been advised of the possibility of such damages.
#
#####################


# --- Check if Powershell is running with Admin rights --- #

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{   

# ---  No Administrative rights, it will display a popup window asking user for Admin rights --- #

$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments

break
}

# --- After user clicked Yes on the popup, your file will be reopened with Admin rights --- #

# --- Check if MSOnline is installed, if not it will install it --- #

Write-Host "Checking if MSOnline is installed"
if (Get-Module -ListAvailable -Name MSOnline) {
    Write-Host "MSOnline Module exists"
} 
else {
    Write-Host "Module does not exist, installing MSOnline Module" 
    
    Install-Module -Name MSOnline -ErrorAction SilentlyContinue -Force -Confirm:$false
    Write-Host "Installation Complete"
 }

# --- Connect o O365 and Exchange Online PowerShell --- #

Write-Host 'Connecting to O365 and Exchange Online PowerShell'
Import-Module MSOnline
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Connect-MsolService –Credential $LiveCred
Write-Host "You've connected to O365 and Exchange Online PowerShell"

# --- Find Extension allocated to UM Mailbox--- #

$umextension = Read-Host -Prompt 'Enter the extension number'
$UMUser = Get-UMMailbox | where { $_.Extensions -eq "$umextension" } 
Write-Host 'This Extension has already been assigned to' $UMUser

# --- Set O365 License Variables --- #

$E3License = Get-MsolAccountSku | where {$_.AccountSkuId -Like  '*enterprisepack'}
$E5License = Get-MsolAccountSku | where {$_.AccountSkuId -Like  '*ENTERPRISEPREMIUM_NOPSTNCONF'}

# --- Check User's O365 License --- #
ForEach ($member in $UMUser) {If (Get-MsolUser -UserPrincipalName $UMUSer.PrimarySMTPAddress | where {$_.IsLicensed -eq $E3License.AccountSkuId -or $E5License.AccountSkuId}) {$UserLicensed = "True"} else {$UserLicensed = "False"}}
Write-Host "The user is licensed?" $UserLicensed

Start-Sleep -s 2

# --- User not licensed, which license to apply? E3 or E5 --- #

If ($UserLicensed -eq "False") {function Get-SomeInput {$input = read-host $member.name "does not have a valid o365 license, do you want to assign them a E3 or E5 license? (E3 or E5 or Exit)"

    switch ($input) `
    {
        'E3' {$AssignE3 = "True"}

        'E5' {$AssignE5 = "True"}

        'Exit' {write-host 'You Selected Exit, the Script will now exit' ; Exit()}

        default {
            write-host 'You may only answer E3 or E5 or Exit, please try again.'
            Get-SomeInput
        }
    }
}
Get-SomeInput
} else {}

Start-Sleep -s 2

# --- Add O365 E3 License --- #

If ($AssignE3 -eq "True") {ForEach ($member in $UMUser) {Write-Host "User need's to be licensed" ; Set-MsolUser -UserPrincipalName $UMUSer.PrimarySMTPAddress -UsageLocation GB ; Set-MsolUserLicense -UserPrincipalName -AddLicenses $E3License.AccountSkuId ; Write-Host "E3 License Assigned to" $UMUSer ; $UserLicenseAdded = "True"}}  else {}


# --- Add O365 E5 License --- #

If ($AssignE5 -eq "True") {ForEach ($member in $UMUser) {Write-Host "User need's to be licensed" ; Set-MsolUser -UserPrincipalName $UMUSer.PrimarySMTPAddress -UsageLocation GB ; Set-MsolUserLicense -UserPrincipalName -AddLicenses $E5License.AccountSkuId ; Write-Host "E5 License Assigned to" $UMUSer ; $UserLicenseAdded = "True"}}  else {}

# ForEach ($member in $UMUser) {If (Get-MsolUser -UserPrincipalName $UMUSer.PrimarySMTPAddress | where {$_.IsLicensed -eq $E3License.AccountSkuId}) {Write-Host $UMUSer "has an E3 License"} else {Write-Host $UMUSer "does not have an E3 License" ; Set-MsolUser -UserPrincipalName $UMUSer.PrimarySMTPAddress -UsageLocation GB ; Set-MsolUserLicense -UserPrincipalName -AddLicenses $E3License.AccountSkuId ; Write-Host "E3 License Assigned to" $UMUSer}}

# --- Disable UM Mailbox --- #

ForEach ($member in $UMUser) {function Get-SomeInput {$input = read-host "Do you want disable the Mailbox for" $member.name "? (Yes or No)"

    switch ($input) `
    {
        'yes' {Disable-UMMailbox -identity $member.name}

        'no' 
{write-host 'You Selected no, the UMMailbox Script will now exit'}

        default {
            write-host 'You may only answer yes or no, please try again.'
            Get-SomeInput
        }
    }
}
Get-SomeInput
}

# --- Remove O365 License

If ($UserLicenseAdded -eq "true") {ForEach ($member in $UMUser) {Write-Host "User has had E3 previously added" ; Set-MsolUserLicense -UserPrincipalName -RemoveLicenses $E3License.AccountSkuId ; Write-Host "E3 License removed from" $UMUSer; $UserLicenseAdded = "False"}} else {}

# --- Script Finished Closing Down --- #

write-host 'The UMMailbox Script has been completed'

    Write-Host "Powershell will now close in 5 seconds"
    Start-Sleep -s 1
    Write-Host "5"
    Start-Sleep -s 1
    Write-Host "4"
    Start-Sleep -s 1
    Write-Host "3"
    Start-Sleep -s 1
    Write-Host "2"
    Start-Sleep -s 1
    Write-Host "1"
    Start-Sleep -s 1
    Write-Host "0"
    Start-Sleep -s 1

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *