Quantcast
Viewing all articles
Browse latest Browse all 175888

PowerShell to extract User, Roles, Duties and Privileges

I came across a useful Windows PowerShell script to extract users and roles with the intention of generating a script to create those same users and roles (in another AX environment). I've used it just for management reporting but is it possible to also extract the duties and privileges?

Here is the script that I have so far:

# 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\UserDetails.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

# Writes a heading line to the file
$lineToWrite = "UserID,Name,Company,Enabled,Role"
$lineToWrite | Out-File -FilePath $filePath -Append;

# Loop through the user list
foreach($user in $userList)
{
# 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 = $user.AxUserID + "," + $user.Name + "," + $user.Company + "," + $user.Enabled + "," + $securityRole.Name
$lineToWrite | Out-File -FilePath $filePath -Append;
}
}


Viewing all articles
Browse latest Browse all 175888

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>