Skip to content

Instantly share code, notes, and snippets.

@bsj17
Last active October 18, 2022 07:19
Show Gist options
  • Save bsj17/2dcc043b8678f18f9b8b45eebe2e4f49 to your computer and use it in GitHub Desktop.
Save bsj17/2dcc043b8678f18f9b8b45eebe2e4f49 to your computer and use it in GitHub Desktop.

Revisions

  1. bsj17 revised this gist Oct 18, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions CheckHeadPhonesBattery.ps1
    Original file line number Diff line number Diff line change
    @@ -48,15 +48,15 @@ if (GenHostOptions) {
    [Parameter(Mandatory=$false,
    ValueFromPipelineByPropertyName=$false,
    Position=0)]
    $Logpath
    $Logpath = "$env:USERPROFILE\AppData\Roaming\Jabra Direct\logs\JabraDirect.main.log"



    )

    Begin
    {
    $Logpath = "$env:USERPROFILE\AppData\Roaming\Jabra Direct\logs\JabraDirect.main.log"

    if (Test-Path -Path $Logpath){

    $go = $true
  2. bsj17 revised this gist Oct 17, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CheckHeadPhonesBattery.ps1
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ if (GenHostOptions) {

    Register-ScheduledJob -name CheckHeadPhonesBattery -Trigger $trigger -ScriptBlock {

    #set battery thresholdhere
    #set battery threshold here. i.e. "currentValue < $threshold"
    $threshold = 5

    function Get-JabraBatteryLevel
  3. bsj17 revised this gist Oct 17, 2022. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion CheckHeadPhonesBattery.ps1
    Original 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))
    $threshold

    Write-Verbose -Message "Creating job"

  4. bsj17 revised this gist Oct 17, 2022. 1 changed file with 139 additions and 90 deletions.
    229 changes: 139 additions & 90 deletions CheckHeadPhonesBattery.ps1
    Original file line number Diff line number Diff line change
    @@ -1,105 +1,154 @@
    $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){
    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
    $go = $true

    }
    else{
    $go = $false
    #ToDo log event viewer
    exit
    }
    }
    Process
    {
    if ($go){
    $LogContent = [IO.File]::ReadAllText($Logpath , [Text.Encoding]::GetEncoding(0))
    }
    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?)"
    $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)
    $matches = [regex]::Matches($LogContent,$r)

    if (-not $matches.Count -le 0) {
    $index = $matches.Count -1
    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
    [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-Warning -Message "Regex condition found 0 mathes in logfile"
    #ToDo log event viewer
    $batteryValue = -1
    #Write-EventLog -source JabraHeadPhonesAutomation -LogName Application -Message "Logfile not found: $Logpath" -EntryType Error -EventId 404
    exit
    }
    }

    }

    }
    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
    }
    }
    else
    {
    Write-Warning -Message "Please run as admin, script needs to create Scheduled job"
    }


    }
  5. bsj17 created this gist Oct 17, 2022.
    105 changes: 105 additions & 0 deletions CheckHeadPhonesBattery.ps1
    Original 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
    }
    }