Powe shell is not working and showing error

chandan2212
Tera Contributor

Hi Team ,

 

This PowerShell-Script is not working properly :

 

# Import Active Directory module
Import-Module ActiveDirectory

# Get username from arguments passed by ServiceNow
$UserName = $args[0] # ServiceNow passes input as arguments

try {
# Get AD user object with group memberships
$adUser = Get-ADUser -Identity $UserName -Properties memberOf

if ($adUser) {
foreach ($group in $adUser.memberOf) {
# Exclude default groups
if ($group -notlike "*Domain Users*" -and $group -notlike "*Authenticated Users*") {
Remove-ADGroupMember -Identity $group -Members $adUser -Confirm:$false
}
}
# Return JSON for ServiceNow
Write-Output "{""status"":""success"",""user"":""$UserName"",""message"":""Removed all group memberships except defaults""}"
} else {
Write-Output "{""status"":""error"",""user"":""$UserName"",""message"":""User not found in AD""}"
}
}
catch {
Write-Output "{""status"":""error"",""user"":""$UserName"",""message"":""Failed to process user: $_""}"
}

 

Showing error :{"status":"error","user":"","message":"Failed to process user: Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again."}

 

 

Can anyone suggest to me how to  reslove this problem.

 

 

thanks ,

Chandan

1 REPLY 1

Viraj Hudlikar
Tera Sage
Tera Sage

Hello @chandan2212 

The error "The argument is null" means your script is starting, but it isn't receiving the username from ServiceNow. The variable $UserName (which is $args[0]) is empty.

You need to go back and check the ServiceNow activity (in your Workflow, Flow Designer) that is launching this PowerShell script.

 

 

You can also validate what parameters are passed from ECC Queue.

1) Navigate to ECC Queue in ServiceNow.

2) Find the Output record use this filter on list (type - PowerShell, state - Error, created- date on which this issue occured).

3) Examine the Payload field of this Output record. This is the XML/JSON that ServiceNow sent to the MID Server. You must ensure the user name is present in the <parameters> section.

 

4) If the value is empty (e.g., value=""), then ServiceNow failed to pass the name.

 

If my response has helped you, hit the helpful button, and if your concern is solved, do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.