[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 68
  • Last Modified:

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.
0
100questions
Asked:
100questions
  • 4
  • 3
1 Solution
 
RobSampsonCommented:
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
A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now