0

I have a server list that feeds into a function. I'm looking to query each machine and get an entire list of all files and folders. Id like to write a file to my local machine, the file name will be the the targets ip ($computer) and append all the data from $userdata into the file.

I keep getting the following error:

Get-WmiObject : Invalid query

foreach ($computer in $Server_List)
{
    $userdata = Get-WmiObject -Credential -Computer $computer -Query "SELECT * from CIM_Data File WHERE Drive = 'C:'"
    echo "$computer" | Out-File -FilePath $File_path + $computer +".txt" -Append $userdata
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
iNoob
  • 1,375
  • 3
  • 19
  • 47

1 Answers1

1

You have a spurious space in your query. The class name is CIM_DataFile, not CIM_Data File.

This:

"SELECT * from CIM_Data File WHERE Drive = 'C:'"
                       ^

needs to look like this:

"SELECT * from CIM_DataFile WHERE Drive = 'C:'"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • You may want to build your file path like this: `$outputfile = Join-Path $File_path "$computer.txt"` rather than doing a string concatenation in the `Out-File` statement. You may also want to export the query result to a CSV rather than using `Out-File`. – Ansgar Wiechers Feb 01 '16 at 15:56
  • Ok, thanks for the advice, so `Export-CSV $outputfile = Join-Path $File_path "$computer.txt"` ? – iNoob Feb 01 '16 at 16:04
  • No. `$outputfile = Join-Path ...; Get-WmiObject ... | Export-Csv $outputfile -Append ...` – Ansgar Wiechers Feb 01 '16 at 16:06
  • It would be nice if I could filter on multiple extensions in the query but, i didnt have any luck with AND Extension = 'txt' OR Extension = 'doc' etc. So ended up going down the route im using now and am looking to filter the csv file post gather – iNoob Feb 01 '16 at 16:06
  • 2
    Please do not move the target. If you have a new question: post a new question. – Ansgar Wiechers Feb 01 '16 at 16:08
  • Thanks for your help – iNoob Feb 01 '16 at 16:08