Solved

Powershell script that sends an email upon login with the username

Posted on 2015-02-24
7
60 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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  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

Suggested Solutions

Set OWA language and time zone in Exchange for individuals, all users or per database.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

713 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