Find orphaned foreign security principals and remove them from groups

active-directorygroupswindows-server-2008-r2

We have a Windows 2008 R2 Domain with multiple outgoing trusts.
In this domain we add the users from the trusted domains to domain local security groups.
It seems that some or all deleted accounts from the foreign domains remain in the groups after deletion (from the originating domain), leaving a SID in the group which cannot be resolved.

What is the easiest way to find such accounts and remove them from the groups?

Best Answer

I dont have a script handy. But personally I'd do something like this.

  1. Enumerate objects in the cn=foreignsecurityprincipals,dc=doamin,dc=com container to build list of FPO SIDs
  2. Use a batch file to iterate through each and do a psgetsid.exe (sysinternals) against SID to see if it resolves. 0 means can resolve and -1 means unavailable or possibly even cant contact correct DC
  3. If %errorlevel% is -1 write that "bad" SID to another file

you can go through the SIDs reported as -1 to see if you really want to delete the FPO represented. Deleting the FPO will clean up any membership its used in. YOu can use DSRM to script deleting the FPO later if desired using a for loop to iterate txt file.