Right now I have a script that gathers user group names and the associated ManagedBy attribute using powershell.
$test = 'OU=example,DC=example,DC=test'
$test | ForEach {Get-ADGroup -Filter * -Properties ManagedBy -SearchBase $_ } |
Select Name, ManagedBy |
Sort -Property Name |
Out-File C:\test.csv
Results come through however the managedby attribute only supplies the FQDN(Distinguished Name) when all I need is the first CN in the result. In the case below, for example, all I need to display under ManagedBy is "testuser". No CN= or DC= distinguished name paths.
Name ManagedBy
---- ---------
TestGroup_Modify CN=testuser,CN=Users,DC=test,DC=domain,DC=com
I've tried a few things to use "-replace" with some formatting in the select but it ends up removing the first CN.
Select -Property Name, @{n='ManagedBy';e={$_.ManagedBy -replace '^.+?,(CN|DC.+)','$1'}}
results:
Name ManagedBy
---- ---------
TestGroup_Modify CN=Users,DC=test,DC=domain,DC=com
Does anyone know a way of formatting these results or experience working with the ManagedBy attribute of a group? I was trying to figure out a way to somehow link the ManagedBy attribute back to the users SAMAccountName but was unsuccessful/lack the powershell experience. (I think it has something to do with the query retrieving ADGroups and not ADUsers) Thanks in advance for any help I receive on this.
The ultimate goal would be to achieve something like this:
Name ManagedBy
---- ---------
TestGroup_Modify testuser
Update*
Ah okay, after changing the replace I get results like this:
Name ManagedBy
---- ---------
TestGroup_Modify testuser=Users,DC=test,DC=domain,DC=com
This is helpful since it starts with the CN, but since every ManagedBy user will have
'=Users,DC=test,DC=domain,DC=com'
appended, would it be possible to filter this string out from results (since all results will have it) to leave just the user CN or would that be outside the scope of powershell? Thanks for your help.
Edit*
Here's what I ran:
$test = 'OU=example,DC=example,DC=test'
$test | ForEach {Get-ADGroup -Filter * -Properties ManagedBy -SearchBase $_} |
Select -Property Name, @{n='ManagedBy';e={$_.ManagedBy -replace '^CN=(.+?),(CN|OU.+)','$1'}} |
Sort -Property Name |
Format-Table -Property Name, ManagedBy -Force -AutoSize |
Out-File C:\test.csv
Got these results:
Name ManagedBy
---- ---------
TestGroup_Modify testuser=Users,DC=test,DC=domain,DC=com
Just looking for a way to list testuser by itself if possible.