Monday, August 6, 2012

Powershell Trick : Make an email ID using user Environment Variables and powershell

Hi,

Last Friday i was working on one script, that script send an email to a “Service Desk” system. But that script required an email id of the sending user. So that if someone replied to that email from service desk software it should goes to users email id. This is global script and we can’t embedded a  hard username value in to this.

Even i don’t want to use “Active Directory” module to this script.

Then i come with an simple idea Winking smile 

I ideas was to use “+” operator and use username  “windows  environments”

We can get a logged in user name of the logged in user by accessing $env:username

I am logged in as user “Aman.Dhally”

06-08-2012 12-34-44

Ok, my domain name for email ids is “Localmail.com”

now we can add both these values “$env:username” and my “domain name” using joining plus “+” operator.

and in a result we go “username + '@Domain name”  and the output will be the complete email id of the user. 

We are saving the result of below to the variable $email so that we can use then anywhere in the script.

$email = "$env:username" + "@localmail.com"
$email

06-08-2012 12-31-41


Finally i got a users email ID Smile 


Thanks


Aman Dhally


spiderman_halping

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.