Your basic ITPro blog... What's going on at work, what I'm interested in.

Thursday, March 20, 2008

2008-03-20 PowerShell Exercise

So, this one is a cool example of how PowerShell integrates and uses WMI. Now, I just need to learn WMI!  :-)


Hey, Scripting Guy! I’ve looked through the entire Script Center but I can’t find an example of what I need to do: find all the .DOC files on drives C and D, but using a single WMI query. In other words, I want to write a WMI query similar to this: Where ( Drive = 'C:' or 'D:' ) and ( Extension = 'doc' ); unfortunately, though, I can’t figure out the correct syntax. Is there a way to mix AND and OR clauses in a single query?


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("Select * From CIM_DataFile Where (Drive = 'C:' OR Drive = 'D:') AND Extension = 'doc'") 
For Each objFile In colFiles 
    Wscript.Echo objFile.Name 


$computerName = "." 
$wmiQuery = "Select * from CIM_DataFile Where (Drive = 'G:' OR Drive = 'U:') AND Extension = 'doc'" 
Get-WmiObject -Query $wmiQuery -ComputerName $computerName

Or, as a one-liner...

Get-WmiObject -Query "Select * from CIM_DataFile Where (Drive = 'G:' OR Drive = 'U:') AND Extension = 'doc'" -ComputerName "."
# You can leave of the -ComputerName parameter altogether if you want

No comments:

Additional Info

My photo
email: support (AT) mangrumtech (DOT) com
mobile: 480-270-4332