Scripting Games–filtering on remote server

In event 3 you have to get information on hard disk capacity.  I’ve only looked at the first couple of dozen scripts but seen this too many times

Get-WmiObject -Class Win32_LogicalDisk | where DriveType -eq 3

or if you prefer the version 2 way

Get-WmiObject -Class Win32_LogicalDisk | where {$_.DriveType -eq 3}

If puppies get terminated for using Write-Host this sort of construct should triggers a mass extinction.

When pulling information back with WMI (or any other technique) from a remote server ALWAYS, ALWAYS, ALWAYS filter on the remote server.  What you are doing here is pulling back all of the data and filtering on the client. This is grossly inefficient when you are dealing with hundreds of machines.

The PowerShell team gave us the –Filter parameter on Get-WmiObject for a reason. Its to do the filtering on the remote server.

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType = 3"

If you are guilty of not using –Filter write out 100 times “I must filter on the remote server”

And no – you can’t write a PowerShell script to do it for you!

One Response to Scripting Games–filtering on remote server

  • jkavanagh58 says:

    While I agree… I usually start out thinking I will use Filter instead of a where clause but I have to admit that I will normally use where instead. If I go back and refine the CLI or script code I might change it but normally I am trying to accomplish a task and I use what works. Filters are great but the variances (gwmi hence WQL syntax, vs ActiveDirectory, PowerCLI, etc..) just make it more expedient to use a where clause and while the code is less efficient the time I can write the code and complete the task tends to be faster. IMHO

Leave a Reply