This thread was started earlier today, http://community.spiceworks.com/topic/343454-lock-computer-automatically-when-user-leaves-desk?page=1, and it has to do with locking a pc automatically when the user leaves the area. I found a link that uses the bluetooth on the phone, but the link was dead. It got me thinking though.
For some reason my cell phone wouldn't pair with my PC (drivers I think), but I did have a bluetooth headset paired. The wheels started spinning. I first search WMI_PnPEntity for all devices plugged in to my machine. It listed everything, but getting it to detect if a device was enabled was a struggle for me. I then turned to Win32_SoundDevice, but that one as well didn't have anything I could see that would say the audio device was actually working or not. I stumbled upon a site that talked about registry keys. I poked around there and found what I was looking for.
HKLM:\Software\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\ lists all the audio devices on the machine, along with their state. Every time I would turn my BlueTooth earpiece off, the state of one of the devices would turn to 8. When My BlueTooth earpiece moves out of range, the state turns to 8 as well because according to the pc, it's "off". Bingo.
Here's what I came up with: it polls the registry key every 15 seconds for any subkeys(audio devices) that have their state at 8. When it gets an 8, it locks the PC. The script would fire off manually in the morning and run until 5pm. This is very rough and doubtfully HIPPA compliant, so "Polish the Turd" everyone.
Do { Start-Sleep -Seconds 15 $AudioDevices = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\" ForEach($indDevice in $AudioDevices) { $DeviceStatus = $AudioDevices | Get-ItemProperty $DeviceDetection = ($DeviceStatus).DeviceState ForEach($IndDetection in $DeviceDetection) { If($IndDetection -eq 1) { Write-Host "device detected." } Else { rundll32.exe user32.dll,LockWorkStation } } } } While((Get-Date).hour -le 18)