Solved

Powershell script that sends an email upon login with the username

Posted on 2015-02-24
7
58 Views
Last Modified: 2015-09-09
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.
0
Comment
Question by:100questions
  • 4
  • 3
7 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 40629218
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
 

Author Comment

by:100questions
ID: 40629227
Will this send the username of the user that just logged in?  Or does one have to specify the user name?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40629240
It will automatically retrieve the username and computername of the user that just logged in, and send the email.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:100questions
ID: 40631733
Thank you, I will try it and report back.
0
 

Author Comment

by:100questions
ID: 40813175
This does not seem to work.    Which variables do I input?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40816764
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
 

Author Comment

by:100questions
ID: 40816816
I will try again.. and report back.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question