Thursday, June 4, 2009

Searching for files remotely

We are looking to possibly centralize the management of our OPT files. Thought maybe a quick script in PowerShell would do the trick. Ran the following line:

Get-childitem \\server1-vp01\c$ -Include *.opt -Recurse

and waited, waited, waited...... 6 minutes later the console displayed my results.
Knowing a little bit about WMI, I decided to approach this from a different angle:
Get-WmiObject `
-class CIM_DATAFile `
-computername 'server-vp01' `
-filter "extension='opt' and drive='c:'"
This gave me back the results in 2.73 seconds!

Comparing the times generated the following:
measure-command {`
get-childitem \\server-vp01\c$ `
-Include *.opt `
-Recurse}
Days              : 0
Hours :
0
Minutes :
6
Seconds :
3
Milliseconds :
476
Ticks :
3634762131
TotalDays : 0.00420690061458333
TotalHours : 0.10096561475
TotalMinutes : 6.057936885
TotalSeconds : 363.4762131
TotalMilliseconds : 363476.2131
measure-command {`
Get-WmiObject `
-class CIM_DATAFile `
-computername 'server-vp01' `
-filter "extension='opt' and drive='c:'"}

Days :
0
Hours :
0
Minutes :
0
Seconds :
2
Milliseconds :
739
Ticks :
27390035
TotalDays : 3.17014293981481E
-05
TotalHours : 0.000760834305555556
TotalMinutes : 0.0456500583333333
TotalSeconds : 2.7390035
TotalMilliseconds : 2739.0035
Pretty obvious which method to use.
Once again, PowerShell and WMI save the day.

3 comments:

Derek Mangrum said...

Hey there. I was playing around with this and have a question... Are you sure your WMI query is doing the same recursive search?

See the end of this article: http://powershell.com/cs/blogs/tips/archive/2009/04/07/accessing-individual-files-and-folders-remotely-via-wmi.aspx

I try using the '%' wildcard and get no returned results, but the time it takes to run is about the same as the 'get-childitem' command.

This is very odd... Still playing with it.

Wes Stahler said...

The WMI query I am running returns the same as GCI. Can you share your query?

J Kavanagh said...

I have run into this as well. I was disappointed. I love WMI but for many things it is just dog slow. Thanks for the article sir.