Get Latest sAMAccountName/UserPrincipleName from AD using Microsoft AD Spoke

Chethan Sharma
Giga Contributor

Hello,

 

We are building a automation flow to create user in AD using Microsoft AD Spoke . Using the spoke action 'Create user' we are able to create a user in AD by providing User name(Mandatory input) . As user name is unique filed my requirement is to query AD to get the latest sAMAccountName/UserPrincipleName from AD and increment the value by one and pass that as username in  'Create user' spoke action.

 

We verified that there is no OOB spoke action to retrieve a latest sAMAccountName/UserPrincipleName from AD , so we are trying to build a Custom action to run a PowerShell command to retrieve the latest sAMAccountName/UserPrincipleName.

 

The below powershell command when I run it from server box PowerShell ISE will retrieve me the latest record :

 

Get-ADUser -credential $cred -Filter {sAMAccountName -Like "PCTR*"} -Properties sAMAccountName | Select sAMAccountName, UserPrincipalName | Sort-Object sAMAccountName -Descending | Select -First 1;   

 

But when I try to incorporate this in costume action getting the below error:

Please let us know if anyone have any better solution to achieve this or if I need to follow any additional steps to overcome this error 

 

ChethanSharma_0-1671692712603.png

Powershell Command :

 

ChethanSharma_1-1671692804294.png

 

Error while executing test Run:

ChethanSharma_2-1671692935935.png

 

 

 

1 REPLY 1

Richard Hine
Tera Guru
Tera Guru

Chethan,

 

I am assuming you have pulled the MID server log files to see if there is further details? My guess would be that the account you are attempting to run the command as when using the flow (likely the MID server account) does not have the same permissions to execute the command as you do..

 

Happy to help if I can,

 

Richard