Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return manager's samacountname for users

I've got a requirement to create a CSV file of all active users from AD including the line manager attribute, however I need the line managers sAMAccountName, not the cn. Here is what I have so far:

Get-ADUser -server server_ip -Filter { mail -like "*" -and ObjectClass -eq "user" } `
  -SearchBase "OU=Active Users,DC=eu,DC=ad,DC=some_company,DC=com" `
  -Properties objectGUID,displayName,office,division,department,employeeNumber,
    employeeID,mobilePhone,officePhone,ipphone,title,givenName,surname,mail,
    manager,sAMAccountName |
  Export-CSV "EU_AD_Properties.csv"

This returns all the data I want, but gives me the line manager's cn, not the samacountname.

Any ideas?

I've tried this:

Get-ADUser -server server_ip -Filter { mail -like "*" -and ObjectClass -eq "user" } `
  -SearchBase "OU=Active Users,DC=eu,DC=ad,DC=some_company,DC=com" `
  -Properties objectGUID,displayName,office,division,department,employeeNumber,
    employeeID,mobilePhone,officePhone,ipphone,title,givenName,surname,mail,
    @{Label="Manager";Expression={(Get-aduser -filter {sAMAccountName -eq $_.Manager}.sAMAaccountName)}},
    sAMAccountName |
  Export-CSV "EU_AD_Properties.csv"

However this errors out.

like image 588
user3008303 Avatar asked Nov 19 '13 10:11

user3008303


2 Answers

Here ya go $user being the user you are querying for manager information

(get-aduser (get-aduser $user -Properties manager).manager).samaccountName
like image 108
Bill Avatar answered Oct 18 '22 00:10

Bill


You can't create custom properties in arguments to the -Properties parameter, because the current object variable $_ doesn't contain a value at that point (or at least not the value you want). You need to do that in a select statement later in the pipeline, when $_ actually holds the value you need to process. The way you try to create the custom property won't work either:

@{Label="Manager";Expression={(Get-aduser -filter {sAMAccountName -eq $_.Manager}.sAMAaccountName)}}

The filter scriptblock doesn't have an attribute sAMAccountName. What you actually want to do is get the user object for the manager CN and retrieve its sAMAccountName attribute:

@{Label='Manager';Expression={(Get-ADUser $_.Manager).sAMAccountName}}

Also, you don't need the filter ObjectClass -eq "user", because Get-ADUser will return only user objects anyway.

So your pipeline should probably look like this:

Get-ADUser -Server IP -Filter {mail -like "*"} -Properties * `
    -SearchBase "OU=Active Users,DC=eu,DC=ad,DC=some_company,DC=com" |
  select objectGUID, displayName, office, division, department, employeeNumber,
    employeeID, mobilePhone, officePhone, ipphone, title, givenName, surname,
    mail, @{Name='Manager';Expression={(Get-ADUser $_.Manager).sAMAccountName}},
    sAMAccountName |
  Export-CSV "EU_AD_Properties.csv"
like image 21
Ansgar Wiechers Avatar answered Oct 17 '22 23:10

Ansgar Wiechers