Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use vbs to prevent computers from using LMHosts files

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use vbs to prevent computers from using LMHosts files, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Q:

How can I prevent my computer from using LMHosts files?

A:

This is an interesting question, at least for scripting experts. It's not because scripting is hard to write; it's actually very simple. The difficulty is (at least for us) to find the LMHosts setting in Windows GUI; that's what we need to do so we can verify that the script actually works. After a blind search (which is common for scripting experts), we finally found what we were looking for:

In case you are as ignorant as scripting expert (hopefully not for you), here are the steps to get to this dialog:

1.

From Network connections in the Control Panel, select any network connection.

two。

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

3.

Click Advanced in the Internet Protocol (TCP/IP) Properties dialog box.

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 computers from using LMHosts files) is 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 define a pair of constants (USE_WINS and USE_LMHOST_FILE) at the beginning of 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, just not using the LMHosts file), 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 or not. 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's always easy to solve problems by writing scripts.

Yeah, 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). Here is the following line of code:

Set objNetworkSettings = objWMIService.Get ("Win32_NetworkAdapterConfiguration")

You're right: it's a little unusual. In most WMI scripts, we should have called the ExecQuery method at this point, and then ExecQuery returned a collection of objects for us to process. You may have noticed that we didn't use ExecQuery at all in this script. Why not use it? Well, the EnableWINS method (the method used to close WINS and LMHosts files) is a "static" method. A static method cannot manipulate a collection of objects; 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 instances of all classes (that is, all network adapters on the computer) will disable LMHosts. If you have multiple network adapters, there are no rules that allow you to disable LMHosts on one adapter and enable it on another. It can only be enabled or disabled all.

At this point, we just call the EnableWINS method, passing the constants USE_WINS and USE_LMHOST_FILE in turn:

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 this return code on the last line of the script. If errResult equals 0, the operation is successful and LMHosts is disabled. If errResult is any value other than 0, well, that's a problem. In this case, you should check WMI SDK for a detailed list of EnableWINS error codes.

Thank you for reading this article carefully. I hope the article "how to use vbs to prevent computers from using LMHosts files" shared by the editor will be helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report