I recently needed to check and see whether a backup drive (which was just a 4TB USB drive) was plugged into a server. But the server had no GUI, so I had to use the command line. There was no drive letter mapped to this drive, so I needed to use something else and I needed to make a script that could be used long-term. Luckily, PowerShell can be used to obtain WMI information on the hardware installed on a computer. This allows administrators to query WMI about the USB devices currently installed on a server. In the following command, we’re going to use gwmi from PowerShell and we’re going to query
for Win32_USBControllerDevice. We’re going to run the command against the computer name in question (example here is host.krypted.com although if we left the -computername option off it would run against the host the command is run on).
Get-WMiObject Win32_USBControllerDevice -computername host.krypted.com | fl Antecedent,Dependent
This will apply a filter, similar to using grep in bash. That filters only the antecedent and dependent fields from the host.krypted.com computer. You could also remove the pipe and pull a full export, but if I’m using this in a script the less data to parse the better. If you think of WMI as containing a big tree about the hardware installed, the filter for Antecedent brings back what must be running in order for the drive to be present and the Dependent returns those that are dependent on the drive.
You can also obtain a lot more information through WMI. For example, you can pull information from any of the WMI classes, such as win32_bios
Get-WmiObject win32_bios -computername host.krypted.com
Note, you can derive properties and methods for a given class by using the get-member commandlet:
Get-WmiObject win32_bios | get-member
Once you know which property you need, you can then parse the information a little further to get a very specific answer:
get-wmiobject win32_bios -computername host.krypted.com | Select-Object displayname
Finally, you can shorten this by replacing the Get-WmiObject commandlet with gwmi, which is an alias for that command. Test it out, if you like:
gwmi win32_bios | get-member
krypted March 28th, 2014