Last active
October 18, 2022 07:19
-
-
Save bsj17/2dcc043b8678f18f9b8b45eebe2e4f49 to your computer and use it in GitHub Desktop.
Script that creates scheduled task which checks Jabra log and send tost notification when battery goes under threshold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function GenHostOptions () { | |
$Yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', 'Yes' | |
$No = New-Object System.Management.Automation.Host.ChoiceDescription '&No', 'No' | |
$options = [System.Management.Automation.Host.ChoiceDescription[]]($Yes, $No) | |
$title = 'Jabra battery notification' | |
$message = "Do you want to proceed creating and enabling Scheduled job?" | |
$result = $host.ui.PromptForChoice($title, $message, $options, 0) | |
switch ($result ) { | |
0 { $true } | |
1 { $false } | |
Default { "wrong option" } | |
} | |
} | |
if (GenHostOptions) { | |
#check if Admin | |
Write-Verbose -Message "Checking if user is admin" | |
$user = [Security.Principal.WindowsIdentity]::GetCurrent(); | |
if((New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)) | |
{ | |
Write-Verbose -Message "Creating JobTrigger" | |
$trigger= New-JobTrigger -RepetitionInterval (New-TimeSpan -Minutes 5) -Once -RepetitionDuration ([TimeSpan]::MaxValue) -At ([datetime]::Now.AddMinutes(5)) | |
Write-Verbose -Message "Creating job" | |
Register-ScheduledJob -name CheckHeadPhonesBattery -Trigger $trigger -ScriptBlock { | |
#set battery threshold here. i.e. "currentValue < $threshold" | |
$threshold = 5 | |
function Get-JabraBatteryLevel | |
{ | |
[CmdletBinding()] | |
[Alias()] | |
[OutputType([int])] | |
Param | |
( | |
# Param1 help description | |
[Parameter(Mandatory=$false, | |
ValueFromPipelineByPropertyName=$false, | |
Position=0)] | |
$Logpath = "$env:USERPROFILE\AppData\Roaming\Jabra Direct\logs\JabraDirect.main.log" | |
) | |
Begin | |
{ | |
if (Test-Path -Path $Logpath){ | |
$go = $true | |
} | |
else{ | |
$go = $false | |
#ToDo log event viewer | |
exit | |
} | |
} | |
Process | |
{ | |
if ($go){ | |
$LogContent = [IO.File]::ReadAllText($Logpath , [Text.Encoding]::GetEncoding(0)) | |
$r = "(?<=(\[(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3})\]\s\[info\]\s{2}\[\n\s{2}'Event\sbattery\sstatus',\n))(.*)(?=\n\]\r?\n?)" | |
$matches = [regex]::Matches($LogContent,$r) | |
if (-not $matches.Count -le 0) { | |
$index = $matches.Count -1 | |
[int]$batteryValue = [regex]::Match(($matches[$index].Groups[3].value -split ",")[2],"level:\s(\d{1,2})").groups[1].value | |
} | |
else{ | |
Write-Warning -Message "Regex condition found 0 mathes in logfile" | |
#ToDo log event viewer | |
$batteryValue = -1 | |
} | |
} | |
} | |
End | |
{ | |
return $batteryValue | |
} | |
} | |
#https://den.dev/blog/powershell-windows-notification/ | |
function Show-Notification { | |
[cmdletbinding()] | |
Param ( | |
[string] | |
$ToastTitle, | |
[string] | |
[parameter(ValueFromPipeline)] | |
$ToastText | |
) | |
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null | |
$Template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02) | |
$RawXml = [xml] $Template.GetXml() | |
($RawXml.toast.visual.binding.text|where {$_.id -eq "1"}).AppendChild($RawXml.CreateTextNode($ToastTitle)) > $null | |
($RawXml.toast.visual.binding.text|where {$_.id -eq "2"}).AppendChild($RawXml.CreateTextNode($ToastText)) > $null | |
$SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument | |
$SerializedXml.LoadXml($RawXml.OuterXml) | |
$Toast = [Windows.UI.Notifications.ToastNotification]::new($SerializedXml) | |
$Toast.Tag = "Jabra" | |
$Toast.Group = "JabraHeadPhones" | |
$Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1) | |
$Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("JabraHeadPhones") | |
$Notifier.Show($Toast); | |
} | |
$batteryValue = Get-JabraBatteryLevel | |
if($batteryValue -lt $threshold){ | |
Show-Notification -ToastTitle "Low Battery" -ToastText $( "Battery Level: {0}%" -f $batteryValue) | |
} | |
else{ | |
#Write-EventLog -source JabraHeadPhonesAutomation -LogName Application -Message "Logfile not found: $Logpath" -EntryType Error -EventId 404 | |
exit | |
} | |
} | |
} | |
else | |
{ | |
Write-Warning -Message "Please run as admin, script needs to create Scheduled job" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment