In your SharePoint environment, have you found users migrating from one domain to another? Or the domain structure changing due to any reason? What if there are bulk migrations of users? Here is the PowerShell script to help you out in doing just that.
This script is to automate the following command…
stsadm -o migrateuser –oldlogin <domain\name> –newlogin <domain\name> [-ignoresidhistory]
The script is well documented so you shouldn’t have any issues in understanding what it does. However, here is what I am doing in English.
- Ensure that there are two folders present called In and Out
- In folder would contain input.txt file
- The format of this file is very simple…
User1Alias:From_Domain:To_Domain
User2Alias:From_Domain:To_Domain
User3Alias:From_Domain:To_Domain
- That’s about it. Once your Input.txt file is complete, run this script
- Once the Script is over, it will create files in Out folder
Confirm the following…
- Ensure that Pending.txt file in the Out folder has nothing inside it. If there is anything, it means the User Migration has failed for that user. If there is no text in the Pending.txt file, it would signify successful migration of all users in the input.txt file.
- Ensure that all the lines that you added in the Input.txt file, NOW appears in Done.txt
FYI - You can verify this by looking in the InputBackup.txt file that gets created automatically in the In folder
#Region Load Assemblies
# Load Sharepoint DLL's
[System.Reflection.assembly]::LoadWithPartialName("Microsoft.SharePoint")
$spFarm = [Microsoft.SharePoint.Administration.SPFarm]::Local
$Date = Get-Date
#All the folders that contain Migration Information.
#Log.xml would contain all the information that is generated by this Script
$root = "C:\Scripts\MigrateUsers"
$Input = "$root\In\Input.txt"
$InputBackup = "$root\In\InputBackup.txt"
$Done = "$root\Out\Done.txt"
$Pending = "$root\Out\Pending.txt"
$Log = "$root\Out\Log.txt"
#Ensure that the Out folder exists. It it doesn't, create it.
if(Test-Path $Pending){} else{New-Item -Path "$root\Out" -type directory -force > $null}
#Start logging
Add-Content $Log "Migration Activity on $Date"
Add-Content $Log "-----------------------------------------"
#Ensure that the In folder exists. It it doesn't, create it.
if(Test-Path $Input)
{
#Removing older backup copies of Input-Backup file if present
if(Test-Path "$InputBackup") { Remove-Item "$InputBackup" }
#Create a back up copy of the input
Add-Content $Log "Creating a backup copy of Input.txt"
Copy-Item $Input "$InputBackup"
Add-Content $Log "Backup of the Input file successfully created"
}
else
{
#Create a folder called In if it doesn't exist
New-Item -Path "$root\In" -type directory -force > $null
Add-Content $Log "Input.txt file doesn't exist in the In Folder. Please add this file and try again!"
break
}
#Start reading from the Input.txt file
Add-Content $Log "About to process Input.txt file"
Add-Content $Log "---------------------------------"
$InputFile = Get-Content $Input
foreach($line in $InputFile)
{
#If successfull write a entry to $SuccessLog
try
{
Add-Content $Log "Reading Line = $line"
$name = $line.Split(':')[0]
$from = $line.Split(':')[1]
$to = $line.Split(':')[2]
if((!$name) -or (!$from) -or (!$to))
{
Add-Content $Log "[FAIL] Could not migrate $line"
Add-Content $Pending $line
}
else
{
$Date = Get-Date
Add-Content $Log "$Date - Migrating the user Name = $name, From = $from, To = $to with `$false, so that history is not ignored"
$spFarm.MigrateUserAccount("$from\$name", "$to\$name", $false)
Add-Content $Log "[SUCCESS]Migrated the user Name = $name, From = $from, To = $to"
Add-Content $Done $line
}
}
catch [System.Exception]
{
Add-Content $Log "Error returned was : $_"
Add-Content $Log "[FAIL]Could not migrate $line"
Add-Content $Pending $line
}
}
Add-Content $Log "File Processed. Check the $Log file for logs, $Done for successful and $Pending for unsuccessful migrations"
Add-Content $Log "Removing Input file. You can find a backup at $InputBackup"
Remove-Item $Input
Add-Content $Log "Input file Removed."
Add-Content $Log "Complete!!!"
Add-Content $Log "==========="
Add-Content $Log "==========="
Hope this helps,
Rahul
Quote of the day:
I don't care what is written about me so long as it isn't true. - Dorothy Parke