Solved

Powershell script that sends an email upon login with the username

Posted on 2015-02-24
7
50 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This article will help you understand what HashTables are and how to use them in PowerShell.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now