Last active
October 18, 2022 07:19
-
-
Save bsj17/2dcc043b8678f18f9b8b45eebe2e4f49 to your computer and use it in GitHub Desktop.
Revisions
-
bsj17 revised this gist
Oct 18, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -48,15 +48,15 @@ if (GenHostOptions) { [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 -
bsj17 revised this gist
Oct 17, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -34,7 +34,7 @@ if (GenHostOptions) { Register-ScheduledJob -name CheckHeadPhonesBattery -Trigger $trigger -ScriptBlock { #set battery threshold here. i.e. "currentValue < $threshold" $threshold = 5 function Get-JabraBatteryLevel -
bsj17 revised this gist
Oct 17, 2022 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -29,7 +29,6 @@ if (GenHostOptions) { $trigger= New-JobTrigger -RepetitionInterval (New-TimeSpan -Minutes 5) -Once -RepetitionDuration ([TimeSpan]::MaxValue) -At ([datetime]::Now.AddMinutes(5)) Write-Verbose -Message "Creating job" -
bsj17 revised this gist
Oct 17, 2022 . 1 changed file with 139 additions and 90 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,105 +1,154 @@ 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" } } -
bsj17 created this gist
Oct 17, 2022 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,105 @@ $trigger= New-JobTrigger -RepetitionInterval (New-TimeSpan -Minutes 5) -Once -RepetitionDuration ([TimeSpan]::MaxValue) -At ([datetime]::Now.AddMinutes(5)) Register-ScheduledJob -name CheckHeadPhonesBattery01 -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); } if($batteryValue -lt $threshold){ Show-Notification -ToastTitle "Low Battery" -ToastText $( "Battery Level: {0}%" -f (Get-JabraBatteryLevel)) } else{ #Write-EventLog -source JabraHeadPhonesAutomation -LogName Application -Message "Logfile not found: $Logpath" -EntryType Error -EventId 404 exit } }