#153413 - 2005-12-15 03:11 AM
The Weekly Scripting Puzzle - December 2, 2005
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
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?
|
|
Top
|
|
|
|
#153425 - 2005-12-16 01:44 AM
Re: The Weekly Scripting Puzzle - December 2, 2005
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
accepted, we trust you. another option might be to PM me with the answer. then I can post who "got it"
|
|
Top
|
|
|
|
#153427 - 2005-12-16 07:45 PM
Re: The Weekly Scripting Puzzle - December 2, 2005
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Hope my latest PM covers it. 
Kent
|
|
Top
|
|
|
|
#153429 - 2005-12-20 05:09 PM
Re: The Weekly Scripting Puzzle - December 2, 2005
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
So.. Who are the winner(s)? 
Kent
|
|
Top
|
|
|
|
#153432 - 2005-12-20 07:12 PM
Re: The Weekly Scripting Puzzle - December 2, 2005
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
In other words WMI just sucks as it is only a half baked idea that M$ implemented. As with one of the other challenges where you could not get events from the security event log.
|
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 584 anonymous users online.
|
|
|