[X++] class AxaptaUserManager, PowerShell and BusinessConnector

Today I stumpled on a little inconspicuous class called AxaptaUserManager.
Taking a closer look to the methods, there are some useful ones.
Axapta User manager methods

Now to combine the class with some useful functionallity, for example we can get all security roles assigned to the current user. To make it even more interesting for you, I did that using PowerShell and the Business Connector!

#Created by Robin Kretzschmar, 15.07.2015
Clear-Host

#Put the path to your config file here, if you want to use one:
$configFile = "\\NETWORKPATH\TO\CONFIG\My_Config_File.axc"
$array = New-Object System.Collections.ArrayList
Add-Type -Path "C:\Program Files\Microsoft Dynamics AX\60\BusinessConnector\Bin\Microsoft.Dynamics.BusinessConnectorNet.dll"

Function AxLogoff
{
    [void]$ax.Logoff()
}

Function AxLogon
{
    try
    {
        $script:ax = new-object Microsoft.Dynamics.BusinessConnectorNet.Axapta
        #Either use it without parameters to use your current client config:
        #$ax.logon('','','','','','')
        #or with a config file to use with any other AX instance:
        $ax.Logon('','','', $configFile)
    }
    catch
    {
        throw 'Logging to AX failed.'
    }
}

Function GetSystemData
{
    #Some base data from the system in case you want to print them out also:
    $XApplication = $ax.CreateAxaptaObject("xApplication")
    $xSession = $ax.CreateAxaptaObject("XSession")   
    Write-Host "BuildNo: " $XApplication.Call("buildNo")
    Write-Host "AOSName: " $xSession.call("AOSName")   
    Write-Host "AOS Port " $ax.CallStaticClassMethod("Session", "getAOSPort")
    Write-Host "DB-Schema: " $ax.CallStaticClassMethod("xSession", "getDbSchema")
    Write-Host "Session Login Date: " $xSession.call("loginDateTime")  
    $script:curUserId = $xSession.call("userId")
    Write-Host "UserId: " $curUserId  
    Write-Host "Computer Name: " $ax.CallStaticClassMethod("xGlobal", "computerName")
}

Function GetUserRoles
{
    $script:array = $ax.CallStaticClassMethod("AxaptaUserManager", "getAXUserRoles")
}

Function PrintRoles
{
    Write-Host "Roles for user'" $curUserId "':"
    Write-Host ($array | Format-Table | Out-String)
}

#Execute functions

AxLogon
GetSystemData
GetUserRoles
PrintRoles
AxLogoff

The output will be:
AxaptaUserManagerOutcome Sorry but I had to mask some data.

Enjoy playing with PowerShell, BusinessConnector and AxaptaUserManager.

I'm always happy to read some cool snippets in the comments!

Robin

Software Engineer / Application Developer

Subscribe to robsCode

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!