I am writing a script to get details of AD Objects. This shows a Treeview, similar to that in AD Users & Computers, but shows Deleted Objects as well.
I am trying to make it as versatile as possible, so using Get-ADObjects, rather than including separate searches for Users, Groups, etc.
I have used the -IncludeDeletedObjects option so I can cope with these as well as existing objects.
Then I use the LastKnownParent to show where the Deleted Object was (since once deleted it is in DeletedObjects of course).
However, when I try to use the MemberOf switch, this includes Group Memberships for other Objects (such as Users, Workstations and other Groups), but does not include any information for Group Memberships for Deleted Objects.
I have tested this with a User object. When Deleted the object is found in DeletedObjects and does not appear to be in any Groups. Admittedly, it is not showing in any of the relevant Groups either. Once the User Object has been restored, it is back in the Groups as before it was Deleted.
Is there any way to show the Groups of which an object was a member before deletion whilst it is ‘deleted’ - in the same way that LastKnownParent indicates where the object was before it was deleted? As the object reappears in the correct Groups once it is Restored, AD must be storing that information somewhere.
Maybe it works by GUIDs, as for File Access Rights for Deleted Objects?
The basic command I am using is:
$ObjectDetail = @(Get-ADObject -Identity "$SelectedObject" -IncludeDeletedObjects -Properties * | select *)
$GroupList = @($ObjectDetail.MemberOf)
Then I write out the various Properties to a RichTextBox:
$GroupList | %{
$richtextbox.AppendText($_.substring(3).split(",")[0])
$richtextbox.AppendText("`r")
}
The last part just shows the name of the Object rather than the complete DN and then writes each one on a new line.
(Incidentally, as I am using the ‘Identity’ switch the first search probably doesn’t actually need to produce an array)