Get Latest sAMAccountName/UserPrincipleName from AD using Microsoft AD Spoke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2022 11:13 PM
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
Powershell Command :
Error while executing test Run:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2023 02:39 PM
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