If you are trying to migrate data from one of your development environment to another development environment, you would typically do this through a database restore. There are a number of parameters within AX you need to change after the restore.
Is there a simple way to preserve the users and their roles in the destination environment (The one being overwritten)? Yes indeed, you can use PowerShell scripts to export the users and their roles before you do the restore. Use this exported list to recreate your users.
The PowerShell script below does the following.
- Loops through and extracts all users
- For each user extracts all the roles the user is assigned
- Creates a new powershell script file which has commands to recreate the user and add them to their roles
- The path of the file is hardcoded and you might want to change that “C:\TEMP\ExportedUsers.ps1″.
# The below code segment has been extracted form the Management Shell to load AX modules #---------------------------------------------------------------------------------------- function ImportAXModule($axModuleName, $disableNameChecking, $isFile) { try { $outputmessage = "Importing " + $axModuleName Write-Output $outputmessage if($isFile -eq $true) { $dynamicsSetupRegKey = Get-Item "HKLM:\SOFTWARE\Microsoft\Dynamics\6.0\Setup" $sourceDir = $dynamicsSetupRegKey.GetValue("InstallDir") $axModuleName = "ManagementUtilities\" + $axModuleName + ".dll" $axModuleName = join-path $sourceDir $axModuleName } if($disableNameChecking -eq $true) { import-module $axModuleName -DisableNameChecking } else { import-module $axModuleName } } catch { $outputmessage = "Could not load file " + $axModuleName Write-Output $outputmessage } } $dynamicsSetupRegKey = Get-Item "HKLM:\SOFTWARE\Microsoft\Dynamics\6.0\Setup" $sourceDir = $dynamicsSetupRegKey.GetValue("InstallDir") $dynamicsAXModulesPath = join-path $sourceDir "ManagementUtilities\Modules" $env:PSModulePath = $env:PSModulePath + ";" + $dynamicsAXModulesPath ImportAXModule "AxUtilLib" $false $true #AxUtil uses "Optimize" verb. #Therefore we use -DisableNameChecking to suppress warning about uncommon verb being used. ImportAXModule "AxUtilLib.PowerShell" $true $false ImportAXModule "Microsoft.Dynamics.Administration" $false $false ImportAXModule "Microsoft.Dynamics.AX.Framework.Management" $false $false #---------------------------------------------------------------------------------------- # Clear the screen so you can see what is happening much more easily cls # Change this file path to where you want to save the generated PowerShell script $filePath = "C:\Temp\ExportedUsers.ps1" $lineToWrite = " " # Deletes the file to ensure we generate a clean file New-Item $filePath -ItemType file -Force # Retrieve the list of users $userList = Get-AXUser # Loop through the user list foreach($user in $userList) { # Writes a line to the file, this line writes a PowerShell script to create/add this user to Dynamics AX $lineToWrite = "New-AXUser -AccountType " + $user.AccountType + " -AXUserId " + $user.AXUserId + " -Company " + $user.Company + " -UserDomain " + $user.UserDomain + " -UserName " + $user.UserName $lineToWrite | Out-File -FilePath $filePath -Append; # Retrieve all the security roles the user is assigned to $securityRoles = Get-AXSecurityRole -AxUserID $user.AXUserId # Loop through the security roles so we can write PowerShell script to the file foreach($securityRole in $securityRoles) { # Writes a line to the file, this line writes a PowerShell script to add the Role to the User $lineToWrite = "Add-AXSecurityRoleMember -AOTName " + $securityRole.AOTName + " -AxUserID " + $user.AxUserID $lineToWrite | Out-File -FilePath $filePath -Append; } }