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)) $threshold Write-Verbose -Message "Creating job" Register-ScheduledJob -name CheckHeadPhonesBattery -Trigger $trigger -ScriptBlock { #set battery thresholdhere $threshold = 5 function Get-JabraBatteryLevel { [CmdletBinding()] [Alias()] [OutputType([int])] Param ( # Param1 help description [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$false, Position=0)] $Logpath ) Begin { $Logpath = "$env:USERPROFILE\AppData\Roaming\Jabra Direct\logs\JabraDirect.main.log" 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" } }