Using the VBS implementation to prevent the computer from using LMHosts files _vbs

Source: Internet
Author: User
Tags constant

Ask:

Hello, Scripting Guy! How do I prevent my computer from using LMHosts files?

--MJ

For:

Hello, MJ. You know, this is an interesting question, at least for the Scripting Guys. Not because the script is hard to write; it's actually very simple. The difficulty is (at least for us) finding the LMHosts setting in the Windows GUI; This is what we need to do so we can verify that the script actually works. After a blind lookup (which is common to the Scripting Guys) we finally found what we were looking for:

In case you're as ignorant as the Scripting Guys (hopefully not for you), here's how to get to this dialog box:

1.

From Network Connections in Control Panel, select any network connections.

2.

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

3.

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

4.

View the WINS tab in the advanced TCP/IP Settings dialog box. Your settings are right here.

As we said, finding the LMHosts check box is the hardest part; clearing the check box (that is, preventing your computer from using LMHosts files) is simple:

 
   
   
Copy Code code as follows:

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 defined a pair of constants (Use_wins and Use_lmhost_file) at the beginning of the script and set them to False. We use constant use_wins to tell the script that we want to completely disable the use of WINS (Windows Internet naming Service). If this is not the case (if you want to continue using WINS, just not using the LMHosts file), the Use_wins value is set to True.

Also, constant Use_lmhost_file tells the script whether we want to use the LMHosts file. We set this constant to False because we will not use LMHosts. If you change your mind and decide to use LMHosts, simply set this constant to True.

Of course it's very simple. It is always easy to write a script to solve a problem.

Well, well: almost always.

The next step is to connect to the WMI service on the local computer (although we can also do this on the remote computer). Next is the following line of code:

Set objnetworksettings = Objwmiservice.get ("Win32_NetworkAdapterConfiguration")

You're right: This is a little unusual. In most WMI scripts, at this point we should call the ExecQuery method, and then ExecQuery to return a collection of objects for us to handle. You may have noticed that we did not use ExecQuery in this script at all. Why not use it? Well, theenablewins method (the method used to close WINS and LMHosts files) is a "static" method. A static method cannot manipulate a collection of objects, and it can only manipulate the class itself. This means that you bind to the Win32_NetworkAdapterConfiguration class ( using the Get method), and then call EnableWINS. The end result is that an instance of all classes (that is, all network adapters on the computer) will disable LMHosts. If you have more than one network adapter, there is no rule that allows LMHosts to be disabled on one adapter and enabling it on a different adapter. Can only be enabled or all disabled.

At this point, we only call the EnableWINS method, passing the constants use_wins and use_lmhost_file in sequence:

Errresult = Objnetworksettings.enablewins (Use_wins, Use_lmhost_file)

Notice that we have captured the return code (the result of the operation) in the variable errresult. We echo back this return code on the last line of the script. If Errresult equals 0, the operation succeeds and LMHosts is disabled. If Errresult is any value other than 0, well, that's a problem. In this case, you should check the WMI SDK for a detailed list of EnableWINS error codes.

The answer to this question is this. You now have a script that disables LMHosts, and the Scripting Guys know how to find LMHosts settings in the GUI. This is a win-win result!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.