Who Wants to Be a Scripting Guy?
The Weekly Scripting Puzzle

Reprinted without permission - feel free to post whether you solved the puzzle or not, or Personal Message (PM) me with the answer, and I will verify and post your result. Please do not post the answer.

December 2, 2005: Thanks, But I Didn’t Even Ask for That

OK, so maybe this script isn’t really broken. Then again, maybe it is. To be honest, we’re not sure what’s going on. Which is why we need your help.

When we write WQL queries we typically use the Select * statement to return the values of all the properties in a class. That’s fine, but on occasion we write queries that return massive amounts of data. In order to cut down on network traffic, we decided to experiment with writing a WQL query that returned values only for selected properties.

With that in mind, we wrote the following script, one designed to return just the names of all the processes running on a computer:

Code:

Break On

$strComputer = "."

$objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2")

$colItems = $objWMIService.ExecQuery ("Select Name from Win32_Process")

For Each $objItem in $colItems
? $objItem.Handle
Next

Exit 0



That’s fine, except we made a mistake: in our For Each loop, we accidentally asked the script to echo the value of the Handle property rather than the Name property. Because we didn’t ask for the value of the Handle property in our WQL query, that means the script should fail, right?

Wrong. Instead, here’s a sample of the information we got back:

Code:

0
4
660
948
972
1016
1028
1212
1248
304
928
1088
1444
1876
1900
1932



As it turns out, these are the values of the Handle property. That’s weird, but this is even weirder: we replaced $objItem.Handle with $objItem.ProcessID (like so) and tried rerunning the script:

Code:

Break On

$strComputer = "."

$objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2")

$colItems = $objWMIService.ExecQuery ("Select Name from Win32_Process")

For Each $objItem in $colItems
? $objItem.ProcessID
Next

Exit 0



Did that work? No. And when we [added an @SERROR] statement and re-ran the script we got this error message:

Code:

Unknown name.



In other words, we get back the process Handle even though we didn’t ask for it, but when we tried to get back the ProcessID we were told that the object doesn’t support the ProcessID property. No problem there, except that the Win32_Process class does support the ProcessID property. Holy smokes!

What’s wrong with this script?