Hi guys,

I need a powershell script that will monitor every 5 mins or so, to see if a specific USB device is present. IF the device is no longer there, then it will restart the PC.

And the script will start looking again also.

Is this Possible?

So far I have got this, which isn’t really helpful, other than the fact it lists all of the devices attached.

gwmi Win32_USBControllerDevice |%{wmi} |Sort Manufacturer,Description,DeviceID |Ft -GroupBy Manufacturer Description,Service,DeviceID

Thanks.

5 Spice ups

There’s a couple ways I can think to do this. You could do it by USB model (the model I used is just the USB drive I have plugged in):

$USB = GET-WMIOBJECT win32_diskdrive | Where { $_.InterfaceType –eq ‘USB’ }
IF ($USB.model -ne "Kingston DataTraveler 3.0 USB Device"){
Restart-Computer
    }

or Test-Path would work if it’s going to have the same drive letter each time it’s plugged in.

$Path = Test-Path E:
IF ($Path -ne $True){
Restart-Computer
    }

Then just run it as a scheduled task.

4 Spice ups

Altering Big Green Man’s script just a little, I would use this:

$findID = "DeviceIdToFind"
$usbs = gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)} |Sort Manufacturer,Description,DeviceID

While ($($usbs.deviceID).Contains($findID)){
	Sleep -Seconds 300 #sleep is total seconds to wait before continuing
	$usbs = gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)} |Sort Manufacturer,Description,DeviceID

}
#findID not found anymore
Restart-Computer

and then just set that script to start at each bootup.

3 Spice ups

I like that.

2 Spice ups

What is your purpose? If you are looking for a way of securely locking the machines, I don’t think I would rely solely on the brand of the key that someone else could order off of ebay. Not to mention, what happens if they stop making that model?

Just because I like to over complicating things, I would code a file onto the key with a GUID in it - pass the model of the key and the GUID to a server and then have the server validate if they two are valid together (and neither have been expired) - only if the server responds appropriately would the machine boot.
Also, I would Stop-Computer, not Restart-Computer. No point in having the machine repeatedly power cycling for days because someone walked away and took the key out…

1 Spice up

its not for securing something, its incase an audio device on an unattended PC disconnects then the pc will restart and “hopefully” reconnect.

1 Spice up

This is Perfect thanks!!!

1 Spice up
$USB = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} |  %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | %{$_.deviceid}
if($USB){
    Write-Host 'USB Exits'
}else{
     Restart-Computer -whatif
}

check this OUT

1 Spice up

Assuming the device is a thumb drive and not a usb keyboard or usb mouse you could check the device’s volume name and if that one is present retrieve the assigned drive letter to be used in the test-path. Something like this. To quickly test it I used C: as the sample driveletter…

if (test-path $(get-wmiobject win32_volume | where-object {$_.label -eq 'Windows'} | select-object driveletter).driveletter)
 {
  "it's ok"
  }
  else
  { restart-computer }