Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

powershell script is not working in flow designer

chandan2212
Tera Contributor

Hi Team, 

 

This script is not working in the custom application:

 

We are giving the user name.

 

 

Import-Module ActiveDirectory -WarningAction SilentlyContinue

# Try to get the username from the environment variable
if (Test-Path env:\SNC_userName) {
$username = $env:SNC_userName
} else {
Write-Host "Environment variable 'SNC_userName' not found. Please provide a username."
exit
}

# Clean up the username if needed
$username = $username -replace "%27", "'"

# Validate the username
if ([string]::IsNullOrWhiteSpace($username)) {
Write-Host "Username is empty or invalid."
exit
}

# Get the user object
$userObj = Get-ADUser -Identity $username -Properties MemberOf

if ($null -eq $userObj) {
Write-Host "User '$username' not found in Active Directory."
exit
}

# Loop through each group and remove the user
foreach ($group in $userObj.MemberOf) {
Remove-ADGroupMember -Identity $group -Members $userObj -Confirm:$false
Write-Host "Removed $username from group $group"
}

 

Can you advise how to correct it?

 

Thanks and regards,

Chandan 

 

 

 

13 REPLIES 13

Hi @chandan2212 ,

I will share the Zoom link when I get bandwidth today.

 

Regards,

Vishal

Hi @Vishal_Jaiswal , 

 

 

It is not possible to join the zoom call , Can ypu suggest once.

 

 

Thanks and regards,

Chandan Patra 

Najmuddin Mohd
Mega Sage

Hello @chandan2212 ,

Can you first try this by running from the Server in which MID Server is installed.
This may give more insights

Regards,
Najmuddin

Hi @Najmuddin Mohd ,

 

I have done but still it is not working.

 

Thanks, and regards,

Chandan Patra