{"id":67883,"date":"2006-02-24T11:05:00","date_gmt":"2006-02-24T11:05:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/02\/24\/how-can-i-prevent-a-computer-from-using-the-lmhosts-file\/"},"modified":"2006-02-24T11:05:00","modified_gmt":"2006-02-24T11:05:00","slug":"how-can-i-prevent-a-computer-from-using-the-lmhosts-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-prevent-a-computer-from-using-the-lmhosts-file\/","title":{"rendered":"How Can I Prevent a Computer from Using the LMHosts File?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I prevent a computer from using the LMHosts file?<BR><BR>&#8212; MJ<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>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 &#8211; at least for us &#8211; was to find the LMHosts setting in the Windows GUI; that\u2019s something we needed to do so we could verify that the script actually worked. After stumbling around a bit (which isn\u2019t too unusual for the Scripting Guys) we eventually found what we were looking for:<\/P><IMG border=\"0\" alt=\"LMHosts File\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/lmhosts.jpg\" width=\"368\" height=\"256\"> \n<P>In case you\u2019re as clueless as the Scripting Guys (and, for your sake, let\u2019s hope not) here\u2019s how we got to this dialog box:<\/P>\n<TABLE class=\"numberedList\" border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR vAlign=\"top\">\n<TD class=\"listNumber\" noWrap align=\"right\">\n<P>1.<\/P><\/TD>\n<TD>\n<P>From <B>Network Connections<\/B> in Control Panel, pick any of your network connections.<\/P><\/TD><\/TR>\n<TR vAlign=\"top\">\n<TD class=\"listNumber\" noWrap align=\"right\">\n<P>2.<\/P><\/TD>\n<TD>\n<P>In the <B>Properties<\/B> dialog box for that connection select <B>Internet Protocol (TCP\/IP) <\/B>and then click <B>Properties<\/B>.<\/P><\/TD><\/TR>\n<TR vAlign=\"top\">\n<TD class=\"listNumber\" noWrap align=\"right\">\n<P>3.<\/P><\/TD>\n<TD>\n<P>In the <B>Internet Protocol (TCP\/IP) Properties<\/B> dialog box click <B>Advanced<\/B>.<\/P><\/TD><\/TR>\n<TR vAlign=\"top\">\n<TD class=\"listNumber\" noWrap align=\"right\">\n<P>4.<\/P><\/TD>\n<TD>\n<P>In the <B>Advanced TCP\/IP Settings<\/B> dialog box, look on the <B>WINS<\/B> tab. There\u2019s your setting.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>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:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const USE_WINS = False\nConst USE_LMHOST_FILE = False<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set objNetworkSettings = objWMIService.Get(&#8220;Win32_NetworkAdapterConfiguration&#8221;)\nerrResult = objNetworkSettings.EnableWINS(USE_WINS, USE_LMHOST_FILE)<\/p>\n<p>Wscript.Echo errResult\n<\/PRE>\n<P>We start this script off by defining a pair of constants &#8211; USE_WINS and USE_LMHOST_FILE &#8211; 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\u2019s not the case &#8211; if you want to continue to use WINS but just don\u2019t want to use the LMHosts file &#8211; then set the value of USE_WINS to True. <\/P>\n<P>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 <I>don\u2019t<\/I> 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.<\/P>\n<P>Of course it\u2019s easy. With scripting it\u2019s always easy.<\/P>\n<P>Well, OK: <I>almost<\/I> always.<\/P>\n<P>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:<\/P><PRE class=\"codeSample\">Set objNetworkSettings = objWMIService.Get(&#8220;Win32_NetworkAdapterConfiguration&#8221;)\n<\/PRE>\n<P>You\u2019re right: this is a bit unusual. In most WMI scripts we\u2019d call the <B>ExecQuery<\/B> 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\u2019t use ExecQuery at all in this script. Why not? Well, the <B>EnableWINS<\/B> method (the method we use to turn off WINS and the LMHosts file) is a \u201cstatic\u201d method. A static method does not work on a collection of objects; instead, it works <I>on the class itself<\/I>. That means that you bind to the Win32_NetworkAdapterConfiguration class (using the <B>Get<\/B> 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\u2019s no provision for disabling LMHosts on one adapter yet enabling it on another. It\u2019s all or nothing.<\/P>\n<P>At that point then we simply call the EnableWINS method, passing &#8211; in order &#8211; the constants USE_WINS and USE_LMHOST_FILE:<\/P><PRE class=\"codeSample\">errResult = objNetworkSettings.EnableWINS(USE_WINS, USE_LMHOST_FILE)\n<\/PRE>\n<P>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 <I>but<\/I> 0, well, then something went wrong. In that case, you should check the <A href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/wmisdk\/wmi\/enablewins_method_in_class_win32_networkadapterconfiguration.asp\" target=\"_blank\"><B>WMI SDK<\/B><\/A> for a detailed list of EnableWINS error codes.<\/P>\n<P>And that\u2019s 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!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I prevent a computer from using the LMHosts file?&#8212; MJ 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 &#8211; at least for us &#8211; was to find [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[36,37,3,5],"class_list":["post-67883","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-management","tag-networking","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I prevent a computer from using the LMHosts file?&#8212; MJ 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 &#8211; at least for us &#8211; was to find [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67883","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67883"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67883\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}