Intelli-Seeker
asked on
Add Manager to Get-ADUser script without the full cn
I had this question after viewing Powershell: Output displayname of "Manager" attribute when querying Active Directory.
I created the script below which is really great, but I want to only have the manager's name, not the entire CN. (I redacted the actual domain and OU from the searchbase). I could really use some assistance on figuring out where to put the syntax from the above Expert's post in my script below to make it work properly. I've tried several different things and am stumped.
I created the script below which is really great, but I want to only have the manager's name, not the entire CN. (I redacted the actual domain and OU from the searchbase). I could really use some assistance on figuring out where to put the syntax from the above Expert's post in my script below to make it work properly. I've tried several different things and am stumped.
Import-Module Activedirectory
Get-ADUser -Filter * -Properties DisplayName,EmployeeID,mail,Manager,memberof -searchbase 'OU=Users,OU=OU,DC=DOMAIN,DC=local' | % {
New-Object PSObject -Property @{
UserName = $_.DisplayName
EmployeeID = $_.EmployeeID
Email = $_.mail
Manager = $_.manager
Groups = ($_.memberof | Get-ADGroup | Select -ExpandProperty Name) -join ","
}
} | Select UserName,EmployeeID,Email,Manager,Groups
ASKER
I received this error when running the script.
At line:7 char:45
+ Manager = $_.manager -split "CN=|,OU=")[1]
+ ~
The hash literal was incomplete.
At line:2 char:131
+ ... ,DC=local' | % {
+ ~
Missing closing '}' in statement block.
At line:7 char:45
+ Manager = $_.manager -split "CN=|,OU=")[1]
+ ~
Unexpected token ')' in expression or statement.
At line:7 char:47
+ Manager = $_.manager -split "CN=|,OU=")[1]
+ ~
Missing type name after '['.
At line:9 char:7
+ }
+ ~
Unexpected token '}' in expression or statement.
At line:10 char:1
+ } | Select UserName,EmployeeID,Email, Manager,Gr oups
+ ~
Unexpected token '}' in expression or statement.
At line:10 char:3
+ } | Select UserName,EmployeeID,Email, Manager,Gr oups
+ ~
An empty pipe element is not allowed.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordE xception
+ FullyQualifiedErrorId : IncompleteHashLiteral
At line:7 char:45
+ Manager = $_.manager -split "CN=|,OU=")[1]
+ ~
The hash literal was incomplete.
At line:2 char:131
+ ... ,DC=local' | % {
+ ~
Missing closing '}' in statement block.
At line:7 char:45
+ Manager = $_.manager -split "CN=|,OU=")[1]
+ ~
Unexpected token ')' in expression or statement.
At line:7 char:47
+ Manager = $_.manager -split "CN=|,OU=")[1]
+ ~
Missing type name after '['.
At line:9 char:7
+ }
+ ~
Unexpected token '}' in expression or statement.
At line:10 char:1
+ } | Select UserName,EmployeeID,Email,
+ ~
Unexpected token '}' in expression or statement.
At line:10 char:3
+ } | Select UserName,EmployeeID,Email,
+ ~
An empty pipe element is not allowed.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordE
+ FullyQualifiedErrorId : IncompleteHashLiteral
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked perfect! Another related question - I passed the output on to the person that requested the csv file. He asked if there was a way to have it list all the groups that a person is in without the commas. In Excel, if a person belongs to multiple groups (which is why we are working on this script) it will show the groups in one column separated by commas. Is there a way to separate the groups into rows in the csv using powershell without manipulating the file after the fact? It could look something similar to what I have in the screenshot.
ASKER
This was a great solution to add a manager to an existing script. Thanks for your assistance.
Sorry, I forgot about the prior comment.
You can't really make a .CSV like in your screenshot because then it's not really a .CSV. In a .CSV, each row should have complete information. However, bending the rules a little bit to make things more easily viewable in Excel, here's a couple options:
1) change the join character for the groups to a newline - "`n" (it's like using Alt-Enter in a cell).
2) another way I've seen this handled is to duplicate the info in the other fields and have each group be its own row.
I'm sure you could also do it like in your screen shot, but it'd be a bit more complex.
You can't really make a .CSV like in your screenshot because then it's not really a .CSV. In a .CSV, each row should have complete information. However, bending the rules a little bit to make things more easily viewable in Excel, here's a couple options:
1) change the join character for the groups to a newline - "`n" (it's like using Alt-Enter in a cell).
2) another way I've seen this handled is to duplicate the info in the other fields and have each group be its own row.
Import-Module Activedirectory
Get-ADUser -Filter * -Properties DisplayName,EmployeeID,mail,Manager,memberof -searchbase 'OU=Users,OU=OU,DC=DOMAIN,DC=local' | % {
$user = $_
$user.memberof | Get-ADGroup | Select -ExpandProperty Name | % {
New-Object PSObject -Property @{
UserName = $user.DisplayName
EmployeeID = $user.EmployeeID
Email = $user.mail
Manager = ($user.manager -split "CN=|,OU=")[1]
Groups = $_
}
}
} | Select UserName,EmployeeID,Email,Manager,Groups
I'm sure you could also do it like in your screen shot, but it'd be a bit more complex.
ASKER
That worked exactly the way I wanted it to. Thanks!
Open in new window