Hi all first timer here and having a bit of a problem with a PS script I’m trying to create.<\/p>\n
I am looking to run a PS WMI query on what USB storage devices are connected to a list of devices in ComputerList.txt what I want to achieve is to have the Device name , Device ID, Manufacturer and description then output to DeviceOutput.csv.<\/p>\n
$hostname = Get-Content C:\\ComputerList.txt
\n$Output = Invoke-Command -ComputerName $hostname { Get-WmiObject Win32_USBControllerDevice | Select Name,DeviceID,Manufacturer,Description }
\n$Output | Out-File C:\\DeviceOutput.csv<\/p>\n
What I have in the output is lacking the relevant information and I am not sure why its failing I think its due to the WMI Win32_USBControllerDevice?<\/p>\n
Any help is much appreciated with this one as I’m sure its something simple.<\/p>","upvoteCount":3,"answerCount":6,"datePublished":"2018-06-06T07:26:55.000Z","author":{"@type":"Person","name":"johnnewell3","url":"https://community.spiceworks.com/u/johnnewell3"},"suggestedAnswer":[{"@type":"Answer","text":"
Hi all first timer here and having a bit of a problem with a PS script I’m trying to create.<\/p>\n
I am looking to run a PS WMI query on what USB storage devices are connected to a list of devices in ComputerList.txt what I want to achieve is to have the Device name , Device ID, Manufacturer and description then output to DeviceOutput.csv.<\/p>\n
$hostname = Get-Content C:\\ComputerList.txt
\n$Output = Invoke-Command -ComputerName $hostname { Get-WmiObject Win32_USBControllerDevice | Select Name,DeviceID,Manufacturer,Description }
\n$Output | Out-File C:\\DeviceOutput.csv<\/p>\n
What I have in the output is lacking the relevant information and I am not sure why its failing I think its due to the WMI Win32_USBControllerDevice?<\/p>\n
Any help is much appreciated with this one as I’m sure its something simple.<\/p>","upvoteCount":3,"datePublished":"2018-06-06T07:26:55.000Z","url":"https://community.spiceworks.com/t/powershell-wmi-usb-storage-query/655480/1","author":{"@type":"Person","name":"johnnewell3","url":"https://community.spiceworks.com/u/johnnewell3"}},{"@type":"Answer","text":"
you need foreach and export as csv<\/p>\n
$hostname = Get-Content C:\\ComputerList.txt\n$data=ForEach($host in $hostname){\nGet-WmiObject Win32_USBControllerDevice -ComputerName $host | \nSelect @{n=\"Computername\";e={\"$host\"}},Name,DeviceID,Manufacturer,Description\n}\n\n$data |export-csv C:\\DeviceOutput.csv -NoTypeInformation\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-06-06T08:12:54.000Z","url":"https://community.spiceworks.com/t/powershell-wmi-usb-storage-query/655480/2","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"Thanks for the prompt reply JitenSH but the script is now having problems as it now errors when running stating problems with the $host variable as it is read-only.<\/p>\n
Cannot overwrite variable Host because it is read-only or constant.
\nAt line:2 char:15<\/p>\n
\n- $data=ForEach($host in $hostname){<\/li>\n
- \n
<\/code><\/pre>\n<\/li>\n- CategoryInfo : WriteError: (Host:String) <\/span>, SessionStateUnauthorizedAccessException<\/li>\n
- FullyQualifiedErrorId : VariableNotWritable<\/li>\n<\/ul>\n
Export-Csv : Cannot bind argument to parameter ‘InputObject’ because it is null.
\nAt line:7 char:8<\/p>\n
\n- $data |export-csv C:\\Scripts\\DeviceOutput.csv -NoTypeInformation<\/li>\n
- \n
<\/code><\/pre>\n<\/li>\n- CategoryInfo : InvalidData: (
[Export-Csv], ParameterBindingValidationException<\/li>\n - FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand<\/li>\n<\/ul>","upvoteCount":0,"datePublished":"2018-06-06T08:20:27.000Z","url":"https://community.spiceworks.com/t/powershell-wmi-usb-storage-query/655480/3","author":{"@type":"Person","name":"johnnewell3","url":"https://community.spiceworks.com/u/johnnewell3"}},{"@type":"Answer","text":"
My bad ,Apparently $Host is a reserved word / Variable in Powershell<\/p>\n
make it<\/p>\n
$hostname = Get-Content C:\\ComputerList.txt\n$data=ForEach($computer in $hostname){\nGet-WmiObject Win32_USBControllerDevice -ComputerName $computer| \nSelect @{n=\"Computername\";e={\"$computer\"}},Name,DeviceID,Manufacturer,Description\n}\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-06-06T08:24:23.000Z","url":"https://community.spiceworks.com/t/powershell-wmi-usb-storage-query/655480/4","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"Thanks for that JitenSH as it has cleared up the error now.<\/p>\n
The only problem I have is what I was facing originally and that is that the script runs but all it shows in the “Computer Name” that its taking from the computerlist.txt but is not actually showing any USB storage device information.<\/p>\n
This is leading me to think Win32_USBControllerDevice is maybe not the correct WMI item to find out the local USB storage information, no matter how much searching I do I cant find what I need as Win32_PNPEntity covers this but just has to much information to digest and no way to filter it.<\/p>\n