简体   繁体   中英

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.

在这里,$user 是您要查询经理信息的用户

(get-aduser (get-aduser $user -Properties manager).manager).samaccountName

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"
Get-ADUser -Filter * -Properties Name,SamAccountName,AccountExpirationDate,Manager | select Name,SamAccountName,AccountExpirationDate,@{N='Manager';E={(Get-ADUser $_.Manager).sAMAccountName}} | Export-Csv "Userdata.csv"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM