Announcing the Saviynt Knowledge Exchange unifying the Saviynt forums, documentation, training,
and more in a single search tool across platforms. Read the announcement here.

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

Shwet01
Regular Contributor
Regular Contributor

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.