![]() |
Table of Contents |
![]() |
Really-It's This EasyIn my conference lectures on scripting, I always try to prove how easy WMI scripting really is. I usually ask students to call out some piece of computer information that they'd like to be able to query. Believe me, I haven't memorized the hundreds of WMI classes that are available, so it's unlikely that I'll already know how to query whatever they ask for. It's a great way to show how a little documentation and a couple of tools can quickly result in a powerful WMI script. For example, suppose you need to query a server to see if any persistent routes have been added by using the route -p add command. No problem. Here are the four steps to writing almost any WMI script. Find the ClassFirst, I have to figure out what to query. This is easily the toughest part of the entire process. I usually start in the WMI Reference documentation, looking at the five categories of Win32 classes:
Of these five, Operating System seems to be the most likely choice for routing information, so I'll expand that topic. Unfortunately, that leaves me with a whole bunch of classes still to work through. Fortunately, they're alphabetical, so I can scroll right down to the R section and look for something like Win32_Route. Nope, nothing. In fact, Win32_Registry is the only thing under R, and that clearly isn't it. Idly scrolling back up, I do see Win32_IP4RouteTable. Aha! That makes sense; Windows XP and Server 2003 both support IPv4 and IPv6; WMI clearly needs some way to distinguish between the two. Looking more closely, I also see Win32_IP4PersistedRouteTable, which looks exactly like what I want. Here's what the Microsoft MSDN Library has to say about Win32_IP4PersistedRouteTable.
That last sentence gives me some pause: "This class was added for Windows Server 2003 family." Scrolling to the bottom of the page reveals that the class is present in Windows XP and Windows Server 2003, meaning I cannot use this on Windows 2000. That's not unusual; as WMI becomes more popular, Microsoft expands it to include more and more aspects of the operating system. For the sake of argument, let's say I'm working entirely with Windows Server 2003 servers, which means I'll have access to this class. The documentation does imply that I can use this class to delete entries, but I'm just interested in seeing if any exist to begin with right now. Write the Query, Test the QueryI have to write a WQL query that will retrieve all instances of this class. Something like SELECT * FROM Win32_IP4PersistedRouteTable should do the trick. Time to fire up Wbemtest and try the query. After running it on my Windows XP machine, just to try it out, I get a results dialog box like the one in Figure 17.9. Sure enough, I have a persisted route on my laptop! According to the properties shown, the route's destination is for 63.171.9.180. Figure 17.9. Examining returned instances of Win32_IP4PersistedRouteTableWhat if my laptop is more typical and doesn't have any persistent routes? My query would return nothing, and there's a valuable lesson: When testing your queries, always make sure there's something for them to return. In this case, create a persistent route, if necessary; that way, you'll be able to tell if your query is working properly. Double-clicking the instance reveals all the properties of the class, with the values for this instance, as shown in Figure 17.10. Figure 17.10. Properties of my persisted routeWrite the ScriptRemember that WMI query script I showed you in Listing 17.1? Here it is again just as a reference. Dim strComputer Dim objWMIService Dim colItems strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & _ strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery( _ "Select * from Win32_NetworkAdapterConfiguration",,48) For Each objItem in colItems WScript.Echo "IPAddress: " & objItem.IPAddress WScript.Echo "MACAddress: " & objItem.MACAddress WScript.Echo "MTU: " & objItem.MTU Next This is a generic WMI query script, and I just need to adapt it to my current needs. I've provided you with several other template scripts in Chapter 32, which will help you get just about anything you want out of WMI. For this example, I first need to replace the query with the one I just wrote and tested.
Set colItems = objWMIService.ExecQuery( _
"Select * from Win32_IP4PersistedRouteTable",,48)
Next, I need to modify the properties that are being used. After all, a persisted route doesn't have a MAC address or an MTU, which were both used in the original script. I want the script to display the route's caption, which tells me its destination address.
For Each objItem in colItems
WScript.Echo "Route to: " & objItem.Caption
Next
I'd also like the script to count the persisted routes and tell me the total of how many it finds. I can add that information easily. Dim iCounter For Each objItem in colItems iCounter = iCounter + 1 WScript.Echo "Route to: " & objItem.Caption Next WScript.Echo iCounter & " routes were found." Finally, I'd like the script to connect to a specified server, not just my local machine. Again, a relatively simple change in VBScript. strComputer = InputBox("Enter server name to check") Set objWMIService = GetObject("winmgmts:\\" & _ strComputer & "\root\cimv2") That's it. Listing 17.2 shows the final, completed script that should do what I need. Listing 17.2. CheckRoutes.vbs. Checks for persisted IPv4 routes on a specified 2003 or XP machine.Dim strComputer Dim objWMIService Dim colItems strComputer = InputBox("Enter server name to check") Set objWMIService = GetObject("winmgmts:\\" & _ strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery( _ "Select * from Win32_IP4PersistedRouteTable",,48) Dim iCounter For Each objItem in colItems iCounter = iCounter + 1 WScript.Echo "Route to: " & objItem.Caption Next WScript.Echo iCounter & " routes were found." You should be able to type this in (or copy it from the CD-ROM accompanying this book) and run it as-is. Try the ScriptThe last step, of course, is to try it. This example should work perfectly; if your future scripts don't work so well, just debug them one error at a time. Following these four simple steps, you've accomplished quite a bit: You located an appropriate WMI class, you created and tested a WQL query, you modified a template script to meet your needs, and you tested the script. That's all there is to it! |
![]() |
Table of Contents |
![]() |