none
PowerShell Automate Windows Lock depending on paired Bluetooth device RRS feed

  • Question

  • I am trying to write a Power Shell script which will check the connection status of paired Bluetooth devices in a certain interval (15 sec) and if found the device is out of range then will lock the users system. In the following code I am not able to figure out how to list all the paired device.

    $ListAllDevice = "Get the list of all paired Bluetooth Device"
    $DeviceStatus = $ListAllDevice | Get-ItemProperty
    $DeviceDetection = ($DeviceStatus).DeviceState
    ForEach($IndDetection in $DeviceDetection)
      {
      If($IndDetection -eq 1)
       {
       Write-Host "device detected."
       }
      Else
       {
       rundll32.exe user32.dll,LockWorkStation
       }
      }
    }

    While in other approach I also tried with the following code, but here Node status doesn’t change even if the connection out of range.

    (Get-Device | Where-Object {$_.Name -like '*Lenovo*'}).NodeStatus

    Any idea how this can be done ? 



    • Edited by Indrajit_83 Monday, April 2, 2018 4:14 PM Formatting
    Monday, April 2, 2018 10:00 AM

Answers

  • Start with this:

    $sb = {
        $e = $Event.SourceEventArgs.NewEvent
        switch($e.EventType){ 
            1 {Write-Host 'Configuration changed' -fore green }
            2 {Write-Host 'Device Arrival' -fore green }
            3 {Write-Host 'Device Removal' -fore green } 
            4 {Write-Host 'Docking' -fore green } 
            default {Write-Host 'Unknown event type!' -fore green } 
        }         
        write-Host ($Event.SourceEventArgs.NewEvent|select *|Out-String) -fore green
    }
    Register-WmiEvent -Class win32_DeviceChangeEvent -SourceIdentifier deviceChange -Action $sb
    

    You will have to construct an event query that selects only your device.  This one events on every device change.


    \_(ツ)_/

    • Marked as answer by Indrajit_83 Thursday, April 5, 2018 9:13 AM
    Thursday, April 5, 2018 8:38 AM

All replies

  • Please do not post colorized code.  Use the code posting tool provided.


    \_(ツ)_/

    Monday, April 2, 2018 2:47 AM
  • The code posting tool is on the edit bar - "<>". Please use it.


    \_(ツ)_/

    Monday, April 2, 2018 10:12 AM
  • Hi,

    According to your situation, I guess you can try Get-PnpDevice cmdlet to get the bluetooth device status. In addition, you can use the following example script to check the status every 15 seconds:
    while (1) {
        if (<#Bluetooth devices are out of range#>) {
            rundll32.exe user32.dll,LockWorkStation
            return
        }
        else {
            Start-Sleep -Seconds 15
        }
    }

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, April 3, 2018 6:24 AM
  • Hi,

    Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, April 5, 2018 7:24 AM
  • Thanks Albert,

     Could you please guide me the how I can get my specific Bluetooth device connected or not connected status ? I tried with following command

    Get-PnpDevice -FriendlyName 'Lenovo P70-A'


    Thursday, April 5, 2018 7:43 AM
  • Start with this:

    $sb = {
        $e = $Event.SourceEventArgs.NewEvent
        switch($e.EventType){ 
            1 {Write-Host 'Configuration changed' -fore green }
            2 {Write-Host 'Device Arrival' -fore green }
            3 {Write-Host 'Device Removal' -fore green } 
            4 {Write-Host 'Docking' -fore green } 
            default {Write-Host 'Unknown event type!' -fore green } 
        }         
        write-Host ($Event.SourceEventArgs.NewEvent|select *|Out-String) -fore green
    }
    Register-WmiEvent -Class win32_DeviceChangeEvent -SourceIdentifier deviceChange -Action $sb
    

    You will have to construct an event query that selects only your device.  This one events on every device change.


    \_(ツ)_/

    • Marked as answer by Indrajit_83 Thursday, April 5, 2018 9:13 AM
    Thursday, April 5, 2018 8:38 AM
  • I am not pro in Power Shell scripting. If you have reference link or demo script to address this scenario please let me know.

    Thursday, April 5, 2018 9:15 AM
  • All examples I have found are written in C#. 

    Here is a targeted example:

    $sb = {
        $e = $Event.SourceEventArgs.NewEvent
        switch($e.EventType){ 
            1 {Write-Host 'Configuration changed' -fore green }
            2 {Write-Host 'Device Arrival' -fore green }
            3 {Write-Host 'Device Removal' -fore green } 
            4 {Write-Host 'Docking' -fore green } 
            default {Write-Host ('Unknown event type:' + $e.EventType) -fore green } 
        }         
        write-Host ($Event.SourceEventArgs.NewEvent|select *|Out-String) -fore green
    }
    $query = @'
    SELECT * FROM __InstanceCreationEvent
             WITHIN 5
             WHERE TargetInstance ISA "Win32_PnPEntity"
             AND TargetInstance.Name = "Headphones (Hesh 2 Wireless Stereo)"
    '@
    Register-WmiEvent -Query $query -SourceIdentifier deviceChange -Action $sb
    
    


    \_(ツ)_/

    Thursday, April 5, 2018 9:34 AM
  • TargetInstance.Name and Device_FriendlyName are different ? I am framing query like followings

    $query = @'

    SELECT * FROM __InstanceCreationEvent

             WITHIN 5

             WHERE TargetInstance ISA "Win32_PnPEntity"

             AND TargetInstance.FriendlyName = "Lenovo P70-A"

     My result as follows

    Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                 

    --     ----            -------------   -----         -----------     --------             -------                 

    21     deviceChange                    NotStarted    False                                ...     

    Friday, April 6, 2018 3:28 AM