have a script that works but when I modify it will not work confused!!

This topic contains 4 replies, has 2 voices, and was last updated by  brian catlin 2 years, 4 months ago.

  • Author
  • #34883

    brian catlin

    I was able to piece together a script. This script will query the AD and return a list which is needed. The problem we had was when we used manager it returned this very long list and (the entire OU path), and what we really wanted was the managers email address.

    I searched and found some code that would give the samaccountname. I figured easy enough to change that to the attribute "mail" but when I do modify it nothing is returned for manager.

    we was able to use the script below and then HR concatenated the end of the email onto the samaccountname in excel so we found a work around. However, as a young powershell user learning the basics its driving me crazy that I cannot get this to just give the email =(

    any ideas?

    Below is the code.

    $props = @('employeeNumber', 'Name', 'mail', 'manager')                                  

    $ous | ForEach {
      Get-ADUser -Properties $props -Filter 'Enabled -eq $true' -SearchBase $_ | Select employeeNumber, name, mail, @{Name='Manager';Expression={(Get-ADUser $_.Manager).sAMAccountName}}
    } |export-csv -NoTypeInformation c:\scripts\HRdata.csv


    The portion that was new to this script was the following @{Name='Manager';Expression={(Get-ADUser $_.Manager).sAMAccountName}}

    I tried as stated earlier to change it to @{Name='Manager';Expression={(Get-ADUser $_.Manager).mail}}

    However that would not work for some reason.

    Everything else I have been using for awhile now to pull up information on attributes.

  • #34884

    Yuan Li

    Try this. You have to use -property mail in your expression otherwise by default it won't show this

    $props = @('Name', 'mail', 'manager')

    $ous | ForEach {
    Get-ADUser -Properties $props -Filter 'Enabled -eq $true' -SearchBase $_ | Select employeeNumber, name, mail, @{Name='Manager';Expression={(Get-ADUser $_.Manager).sAMAccountName}},@{Name='Manager email';Expression={(Get-ADUser $_.Manager -Properties mail).mail}} | ft

  • #34885

    Yuan Li

    Also, you can just use get-aduser | gm to list all the properties

  • #34887

    brian catlin

    Thank you . That worked. I just didnt fully understand some of the script I was using. that cleared it up for me!

    Back to studying my don Jones powershell books! the script I am working with is way ahead of where my studies are.

    Thanks again Yuan!

  • #34888

    brian catlin

    edited this since your above answer actually answers all my questions in full once I thought about it.

You must be logged in to reply to this topic.