9.4.3.5. Using PowerShell Scripts With the Active Directory Connector

The Active Directory connector supports PowerShell scripting. The following example shows a simple PowerShell script that is referenced in the connector configuration and can be called over the REST interface.

This PowerShell script creates a new MS SQL user with a username that is specified when the script is called. The script sets the user's password to Passw0rd and, optionally, gives the user a role. Save this script as openidm/script/createUser.ps1.

    if ($loginName -ne $NULL) {
	[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
	$sqlSrv = New-Object ('Microsoft.SqlServer.Management.Smo.Server') ('WIN-C2MSQ8G1TCA')
 
	$login = New-Object -TypeName ('Microsoft.SqlServer.Management.Smo.Login') ($sqlSrv, $loginName)
	$login.LoginType = 'SqlLogin'
	$login.PasswordExpirationEnabled = $false
	$login.Create('Passw0rd')
	#  The next two lines are optional, and to give the new login a server role, optional
	$login.AddToRole('sysadmin')
	$login.Alter()
} else {
	$Error_Message = [string]"Required variables 'loginName' is missing!"
    Write-Error $Error_Message
    throw $Error_Message
}
    

Now edit the Active Directory connector configuration to reference the script. Add the following section to the connector configuration file (opendim/conf/provisioner.openicf-ad.json).

"systemActions" : [   
    {
        "_scriptId" : "ConnectorScriptName",
        "actions" : [
            {
                "systemType" : ".*ActiveDirectoryConnector",
                "actionType" : "Shell",
                "actionSource" : "@echo off \r\n echo %loginName%\r\n"
            },
            {
                "systemType" : ".*ActiveDirectoryConnector",
                "actionType" : "PowerShell",
                "actionFile" : "script/createUser.ps1"
            }
        ]
    }
]   
    

To call the PowerShell script over the REST interface, use the following request, specifying the userName as input:

$ curl
 --header "X-OpenIDM-Username: openidm-admin"
 --header "X-OpenIDM-Password: openidm-admin"
 --request POST
 "http://localhost:8080/openidm/system/ActiveDirectory/account?_action=script&_scriptId=ConnectorScriptName&loginName=myUser"