Learning Windows Scripting.. pff
After my last post about VBScript/WSH I thought I would bite the bullet and get myself a couple of books to learn about Windows Scripting to assist with network management, logon scripting etc.
I bought two books, Microsoft Press Windows Scripting Self-Paced Learning Guide and Windows Admin Scripting Little Black Book Second Edition.
The obvious place to start was with the book that will teach me about Windows Scripting so I begin on Saturday afternoon. 164 pages later and I’m on Lab 18a which is the DIY section of the chapters. A great idea to actually use VBSCript to achieve what you want to do. This particular chapter deals with WMI and accessing the WMI namespace to retrieve data that you can use for reporting or whatever really. So the lab runs you through creating a script which ends up looking exactly like this:
Option Explicit
Dim objItem, objWMIService
Dim colItems
Set objWMIService = GetObject("winmgmts:\\")
Set colItems = objWMIService.ExecQuery("SELECT * FROM AddRemovePrograms")
For Each objItem in colItems
WScript.Echo "Display Name: " & objItem.DisplayName
WScript.Echo "Publisher: " & objItem.Publisher
WScript.Echo "Version: " & objItem.Version
WScript.Echo
Next
Simple enough yes? Well no actually, no it’s not. When you come to run this script you get an error. Believe me there’s nothing wrong with this script per say. It’s syntactically correct but there is a problem. This isn’t a small problem, no no, it’s gigantic. Especially to someone that’s learning a subject. The problem lies in this bit of code:
Set colItems = objWMIService.ExecQuery("SELECT * FROM AddRemovePrograms")
Getting results from that particular provider is going to be a task and a half because it DOES NOT EXIST! The error code generated basically means that the class doesn’t exist so forget it. Fortunately there IS a way to get this script working which I managed to find last night. Fortunately for me I can find solutions to problems but what about the thousands of other people out there scratching their heads wondering why their code doesn’t work? The author is so nonchalant about this particular script that you’ll stare at it for hours wondering why your code doesn’t work and his obviously does. (Check his, you’ll see it doesn’t work either)
As you can tell I’m a little cheesed off about this particular error in the book but happy enough that I’ve found a solution. I’ll be reporting it to MS Press in due course but for now here’s the web page to follow so you can fix it.
Dx21 WMI Reference (Under Construction)
NB: Watch out for their typo too! When you come to enable the MOF (go read the page) you’ll see their command line includes the word EnableAddRemovePrograms.mof. Just remove the word Enable from the beginning so that it looks like: mofcomp AddRemovePrograms.mof
– Lewis
Thanks! I’ve been going through the same book and came across that piece of code, fortunately, I don’t have much patience so I googled the query (“select * from AddRemovePrograms”) and came across your posting. What a relief. I hope you’ve already sent in a correction to MS Press.
No problem Mathenge, I have reported it to MS Press but get the feeling that this particular query may be geared towards Windows Server 2003. I’ve not had the opportunity to test against 2003 but I will be doing so in due course and will of course report back once I have.
For now, if you are scripting in the Enterprise then installing the .MOF fix I mentioned in the post is probably not such a simple thing to do. It is doable by all means but it’s easier to use something tried and true. Have a look at another post I’ve done on getting all programs installed on a PC, it will of course work remotely: http://www.lewisroberts.com/wp-trackback.php?p=28
– Lewis
I’ve just tested the script against a Windows 2003 server with the same result. It is a pitty bookwriters do not test their scripts on new machines. It would save us a lot of trouble. Anyway, thanks for the remarks. It saved me some frustration.