Category: AX Powershell

Copy AX Security Roles to Another User Using Powershell

Copy AX Security Roles to Another User Using Powershell

As anyone who is an admin for Dynamics AX 2012 (R2 in my case) can attest, assigning user security roles can be a monotonous chore, especially if you have a high number of users that need specific roles.

Initially when my company implemented AX, user security roles were added via the AX client.  User to copy on one side, user to add on the other.  Click assign roles for each one you need to copy.

Ugh.

About a year later I was looking into other options and found that while there isn’t much Powershell for AX 2012, one of the things you can do is add or remove security roles.  Tell me more!

Let’s start, shall we?

Below in this first image, we have one Leia Organa, new hire to our company.

Oddly, according to HR she needs to have the same roles as her brother:

At this point, you need to be on a computer that is configured with an AX connection to the AOS service, AX management utilities installed, and the user you are running this as in Windows needs to have security or system admin permissions in the AX environment.  In my case, I run this directly on the AOS server.  YMMV

Anyway, the code:

#Load AX Powershell functions
. "C:\Program Files\Microsoft Dynamics AX\60\ManagementUtilities\Microsoft.Dynamics.ManagementUtilities.ps1"

$axuserold = read-host 'Enter the AD username of the person to copy from'
$axusernew = read-host 'Enter the AD username of the person to copy to'

get-axsecurityrole -axuserid $axuserold | foreach ($_) {add-axsecurityrolemember -axuserid $axusernew -aotname $_.aotname}

What it does:

Step 1:  Enter the Active Directory user name of the person to copy from (pauses for input):

Step 2:  Enter the Active Directory user name of the person to copy to (pauses for input):

Hit enter, and once completed the window goes away.

What does it do?

It uses the Powershell command “get-axsecurityrole” for a specific user you mention, and then using the “foreach” option Powershell has it uses the command “add-axsecurityrolemember” to add that role to the user you specify.  It loops through each role the originated user has and adds it in.

Finally, Leia is good to go:

This has saved me countless hours of tediously adding roles to new people to the organization.  Hopefully this can help you out as well.

***DISCLAIMER*** I am not responsible for this breaking or damaging any of your stuff.  Copyrights belong to their original owners***