Powershell Script for Battery Status Monitoring and information via blat (E-Mail)

1

1

I've found a Powershell Script on this Post: Triggering tasks and running programs when Windows 7 is plugged in or unplugged?

The Script is the following:

New-EventLog -Source BatteryStatusMonitor -LogName Application

Function OnBatteryStatusChange ($NewStatus) {
  If ($NewStatus -eq 1) {
    $EventID = 5001
    $Message = "The computer was unplugged."
  } ElseIf ($NewStatus -eq 2) {
    $EventID = 5002
    $Message = "The computer was plugged in."
  } Else {
    $EventID = 5000
    $Message = "Battery status changed to $NewStatus"
  }
  Write-EventLog -LogName Application -Source BatteryStatusMonitor -EventID $EventID -Message $Message
}

$Query = "select * from __instancemodificationevent within 3 where targetinstance isa 'win32_battery' and targetinstance.batterystatus <> previousinstance.batterystatus"

Register-WmiEvent -Query $Query -Action {OnBatteryStatusChange $Event.SourceEventArgs.NewEvent.TargetInstance.BatteryStatus} -SourceIdentifier "BatteryStatusChange"

For (;;) {}

Now I'd like to alter this script so it will send an E-Mail using the command line E-Mail-Client blat wich can be found here: http://www.blat.net/ I don't care if there will be an E-Mail for each change or just for when it has been plugged in again OR has been plugged out. I tried to do it myself, but my Powershell knowledge ends there. I tried to add a "beep" command after the 3 definitions of the variable $Message to determine if it'd work that way without flooding our mail server, but it didn't do anything. I tried to add the beep also after the Write-Eventlog Command and after the $Query variable, but it all didn't do a thing, too. Otherwise, when I'm adding the beep into the last brackets of the script like:

For (;;) {[console]::beep(500,500)}

It'll beep all the time, wich wouldn't help and would flood the mail-server.

So can someone help me? Is it possible to alter this script in a way, it would invoke a command like blat for sending E-Mails every time, the Power has been plugged in or out or both?

Kind regards,

Kevin van Thiel

Geco Mynx

Posted 2016-10-25T08:45:55.460

Reputation: 91

sounds exciting! i'll take a look at it in the evening (if you don't have an answer until then). Any reason for using blat instead of PowerShells Send-MailMessage? – SimonS – 2016-10-25T12:07:04.997

Answers

0

I found a solution already. It worked just fine when running from the ISE, so I had to make a shortcut and added "PowerShell -noexit -f " in front of the path to the PowerShell-Script, then I check the "Run as administrator" checkbox. After that, it runs just fine using the shortcut. The blat-command has to be in the function just like I thought:

...        
Function OnBatteryStatusChange ($NewStatus) {
      If ($NewStatus -eq 1) {
        $EventID = 5001
        $Message = "The computer was unplugged."
        blat.exe -subject "BatteryStatusMonitor" -from noreply-batterystatusmonitor@domain.de (...)
      } ElseIf ($NewStatus -eq 2) {
...

After that, I also did a scheduled task which runs the script automatically at user login and in the background. For the action of the scheduled task, you have to give it the path to the PowerShell itself in the Windows-Directory. In the optional parameters field, you have to add the path to the script "PowerShell -noexit -f " in the front, just like in the shortcut.

After a view tests, it does very well now that way.

Geco Mynx

Posted 2016-10-25T08:45:55.460

Reputation: 91