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

Welcome Forums General PowerShell Q&A 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

 
Participant
2 years, 10 months ago.

  • Author
    Posts
  • #34883

    Participant
    Points: 0
    Rank: Member

    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=@("ou=testfolder,dc=test,dc=net,dc=int")
     
    $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

    Participant
    Points: 0
    Rank: Member

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

    $props = @('Name', 'mail', 'manager')
    $ous=@("ou=test,dc=abc,dc=test,dc=com")

    $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

    Participant
    Points: 0
    Rank: Member

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

  • #34887

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

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

The topic ‘have a script that works but when I modify it will not work confused!!’ is closed to new replies.