![]() |
Table of Contents |
![]() |
Using the Query ResultsLet's look at a real-world use for WMI, and walk through the process of building the script. Suppose you want to modify a remote computer's network configuration so that all network adapters have DHCP enabled. Actually, you'll probably want to check multiple machines at once, so you'll need the script to read computer names from a text file that you'll create, using one computer name per line within the file. If the script finds that DHCP is already enabled, you want it to tell you so. NOTE A slightly more real-world task might be to modify the configuration only for a specific network adapter, like the one named Local Area Network, in each machine. That requires working with WMI associator classes, which I'll cover in the next chapter. The first part I like to handle is the WMI bit. I've found the Win32_NetworkAdapterConfiguration class, which has an EnableDHCP method that should do the job. I used the PrimalScript WMI Query Wizard to generate a template script for the class, and then trimmed it down to look like this. On Error Resume Next 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 "DHCPEnabled: " & objItem.DHCPEnabled WScript.Echo "Caption: " & objItem.Caption Next I need to have the script run through a text file, so I'll add the appropriate code. I showed you how to work with files and folders in Chapter 12. Dim strComputer Dim objWMIService Dim colItems Dim objFSO, objTS Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTS = objFSO.OpenTextFile("c:\input.txt") Do Until objTS.AtEndOfStream strComputer = objTS.ReadLine Set objWMIService = GetObject("winmgmts:\\" & _ strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery( _ "Select * from Win32_NetworkAdapterConfiguration",,48) For Each objItem in colItems WScript.Echo "DHCPEnabled: " & objItem.DHCPEnabled WScript.Echo "Caption: " & objItem.Caption Next Loop So far, this script is just displaying the caption and current DHCP status for each network adapter configuration. I need to add some logic to enable DHCP if it isn't already enabled. Dim strComputer Dim objWMIService Dim colItems Dim objFSO, objTS Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTS = objFSO.OpenTextFile("c:\input.txt") Do Until objTS.AtEndOfStream strComputer = objTS.ReadLine Set objWMIService = GetObject("winmgmts:\\" & _ strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery( _ "Select * from Win32_NetworkAdapterConfiguration",,48) For Each objItem in colItems If objItem.DHCPEnabled = True Then WScript.Echo "DHCP Enabled for: " & objItem.Caption Else WScript.Echo "Enabling DHCP for: " & objItem.Caption objItem.EnableDHCP End If Next Loop This modification has an If…Then construct examining the DHCPEnabled property, rather than simply displaying the property. If the property isn't True, the script executes the EnableDHCP method to turn on DHCP for the network adapter configuration. In either event, an appropriate message is displayed to let me know what's happening. |
![]() |
Table of Contents |
![]() |