Powershell script that sends an email upon login with the username

There is a need for a Powershell script which will trigger on login, which will send the name of the username which just logged in, to a specified email address.
100questionsAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
RobSampsonConnect With a Mentor Commented:
Hi, in a simple form, this should work.

Regards,

Rob.

$strUsername = get-content env:username
$strComputer = get-content env:computername
$strSMTPServer = "yourmailserver"
$strFrom = "logins@domain.com"
$strTo = "admin@domain.com"
$strSubject = "Login for $strUsername on $strComputer"
$strBody = "User $strUsername logged in on $strComputer"
Send-MailMessage -To $strTo -From $strFrom -Subject $strSubject -Body $strBody -SmtpServer $strSMTPServer

Open in new window

0
 
100questionsAuthor Commented:
Will this send the username of the user that just logged in?  Or does one have to specify the user name?
0
 
RobSampsonCommented:
It will automatically retrieve the username and computername of the user that just logged in, and send the email.
0
Building an Effective Phishing Protection Program

Join Director of Product Management Todd OBoyle on April 26th as he covers the key elements of a phishing protection program. Whether you’re an old hat at phishing education or considering starting a program -- we'll discuss critical components that should be in any program.

 
100questionsAuthor Commented:
Thank you, I will try it and report back.
0
 
100questionsAuthor Commented:
This does not seem to work.    Which variables do I input?
0
 
RobSampsonCommented:
I wonder if you might have anti-virus software that is blocking the email from sending?

We use McAfee anti-virus, and need to enable the "mass mail" rule to allow a machine to send emails through the SMTP server.

The variables you need to change are these three:
$strSMTPServer = "yourmailserver"
$strFrom = "logins@domain.com"
$strTo = "admin@domain.com"

where the $strSMTPServer is the name of your SMTP server (that may need to allow relay from your machines sending email), and the other two are your "from" and "to" addresses.

Rob.
0
 
100questionsAuthor Commented:
I will try again.. and report back.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.