LOGO

PowerShell Login Detection and Email Alerts

April 8, 2013
PowerShell Login Detection and Email Alerts

Automating Email Notifications with Windows Task Scheduler

The Windows Task Scheduler offers the capability to dispatch emails automatically, either on a predetermined schedule or as a reaction to defined system events.

However, the built-in email functionality of the Task Scheduler is often considered inadequate for many users’ needs.

Leveraging Built-in Windows Tools

Previously, we demonstrated a method for achieving this through the utilization of external software.

Nevertheless, many users prefer to avoid installing additional programs when equivalent functionality can be accessed using tools already present within the Windows operating system.

This approach streamlines the process and reduces the reliance on third-party dependencies.

Alternative Methods for Email Automation

Instead of relying on the Task Scheduler’s direct email capabilities, consider utilizing PowerShell scripts.

PowerShell provides a robust and flexible way to construct and send emails directly from the command line, which can then be triggered by a scheduled task.

This method offers greater control over email content, formatting, and delivery options.

Furthermore, it eliminates the need for external email clients or utilities.

Benefits of Using Native Windows Tools

  • Reduced software dependencies.
  • Enhanced control over email parameters.
  • Streamlined automation process.
  • Improved system security.

By harnessing the power of PowerShell and the Windows Task Scheduler, users can effectively automate email notifications without the complexities of third-party solutions.

PowerShell and Task Scheduler Integration

Initially, a scheduled task must be established. To begin this process, simultaneously press the Win + R keys to open the Run dialog box. Then, type “control schedtasks” and press Enter.

how-to-use-powershell-to-detect-logins-and-alert-through-email-1.jpg

Upon opening Task Scheduler, select the “Create Task…” link to initiate task creation.

how-to-use-powershell-to-detect-logins-and-alert-through-email-2.jpg

Within the General pane, assign a name and description to the task. It is also recommended to select the option “Run whether user is logged on or not.”

how-to-use-powershell-to-detect-logins-and-alert-through-email-3.jpg

Next, navigate to the Triggers tab and add a new trigger. Configure this trigger to activate upon logon, which can be chosen from the available dropdown menu.

how-to-use-powershell-to-detect-logins-and-alert-through-email-4.jpg

Subsequently, switch to the Actions tab and add a new action. Choose the option to start a program, specifying “powershell” as the program to be launched. Then, paste the following command into the Arguments text box.

-Command “Send-MailMessage -From "someone@gmail.com" -To "someone@gmail.com" -Body "Someone Just Logged In" -Subject "LOGIN"  -SmtpServer “smtp.gmail.com” -Port 587 -Credential $(New-Object System.Management.Automation.PSCredential (someone@gmail.com, $(ConvertTo-SecureString "PASSWORD" -AsPlainText -Force))) –UseSsl”

It’s important to replace all instances of someone@gmail.com with a valid GMail username and PASSWORD with the corresponding password. Using a different SMTP server is also an option.

how-to-use-powershell-to-detect-logins-and-alert-through-email-5.jpg

On the Conditions tab, deselect the “Start the task only if the computer is on AC power” option. This ensures email notifications are received even when using a laptop on battery power.

how-to-use-powershell-to-detect-logins-and-alert-through-email-6.jpg

Press the OK button to save the task. Email notifications will now be sent whenever a user logs into the computer.

how-to-use-powershell-to-detect-logins-and-alert-through-email-7.jpg
#PowerShell#login detection#email alerts#security monitoring#system administration#scripting