Page 1 of 2 12>
Topic Options
#153413 - 2005-12-15 03:11 AM The Weekly Scripting Puzzle - December 2, 2005
Shawn Administrator Offline
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
#153414 - 2005-12-15 03:57 AM Re: The Weekly Scripting Puzzle - December 2, 2005
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
I think the point of this puzzle is two-fold.

1) Why does fetching the ProcessID in the second script not work ?

2) If fetching the ProcessID in the second script doesn't work, why does fetching the Handle in the first script work ?

-Shawn

Top
#153415 - 2005-12-15 05:54 AM Re: The Weekly Scripting Puzzle - December 2, 2005
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: CA
But you never come back and answer them. I think they should all be in the same thread to make the question/answer easy to locate.
Top
#153416 - 2005-12-15 06:40 PM Re: The Weekly Scripting Puzzle - December 2, 2005
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
doc, the point is that they are never answered.
so, actually, nobody ever really knows for sure if they solved it or not.
_________________________
!

download KiXnet

Top
#153417 - 2005-12-15 06:45 PM Re: The Weekly Scripting Puzzle - December 2, 2005
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Ok new policy then, I will post the answers when "the scripting guys" get around to posting their answers. Happy now, you basta ? ;0)
Top
#153418 - 2005-12-15 10:36 PM Re: The Weekly Scripting Puzzle - December 2, 2005
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: CA
Well, just saying... [The Scripting Guys do post the answer Jooel], so think we should post the answer at some time too and if all these "Challenges" were in the same post that would be even better I think.
 

Top
#153419 - 2005-12-15 10:45 PM Re: The Weekly Scripting Puzzle - December 2, 2005
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
all challenges in same post?
hmm...
don't know if there would be anything to gain with that.
but, easier browsing than learning to use the board search.
_________________________
!

download KiXnet

Top
#153420 - 2005-12-15 10:49 PM Re: The Weekly Scripting Puzzle - December 2, 2005
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: CA
Board search is not always that good. If they were all stored in the same post then we could post that link on FAQ or change the post to a STICKY even.
Top
#153421 - 2005-12-15 10:51 PM Re: The Weekly Scripting Puzzle - December 2, 2005
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: CA
Or so we don't spoil the question part of this... maybe have a 2 part post. 1 for the Question and 1 for the Answer.

Don't look at the answer unless you have no intention of figuring it out on your own and just want to see the anwer. For those that want to really learn they should first try to figure it out on their own before looking at the answer.
 

Top
#153422 - 2005-12-16 12:37 AM Re: The Weekly Scripting Puzzle - December 2, 2005
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Create a separate forum. Call it the 19th hole or something and mod it heavy so only certain people can start a new topic but anyone can reply.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#153423 - 2005-12-16 01:17 AM Re: The Weekly Scripting Puzzle - December 2, 2005
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
well i think in the short term, will just post the answer to the puzzle at the end of the thread that poses the puzzle.

By the way Ron ... I don't see anybody saying "I know whats wrong with this script!" ;0)

Top
#153424 - 2005-12-16 01:26 AM Re: The Weekly Scripting Puzzle - December 2, 2005
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
shawn, is the answer:
"I know what's wrong with the WMI"

accepted?
_________________________
!

download KiXnet

Top
#153425 - 2005-12-16 01:44 AM Re: The Weekly Scripting Puzzle - December 2, 2005
Shawn Administrator Offline
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
#153426 - 2005-12-16 06:40 PM Re: The Weekly Scripting Puzzle - December 2, 2005
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Still waiting for more folks to PM me with the "two part" answer to this puzzle ... one member is real close ... ;0)
Top
#153427 - 2005-12-16 07:45 PM Re: The Weekly Scripting Puzzle - December 2, 2005
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Hope my latest PM covers it.

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#153428 - 2005-12-16 07:51 PM Re: The Weekly Scripting Puzzle - December 2, 2005
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Yup, you answered why fetching the ProcessID in the second script doesn't work, and how to fix it.
Top
#153429 - 2005-12-20 05:09 PM Re: The Weekly Scripting Puzzle - December 2, 2005
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
So.. Who are the winner(s)?

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#153430 - 2005-12-20 06:26 PM Re: The Weekly Scripting Puzzle - December 2, 2005
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
I proclaim Kent as the winner - he was the only one that got the correct answer.
Top
#153431 - 2005-12-20 06:31 PM Re: The Weekly Scripting Puzzle - December 2, 2005
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
The correct answers are as follows:

1) Why does fetching the ProcessID in the second script not work ?

Because the WQL query was optimized to return only the name, like this:

Select Name from Win32_Process

If you wanted the ProcessID, would have to use Select * or Select ProcessID

2) If fetching the ProcessID in the second script doesn't work, why does fetching the Handle in the first script work ?

Because Handle is a "special" property of the Win32_Process object - its what is called a "key" value ... and key values always get returned with query, no matter what you specify.

Top
#153432 - 2005-12-20 07:12 PM Re: The Weekly Scripting Puzzle - December 2, 2005
Howard Bullock Offline
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.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
Page 1 of 2 12>


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 584 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.078 seconds in which 0.03 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org