PowerShell Scripts and Microsoft Exchange 2010

What are the default PowerShell scripts for Exchange 2010 and what do they do?

Step-by-step guide

As described in this article PowerShell scripts can be used to perform various tasks during a migration.

The default PowerShell scripts included with the migration tool are for migrations to Microsoft 365. If you are migrating to Exchange 2010, you need to update the scripts, or disable them if they are not needed. The same changes to the Init script can also be made to the ‘Get User’ Init script when migrating from Exchange 2010. These scripts make use of PowerShell replacement variables

Init Script

The init script initializes a remote PowerShell session with the Exchange 2010 server. You should change the value of [!ps-url] in the default script to the URL for your Exchange 2010 PowerShell endpoint. For example:

# Set this so that errors are thrown from Cmdlets
$ErrorActionPreference = 'Stop'
# Open the connection
$sesh = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://myserver.com/powershell -credential (New-Object -TypeName System.Management.Automation.PSCredential -argumentlist '[!admin-email]',(ConvertTo-SecureString '[!admin-password]' -AsPlainText -Force)) -Authentication Basic -AllowRedirection
# Import the session
$imp = Import-PSSession -Session $sesh

Running Locally

It is also possible to run CloudM Migrate directly on an Exchange server. In this case you do not need to setup a remote session and can set the ‘Init Script’ to ‘Never’ run in the tool configuration. The same also applies to the ‘Get User Init Script’ when migrating from Exchange 2010 and you have installed the tool directly on an Exchange server.

Before Script

The before script checks that the specified user exists in Exchange 2010, and creates it if not. An example script for Exchange 2010 is shown below. You should edit the default script (which is for Microsoft 365) to specify UserPrincipalName rather than MicrosoftOnlineServicesID. Note that if migrating to Live@Edu, it should be replaced with WindowsLiveID.

# Set this so that errors are thrown from Cmdlets
$ErrorActionPreference = 'Stop'
# Check if a user exists
Function UserExists() {
    trap {
        return $false
    }
    Get-User -Identity [!user-importname]@[!admin-domain]
    return $true
}
# Create a user if it doesn't exist
Function CreateUser() {
    $ret = UserExists
    if ($ret -eq $false) {
        New-Mailbox -Name '[!user-givenname] [!user-familyname]' -UserPrincipalName [!user-importname]@[!admin-domain] -Password (ConvertTo-SecureString -String '[!user-password]' -AsPlainText -Force)
        # Allow some time for the account to become active
        Start-Sleep –s 15
    }
}
CreateUser

Error Messages

The tool determines whether a script has run successfully if no exception is thrown from the script. In PowerShell, internal cmdlets handle their own errors unless instructed to do so in the script. It is usually important to include the global statement $ErrorActionPreference = 'Stop', in the script.

You can also throw your own custom exception from scripts if required which will then be displayed in the user interface and trace files.

Was this article helpful?
1 out of 1 found this helpful