query ad for users sam only & managers sam only?

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Tim Pringle Tim Pringle 1 year, 9 months ago.

  • Author
    Posts
  • #22433
    Profile photo of Jake Sully
    Jake Sully
    Participant

    Howdy,

    I have been trying to come up with a way to get the samaccountname for all users in AD and then their managers samaccountname and export to a saved directory, then send an email out with a notice the scheduled task runs.

    I have gotten as far as the script below, and it does give me the samaccountname of the users and manager, but I don't need the displayname, or I should say the people that want this don't want the displayname included.

    I then can't figure out a way to send an email that the windows 2012 scheduled task has run. Can anyone help me?

    I'd like to email out the scheduled task results as well as the csv file if possible?

    Get-ADUser -Server sandiegodc1 -Filter {mail -like "*"} -Properties * ` |
    select DisplayName, @{Name='Manager';Expression={(Get-ADUser $_.Manager).sAMAccountName}},
    sAMAccountName |
    Export-CSV "c:\temp\CaliOffice_Properties.csv"

  • #22437
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    Put this at the end of your script:

    $Attachment = 'c:\temp\CaliOffice_Properties.csv'
    $Attachment | Send-MailMessage -SmtpServer  -Body 'Your Text Here' -From  -To  -Subject 'Subject Text Here'
  • #22686
    Profile photo of Jake Sully
    Jake Sully
    Participant

    Thx! Do I have to include the "Display Name" or can I query for only the sAMAccountName of the users and the sAMAccountName of the managers for each user?

  • #22687
    Profile photo of Jake Sully
    Jake Sully
    Participant

    Thx! Do I have to include the "Display Name" or can I query for only the sAMAccountName of the users and the sAMAccountName of the managers for each user?

  • #22688
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    You can exclude the Display Name attribute.

    You might also need to add -NoTypeInformation to your Export-CSV command, so that it can be identified when opened in programs such as Excel.

You must be logged in to reply to this topic.