We are delighted to share our new EIC Delivery Methodology for efficiently managing Saviynt Implementations and delivering quick time to value. CLICK HERE.

How to add multiple server in powershell script for mailbox creation at exchange level

Shwet01
New Contributor III
New Contributor III

Hi All,

We create remote mailbox for any user at exchange level using PowerShell script. Currently only one server is added for that, but we want do add multiple server as backup.

So even if one server fails, PowerShell script should pick other server and should not disturb the mailbox creation.

I am not aware where can we add this configuration in script.

Thanks

shwet

1 REPLY 1

rushikeshvartak
All-Star
All-Star
# Array of Exchange servers
$exchangeServers = @("Server1", "Server2", "Server3")

# Mailbox creation parameters
$userName = "user1"
# Add other mailbox creation parameters as needed

foreach ($exchangeServer in $exchangeServers) {
    try {
        # Attempt to create the mailbox on the current server
        $result = New-RemoteMailbox -UserPrincipalName "$userName@domain.com" -RemoteRoutingAddress "$userName@contoso.mail.onmicrosoft.com" -OnPremisesOrganizationalUnit "OU=Users,DC=domain,DC=com" -DomainController $exchangeServer -ErrorAction Stop

        # If successful, break out of the loop
        Write-Host "Mailbox created successfully on $exchangeServer"
        break
    } catch {
        # Handle the error (optional)
        Write-Host "Error creating mailbox on $exchangeServer: $_"
    }
}

# Add additional logic here if needed after the loop

Regards,
Rushikesh Vartak
If you find the response useful, kindly consider selecting Accept As Solution and clicking on the kudos button.