Tuesday, August 28, 2012

Get the Email Address of the Current User

Ran into an issue with a scheduled job that was sending emails.  Our SMTP host does not allow messages where the FROM address does not belong to the user submitting the messages (so I can’t spoof my boss’s email).  The script I was scheduling will run on different servers using different credentials.  V1 of my script hard coded the FROM address, and those messages were being rejected by our SMTP host.  Instead of updating the scripts so that the FROM address matched the credentials of the Scheduled Job, I used this script snippet to discover the email address:


### Get the Email address of the current user



    ### Get the Distinguished Name of the current user

    $userFqdn = (whoami /fqdn)


    ### Use ADSI and the DN to get the AD object

    $adsiUser = [adsi]("LDAP://{0}" -F $userFqdn)


    ### Get the email address of the user

    $senderEmailAddress = $adsiUser.mail[0]




    Throw ("Unable to get the Email Address for the current user. '{0}'" -f $userFqdn)



No comments: