Previous Section Table of Contents Next Section

Writing the Query in VBScript

Now it's time to incorporate the query into a script. This time, I'll start with the shell of the script, which will read the computer names from the text file.


Dim oFSO, oTS, sComputer



Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oTS = oFSO.OpenTextFile("c:\input.txt")

Do Until oTS.AtEndOfStream



 sComputer = oTS.ReadLine



Loop

That is easy enough. Now, for each, I need to retrieve a specified instance of Win32_NetworkAdapter. The caption I'm looking for-"Local Area Connection"-is stored in a property named NetConnectionID.

TIP

How did I know which property to use? Simple: Wbemtest. I clicked EnumInstances and typed Win32_NetworkAdapter as the superclass name. Then, I double-clicked on the first instance that was returned to display its properties. I scrolled down, looking for "Local Area Connection" in the values column, and I found it in a property named NetConnectionID. If I hadn't found "Local Area Connection" at all, I would have tried the next instance in the list, and kept browsing until I found it.


Actually, I don't want to retrieve the Win32_NetworkAdapter instance at all. Instead, I need to retrieve all associated Win32_NetworkAdapterConfiguration instances. However, as I discovered earlier, I need to retrieve the DeviceID on my own, based on a simpler WQL query. Here's the modified script.


Dim oFSO, oTS, sComputer

Dim oWMI, oConfigs, oConfig, oAdapters, oAdapter



Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oTS = oFSO.OpenTextFile("c:\input.txt")



Do Until oTS.AtEndOfStream



 sComputer = oTS.ReadLine



 oWMI = GetObject("winmgmts:\\" & _

  sComputer & "\root\cimv2")



 Set oAdapters = oWMI.ExecQuery( _

  "SELECT DeviceID FROM Win32_NetworkAdapter " & _

  "WHERE NetConnectionID = 'Local Area Connection')



 For Each oAdapter in oAdapters

  Set oConfigs = oWMI.ExecQuery( _

   "ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & _

   oAdapter.DeviceID & "'} " & _

   "WHERE RESULTCLASS = Win32_NetworkAdapterConfiguration")



 Next



Loop

Of course, simply retrieving the class doesn't do anything. Keep in mind that oConfigs will contain a collection of Win32_NetworkAdapterConfiguration instances, although in almost all cases the collection will only contain one instance. I'll need to loop through the instances and check each one to see if DHCP is enabled. Here's how.


Dim oFSO, oTS, sComputer

Dim oWMI, oConfigs, oConfig



Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oTS = oFSO.OpenTextFile("c:\input.txt")



Do Until oTS.AtEndOfStream



 sComputer = oTS.ReadLine



 oWMI = GetObject("winmgmts:\\" & _

  sComputer & "\root\cimv2")



 Set oAdapters = oWMI.ExecQuery( _

  "SELECT DeviceID FROM Win32_NetworkAdapter " & _

  "WHERE NetConnectionID = 'Local Area Connection')



 For Each oAdapter in oAdapters



  Set oConfigs = oWMI.ExecQuery( _

   "ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & _

   oAdapter.DeviceID & "'} " & _

   "WHERE RESULTCLASS = Win32_NetworkAdapterConfiguration")



  For Each oConfig In oConfigs



   If oConfig.DHCPEnabled Then

    WScript.Echo "DHCP Enabled on " & sComputer

   Else

    WScript.Echo "Enabling DHCP on " & sComputer

    oConfig.EnableDHCP

   End If



  Next



Next



Loop

That's it! The script will read the text file and set each computer's "Local Area Connection" to use DHCP. If you'd like to test it, Listing 19.1 shows the complete listing, along with in-line comments.

Listing 19.1. SetDHCP.vbs. This script sets the Local Area Connection adapter to use DHCP for each computer named in text file.

Dim oFSO, oTS, sComputer

Dim oWMI, oConfigs, oConfig



'get a filesystemobject and open the input file

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oTS = oFSO.OpenTextFile("c:\input.txt")



'for each line of the input file...

Do Until oTS.AtEndOfStream



 'read the computer name from the file

 sComputer = oTS.ReadLine



 'connect to WMI on the remote computer

 oWMI = GetObject("winmgmts:\\" & _

  sComputer & "\root\cimv2")



 'query a collection of Win32_NetworkAdapter

 'instances that have a NetConnectionID of

 ' Local Area Connection

 Set oAdapters = oWMI.ExecQuery( _

  "SELECT DeviceID FROM Win32_NetworkAdapter " & _

  "WHERE NetConnectionID = 'Local Area Connection')



 'for each of those adapters...

 For Each oAdapter in oAdapters

  'query the associated network adapter configurations

  Set oConfigs = oWMI.ExecQuery( _

   "ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & _

   oAdapter.DeviceID & "'} " & _

   "WHERE RESULTCLASS = Win32_NetworkAdapterConfiguration'")



  'for each of those configurations...

  For Each oConfig In oConfigs



   'is DHCP enabled?

   If oConfig.DHCPEnabled Then



    'yes - display a message

    WScript.Echo "DHCP Enabled on " & sComputer



   Else



    'no - display a message and enable it

    WScript.Echo "Enabling DHCP on " & sComputer

    oConfig.EnableDHCP



   End If



  Next



Next



Loop

You'll need to provide the appropriate input file, c:\input.txt, in order to use this script.

    Previous Section Table of Contents Next Section