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
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