One of our customers changed the company name. So the alias of all there SharedMailboxes on office365 needed to be changed. This script below contains 2 functions, the fist one is for adding an extra alias to the mailbox and the second one is for activating the new alias. This is the first time i scripted against office365, so it could be that the script is not faultyproof. But hey, its a beginning. Saved about 10 hours of manual clicking during the critical company name change.
<# .SYNOPSIS Add new office365 domain alias and Activate the new office365 alias .DESCRIPTION This scripts contains 2 Functions 1. Create a new domain alias 2. Active the new domain alias and set to primary .PARAMETER(s) $AllMailboxes = Your mailbox filter for selecting your mailboxes $Domainsuffix = New mailbox suffix $Logfile = Logging the actions to a logfile .INPUTS NewMailboxDomainAlias -AllMailboxes $AllMailboxes -Domainsuffix $Domainsuffix -logfile $logfile SetNewAddressActive -AllMailboxes $AllMailboxes -Domainsuffix $Domainsuffix -logfile $logfile .OUTPUTS Log file stored in C:\.log> .NOTES Version: 1.0 Author: Rob Verhees Creation Date: 20-11-2018 Purpose/Change: Initial script development #> function NewMailboxDomainAlias { param ( $AllMailboxes, $Domainsuffix, $logfile ) Foreach ($Mailbox in $AllMailboxes) { $NewAddress = $Mailbox.Alias + "@$Domainsuffix" $Mailbox.EmailAddresses += $NewAddress try{ Set-Mailbox -Identity $Mailbox.Alias -EmailAddresses $Mailbox.EmailAddresses -ErrorAction stop write-log -Level INFO -Message "Succesfully add alias: $newaddress" -logfile $logfile } Catch { write-log -Level ERROR -Message ":: Failed to add the following alias: $newaddress" -logfile $logfile write-log -Level ERROR -Message ":: Mailbox name: $($mailbox.name)" -logfile $logfile write-log -Level ERROR -Message $Error[0].Exception.Message -logfile $logfile } } } function SetNewAddressActive { param ( $AllMailboxes, $Domainsuffix, $logfile ) Foreach ($Mailbox in $AllMailboxes) { $NewAddress = $Mailbox.Alias + "@$Domainsuffix" try{ Set-Mailbox -Identity $Mailbox.Alias -WindowsEmailAddress $NewAddress -ErrorAction stop write-log -Level INFO -Message "Succesfully Activated the alias: $newaddress" -logfile $logfile } Catch { write-log -Level ERROR -Message "Failed to activate the new alias: $newaddress" -logfile $logfile write-log -Level ERROR -Message ":: Mailbox name: $($mailbox.name)" -logfile $logfile write-log -Level ERROR -Message $Error[0].Exception.Message -logfile $logfile } } } Function Write-Log { [CmdletBinding()] Param( [Parameter(Mandatory=$False)] [ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG")] [String] $Level = "INFO", [Parameter(Mandatory=$True)] [string] $Message, [Parameter(Mandatory=$False)] [string] $logfile ) $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss") $Line = "$Stamp $Level $Message" If($logfile) { Add-Content $logfile -Value $Line } Else { Write-Output $Line } } Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Confirm:$false -Force $CRED = Get-Credential $SESSION = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $CRED -Authentication Basic -AllowRedirection Import-PSSession $SESSION $logfile = "C:\NewDomainAlias.txt" $Domainsuffix = "contoso.nl" $AllMailboxes = Get-MailBox -Filter {RecipientTypeDetails -eq "Sharedmailbox"} NewMailboxDomainAlias -AllMailboxes $AllMailboxes -Domainsuffix $Domainsuffix -logfile $logfile SetNewAddressActive -AllMailboxes $AllMailboxes -Domainsuffix $Domainsuffix -logfile $logfile
The post Add new email alias Office365 appeared first on Rob V IT.