Use vbs to prevent computers from using LmHosts files

Source: Internet
Author: User

Q:

Hello, script expert! How to Prevent computers from using LmHosts files?

-- MJ

A:

Hello, MJ. You know, this is an interesting question, at least for "script experts. It is not because the script is difficult to write; this is actually very simple. The difficulty is (at least for us) Finding LmHosts settings in the Windows GUI; this is what we need to do so that we can verify that the script actually works. After a blind search (which is common to the "scripting experts"), we finally found what we were looking:

In case you do not know anything like a scripting expert (for you, hopefully not), follow these steps to reach this dialog box:

1.

From "network connection" in "Control Panel"Select any network connection.

2.

"Attributes" connected here"Select "Internet Protocol (TCP/IP)" in the dialog box )"And then click "properties".

3.

In the "Internet Protocol (TCP/IP) Attribute"Click "advanced" in the dialog box".

4.

In "Advanced TCP/IP Settings"View "wins" in the dialog box"Tab. Your settings are here.

As we said, finding the LmHosts check box is the most difficult part. Clearing the check box (to prevent computers from using the LmHosts file) is simple:

CopyCodeThe Code is as follows:
On Error resume next
Const use_wins = false
Const use_lmhost_file = false

Strcomputer = "."
Set ob1_miservice = GetObject ("winmgmts: \" & strcomputer & "\ Root \ cimv2 ")

Set objnetworksettings = obw.miservice. 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) in the script and set them to false. We use the 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, but do not use the LmHosts file), set use_wins to true.

At the same time, the constant use_lmhost_file tells the script whether to use the LmHosts file. We set this constant to false because we will notUse LmHosts. If you change your mind and decide to use LmHosts, you only need to set this constant to true.

Of course, this is very simple. It is always easy to solve the problem by writing scripts.

Well, okay:Almost always.

The next step is to connect to the WMI Service on the Local Computer (although we can perform this operation on the remote computer ). Next is the following line of code:

 
Set objnetworksettings = obw.miservice. Get ("win32_networkadapterconfiguration ")

You are right: this is a bit unusual. In most WMI scripts, we should have calledExecqueryThen execquery returns a set of objects for us to process. You may have noticed that execquery is not used in this script at all. Why not? Well,EnablewinsMethod (used to close the wins and LmHosts files) is a "static" method. A static method cannot operate the set of objects, but it can only operate the class itself.. This indicates that you are bound to the win32_networkadapterconfiguration class (usingGetMethod), and then call enablewins. The final result is that LmHosts is disabled for all class instances (that is, all network adapters on the computer. If you have multiple network adapters, no rules allow you to disable LmHosts on one adapter and enable it on another adapter. You can only enable all or disable all.

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

 
Errresult = objnetworksettings. enablewins (use_wins, use_lmhost_file)

Note that the Returned Code (Operation Result) in the variable errresult is captured ). The Returned Code is displayed in the last line of the script. If errresult is equal to 0, the operation is successful and LmHosts is disabled. If errresult is notAny value of 0. Well, there is a problem. In this case, checkWMI SDKTo obtain a detailed list of enablewins error codes.

This is the answer to this question. You now have the script to disable LmHosts, while the script expert understands how to find The LmHosts settings in the GUI. This is a win-win result!

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.