Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I prevent a computer from using the LMHosts file?

-- MJ

SpacerHey, Scripting Guy! AnswerScript Center

Hey, MJ. You know, this was an interesting one, at least for the Scripting Guys. Not because the script was hard to write; that was actually pretty easy. What was hard - at least for us - was to find the LMHosts setting in the Windows GUI; that’s something we needed to do so we could verify that the script actually worked. After stumbling around a bit (which isn’t too unusual for the Scripting Guys) we eventually found what we were looking for:

LMHosts File

In case you’re as clueless as the Scripting Guys (and, for your sake, let’s hope not) here’s how we got to this dialog box:

1.

From Network Connections in Control Panel, pick any of your network connections.

2.

In the Properties dialog box for that connection select Internet Protocol (TCP/IP) and then click Properties.

3.

In the Internet Protocol (TCP/IP) Properties dialog box click Advanced.

4.

In the Advanced TCP/IP Settings dialog box, look on the WINS tab. There’s your setting.

Like we said, finding the LMHosts check box was the hard part; clearing the check box (and thus preventing the computer from using the LMHosts file) was easy:

On Error Resume Next
 
Const USE_WINS = False
Const USE_LMHOST_FILE = False

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objNetworkSettings = objWMIService.Get("Win32_NetworkAdapterConfiguration")
errResult = objNetworkSettings.EnableWINS(USE_WINS, USE_LMHOST_FILE)

Wscript.Echo errResult

We start this script off by defining a pair of constants - USE_WINS and USE_LMHOST_FILE - and setting both to False. We use the constant USE_WINS to tell the script that we want to disable the use of WINS (Windows Internet Name Service) altogether. If that’s not the case - if you want to continue to use WINS but just don’t want to use the LMHosts file - then set the value of USE_WINS to True.

Meanwhile, the constant USE_LMHOST_FILE tells the script whether we want to use the LMHosts file. We set this constant to False because we don’t want to use LMHosts. If you change your mind and decide you want to use LMHosts after all, then simply set this constant to True.

Of course it’s easy. With scripting it’s always easy.

Well, OK: almost always.

The next step is to connect to the WMI service on the local computer (although we can also perform this operation on a remote machine). That brings us to this line of code:

Set objNetworkSettings = objWMIService.Get("Win32_NetworkAdapterConfiguration")

You’re right: this is a bit unusual. In most WMI scripts we’d call the ExecQuery method at this point; in turn, ExecQuery would bring us back a collection of objects to work with. You might have noticed that we don’t use ExecQuery at all in this script. Why not? Well, the EnableWINS method (the method we use to turn off WINS and the LMHosts file) is a “static” method. A static method does not work on a collection of objects; instead, it works on the class itself. That means that you bind to the Win32_NetworkAdapterConfiguration class (using the Get method) and then call EnableWINS. The net effect: all instances of the class (that is, all the network adapters on your computer) will have LMHosts disabled. If you have multiple network adapters there’s no provision for disabling LMHosts on one adapter yet enabling it on another. It’s all or nothing.

At that point then we simply call the EnableWINS method, passing - in order - the constants USE_WINS and USE_LMHOST_FILE:

errResult = objNetworkSettings.EnableWINS(USE_WINS, USE_LMHOST_FILE)

Notice that we capture the return code (the results of the operation) in a variable named errResult. In the last line of the script we echo back this return code. If errResult is equal to 0, that means the operation succeeded and LMHosts has been disabled. If errResult is anything but 0, well, then something went wrong. In that case, you should check the WMI SDK for a detailed list of EnableWINS error codes.

And that’s about the size of it. You now have a script that disables LMHosts, and the Scripting Guys now know how to find the LMHosts setting in the GUI. A win-win situation!