This line: If ($members -contains $user) {
Should be: If ($members -contains $user.distinguishedname) {
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
The script below keeps dropping into the 'Automation has failed...' else statement once a user has been added into a security group. I can see that the user has been added but need the additional check to write the output as these outputs are being used in a workflow in ServiceNow to determine the next action to be taken.
I'm not a windows administrator and don't use PS that often but the logic looks sound to me. Wondering if I need to 'wait' a certain amount of time to do the additional check to give the domain controllers chance to replicate? Hench it coming back as a failure?
Appreciate any assistance.
# Get the user and group information
$user = get-aduser $UserName -server $UserController -credential $MyCredentials
$group = get-adgroup $GroupName -server $DomainController -credential $MyCredentials
$members = Get-ADGroupMember -server $DomainController -credential $MyCredentials -Identity $GroupName -Recursive |Select -ExpandProperty distinguishedName
# Check group membership
If ($members -contains $user) {
Write-Host("User exists in the group")
}
Else
{
Set-ADObject -identity $group -add @{member=$user.DistinguishedName} -server $DomainController -credential $MyCredentials
If ($members -contains $user) {
Write-Host("User successfully added to group")
}
Else {
Write-Host("Automation has failed, user not added to group")
}
}
This line: If ($members -contains $user) {
Should be: If ($members -contains $user.distinguishedname) {
This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.
Comments have been turned off. Learn more