Confirm when account was removed from group with PS

can someone help me modify this so when an account that is in the users.txt file is not in the group , it will output an error that specifies the samaccountname that was not in the group ?

Instead of the generic error

Remove-ADGroupmember : The specified account name is not a member of the group
At C:\removefromgroups.ps1:3 char:1
+ Remove-ADGroupmember -identity $_ -member $User -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (MY Test group:ADGroup)

Foreach ($User in GC C:\Users.txt){
GC C:\groups.txt | % {
Remove-ADGroupmember -identity $_ -member $User -Confirm:$false
      }
}

Open in new window

LVL 2
MilesLoganAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
I don't remember hearing about any change in behavior around this cmdlet, but on a couple machines I've tested specifying a user that isn't a member of the group doesn't produce any error.
I've only tested on PS versions 5.0 and 5.1.

Typically I would expect to have to deal with such an exception by using a Try/Catch statement, and it might require the Remove-ADGroupMember cmdlet to have the -ErrorAction parameter set to Stop so that the error becomes terminating.
1
Jose Gabriel Ortega CastroCEOCommented:
As footech says:

Foreach ($User in GC C:\Users.txt){
GC C:\groups.txt | % {
    try{
        Remove-ADGroupmember -identity $_ -member $User -Confirm:$false -ea stop
    }
    catch{
        $error=$_.exception.ErrorMessage
        write-output "The user with samAccountName: $_  was not in the group. ErrorMessage:$error" 
    }
      }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Daryl BamforthTechnical ExpertCommented:
The catch for that error is


catch [Microsoft.ActiveDirectory.Management.ADException] {

}

Open in new window


You can find the exception by using
catch {
    write-output "Unexpected Exception: $($error[0].Exception.GetType().Fullname)"
    $UnexpectedException = $error[0].Exception.GetType().Fullname

}

Open in new window


Ideally every exception should be handled correctly, if you add the above as a final catch you will be able to build up your scripts to handle any eventuality.
0
MilesLoganAuthor Commented:
Thank you Jose , that is what I was looking for .
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.