Skip to content

Instantly share code, notes, and snippets.

@martin-rdz
Created December 7, 2018 18:37
Show Gist options
  • Save martin-rdz/558d473297b6f3c9fa52def0d9e00258 to your computer and use it in GitHub Desktop.
Save martin-rdz/558d473297b6f3c9fa52def0d9e00258 to your computer and use it in GitHub Desktop.
a small watchdog for PowerShell
# Before first PowerShell usage you might have to adapt the policies
# Get-ExecutionPolicy -List
# Set-ExecutionPolicy Unrestricted -Scope CurrentUser
# --------------------------------------------------------------------------------
# This setup is only necessary once
# Insert the password into the empty string below and run script
# powershell creates a text file with the (encrypted credentials, Windows Data Protection API)
# then the watchdog acesses it...
#$secureStringPwd = "test" | ConvertTo-SecureString -AsPlainText -Force
#$secureStringText = $secureStringPwd | ConvertFrom-SecureString
#Set-Content "Mailkey.txt" $secureStringText
#exit
# --------------------------------------------------------------------------------
# define process to watch
$processToCheck = "R2CH"
$process = (Get-Process -name $processToCheck -ErrorAction SilentlyContinue) -eq $null
# get the lacros gmail password
$username = 'sender@gmail.com'
$securePwd = Get-Content "Mailkey.txt" | ConvertTo-SecureString
$credObject = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $securePwd
# setup the mail object
$smtpClient = new-object system.net.mail.smtpClient
$smtpClient.Host = 'smtp.gmail.com'
$smtpClient.Port = 587
$smtpClient.EnableSsl = $true
# alternative get credential $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password");
#$smtpClient.Credentials = [Net.NetworkCredential]($pwdTxt | ConvertTo-SecureString)
$smtpClient.Credentials = $credObject
$sendmail = $false
$outputtext = ''
If (!($process)) {
#Write-Output (Get-Process -name $processToCheck)
$outputtext = "Process is running`r`n"
$netstat_res = netstat -an | find ":7000 "
$netstat_res = ($netstat_res -split '\n')[0]
#Write-Output ($netstat_res).Contains("172.23.159.100")
IF (($netstat_res).contains("192.168.0.11")) {
If (($netstat_res).contains("LISTENING")) {
$outputtext = $outputtext + "Running on correct port and listening`r`n" + $netstat_res
} elseif (($netstat_res).contains("ESTABLISHED")) {
$outputtext = $outputtext + "Running on correct port and connected`r`n" + $netstat_res
}
} elseif (($netstat_res).contains("192.168.0.10")) {
$outputtext = $outputtext + "but on a wrong port`r`n" + $netstat_res
$sendmail = $true
}
} else {
#Start-Process $processToCheck
$outputtext = "Process not running"
$sendmail = $true
}
#$sendmail = $true
if ($sendmail) {
# send mail
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = $username
$emailMessage.To.Add('recipient1')
$emailMessage.To.Add('recipient2')
$emailMessage.Subject = 'watchdog'
$emailMessage.Body = $outputtext
$smtpClient.Send( $emailMessage )
#$smtpClient.Send($username,'radenz@tropos.de','watchdog', $outputtext)
}
Write-Output ("send mail " + $sendmail)
Write-Output $outputtext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment