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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
<# .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 |