Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

A script to pull manager email addresses from a list of users

$
0
0

Hello all,

I am trying to pull a list of managers from a csv list of usernames. I can do it with two separate foreach loops and some text file massaging in between stages. I would like to get it to the point that I can get the email addresses as output without needing to do the in between text file cleanup.

$b = Import-Csv "C:\temp\userlist\accountaccess" -header ("samaccountname")
foreach ($user in $b) {
get-aduser $user.samaccountname -properties manager|Select manager | out-file C:\temp\userlist\manager.txt -width 150 -append
}


this generates a list of users 


manager                                                                                                                                              
-------                                                                                                                                              
CN=lastname1\, firstname1,OU=Users,DC=domain,DC=com                                 



manager                                                                                                                                              
-------                                                                                                                                              
CN=lastname2\, firstname2,OU=Users,DC=domain,DC=com                                                                          

I format that file down to a minimum:                                                                                                                               

"CN=lastname1\, firstname1,OU=Users,DC=domain,DC=com"
"CN=lastname2\, firstname2,OU=Users,DC=domain,DC=com"

then run this:

$manager = Import-Csv "C:\temp\userlist\manager.txt" -header ("distinguishedname")
foreach ($user in $manager) {
get-aduser $user.distinguishedname -properties EmailAddress | select EmailAddress
}

And that gives me a list of email addresses ouput to the screen.

I'd like to request assistance with merging those operations into one loop that outputs the text file containing the email addresses if thats possible.

I was thinking along the lines of processing each users manager and getting the email address then appending that to a file, but I'm stuck as to how to get there.

Thanks for any input,

John


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>