1

I have a Node JS NW.js/Angular app that continuously runs a PowerShell script every 5 seconds to get the currently connected removable storage devices (USB thumb drives, SD cards) that I then show to the user in the UI. My question is: Is there anything I can do on the PowerShell script itself to notify me of changes rather then running it in a continuous loop? It seems fine now and working smoothly, just wondering if there's a "better" way. I'm using the node-powershell library from NPM to call the command from a simple JavaScript setInterval() method.

ConvertTo-Json @(Get-WmiObject Win32_Volume -Filter ("DriveType=2 AND FileSystem IS NOT NULL"))
1
  • it seems that you can register an action to happen when an event occurs. the Register-WmiEvent and Register-CimIndicationEvent both seem to be able to do what your code is doing. i do not know if you need to leave a powershell session running for it to work, tho. [blush] i've only read about the two cmdlets.
    – Lee_Dailey
    Commented Mar 11, 2019 at 1:26

1 Answer 1

0

You can hook a Scheduled Task to a Windows hardware change event. Hardware change event ID detected > Task is triggered > Task calls your script...

Connection Event IDs When a USB removable storage device is connected to a Windows 7 system, a number of event records should be generated in the Microsoft-Windows-DriverFrameworks-UserMode/Operational event log. The records include those with Event ID 2003, 2004, 2005, 2010, 2100, 2105, and more. ...

Disconnection Event IDs When a USB thumb drive is disconnected from a Windows 7 system, a few event records should be generated in the same event log as the connection events. Records with Event ID 2100, 2102, and potentially more may be generated when a USB device is disconnected. ...

Not the answer you're looking for? Browse other questions tagged or ask your own question.