Generate User Mapping for Sharegate migration tool using PnP PowerShell
Generate User Mapping for Sharegate migration tool using PowerShell:
Use the below PowerShell code to generate the User Mapping file (.sgum) for Sharegate Desktop migration. Click on "Import user and group mapping" button as mentioned screen shot in below. Once mapping updated then you can see Source and Destination Users and Groups will be mapped under "Source" and "Destination" column. The Sharegate tool will map your users automatically and migrated.
You have another option "Unresolved users or groups", this will use to map user on target which is missing. You can replace User on target which is existed at the source, but does not exist at the destination.
Use below code in PowerShell.
-------------------------------------------------- Sample Code --------------------------------------------------
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#This code is use for get current folder path from where you are running .ps1 file
#Generated UserMapping will be saved on same path with extension .sgum
[string]$scriptName = $MyInvocation.MyCommand.Name.TrimEnd(".ps1");
[string]$scriptFullPath = $MyInvocation.MyCommand.Definition;
[string]$scriptFileFolder = Split-Path -Path $scriptFullPath -Parent;
$currentDate = Get-Date -format ddMMyyyy-hhmmss;
$errorFile = "UserMapping_$($currentDate).csv"
$errorDetails = "SourceURL,Status"
$errorDetails | Out-file $errorFile -append
function GenerateUserMapping()
{
#Source URL
$sourceURL = "USE SELECT SOURCE SITE URL TO GET ALL USERS";
#Connect to Source URL using PNP connection
Connect-PnPOnline $sourceURL -UseWebLogin
$sourceClientContext = Get-PnPContext
$sourceWeb = $null;
try
{
#Get Source URL Wen context
$sourceWeb = $sourceClientContext.Web;
$sourceClientContext.Load($sourceWeb);
$sourceClientContext.ExecuteQuery();
}
catch
{
[string]$FuncName=$MyInvocation.MyCommand.Name;
Write-Host -Message $_ -FunctionName $FuncName;
$errorDetails = "$sourceURL, error"
$errorDetails | Out-file $errorFile -append
}
#Get all Users
$allUsers = $sourceWeb.SiteUsers
$sourceClientContext.Load($allUsers)
$sourceClientContext.ExecuteQuery()
#This function is used for to create actual file
generateUserXMLFileForSharegate $Users $sourceURL
}
function generateUserXMLFileForSharegate($allUsers,$sourceURL)
{
$NameofFile = "PROVIDE ANY NAME FOR FILE"
$XMLFile ="$scriptFileFolder\UserMapping_$NameofFile.sgum"
#Create new XML File
[System.XML.XMLDocument]$XMLDocument=New-Object System.XML.XMLDocument
#Add XML Declaration
$Declaration = $XMLDocument.CreateXmlDeclaration("1.0","UTF-8",$null)
$XMLDocument.AppendChild($Declaration)
#Create Root Node
[System.XML.XMLElement]$XmlMainRoot =$XMLDocument.CreateElement("UserMappings")
[System.XML.XMLElement]$XMLRoot=$XMLDocument.CreateElement("Mappings")
$allUsers | ForEach-Object {
#Add child node "Item"
if ($_.Email -ne "")
{
$ItemElement = $XMLDocument.CreateElement("Mapping")
$ItemElement1 = $XMLDocument.CreateElement("Source")
$ItemElement1.SetAttribute("AccountName", $_.LoginName)
$ItemElement1.SetAttribute("DisplayName", $_.LoginName)
$ItemElement1.SetAttribute("PrincipalType", "None")
$ItemElement2 = $XMLDocument.CreateElement("Destination")
$ItemElement2.SetAttribute("AccountName","i:0#.f|membership|$($_.Email)")
$ItemElement2.SetAttribute("DisplayName","i:0#.f|membership|$($_.Email)")
$ItemElement2.SetAttribute("PrincipalType", "None")
$ItemElement.AppendChild($ItemElement1)
$ItemElement.AppendChild($ItemElement2)
$XMLRoot.AppendChild($ItemElement)
}
}
# Append Root Node to XML
$XmlMainRoot.AppendChild($XMLRoot)
$XMLDocument.AppendChild($XmlMainRoot)
#Save XML File
$XMLDocument.Save($XMLFile)
}
try
{
GenerateUserMapping
}
catch
{
[string]$FuncName=$MyInvocation.MyCommand.Name;
Write-Host -Message $_ -FunctionName $FuncName;
}
-------------------------------------------------- Sample Code --------------------------------------------------

Comments
Post a Comment