{"id":64563,"date":"2007-06-28T00:14:00","date_gmt":"2007-06-28T00:14:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/06\/28\/how-can-i-remap-network-drives-so-they-use-an-ip-address-rather-than-a-computer-name\/"},"modified":"2007-06-28T00:14:00","modified_gmt":"2007-06-28T00:14:00","slug":"how-can-i-remap-network-drives-so-they-use-an-ip-address-rather-than-a-computer-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-remap-network-drives-so-they-use-an-ip-address-rather-than-a-computer-name\/","title":{"rendered":"How Can I Remap Network Drives So They Use an IP Address Rather Than a Computer Name?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I remap network drives so they point to the IP address of the remote computer rather than the computer name?<BR><BR>&#8212; YM <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, YM. You know, <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jun07\/hey0626.mspx\"><B>yesterday<\/B><\/A> we started off this column by saying, \u201cNever mind,\u201d referring to the fact that, in a previous article, the Scripting Guy who writes this column lamented the fact that nothing interesting ever happened to him. So how are we going to start off today\u2019s column? Like this: never mind.<\/P>\n<P>Yesterday we noted that the Scripting Guy who writes this column was the \u2026 joyful \u2026 owner of a car that had experienced a \u201cfailure to proceed.\u201d According to the preliminary diagnosis, the car had a broken ECM (which, as it turns out, is actually an <I>ECU<\/I>), and the Scripting Guy who writes this column appeared well on his way to being \u201cthe Scripting Guy who just spent several thousand dollars getting his stupid car fixed.\u201d<\/P>\n<P>Now, we should make clear that there was absolutely no question that the car was broken: even the mechanics were unable to get it to start, or even to get it to come <I>close<\/I> to starting. However, when the mechanics ran their computerized diagnostic tests everything came up just fine: apparently there was absolutely nothing wrong with the car whatsoever. (Well, aside from the fact that it wouldn\u2019t actually start.) No matter what the mechanics tried, they couldn\u2019t find a problem. <I>There was nothing wrong with the car whatsoever<\/I>.<\/P>\n<P>Well, aside from the fact that it wouldn\u2019t actually start.<\/P>\n<P>At some point in the process one of the mechanics discovered a fuse that appeared to have burned out. This fuse is responsible for displaying warning lights on the dashboard; it has nothing to do with starting the car. And yet, once the fuse was replaced the car started right up. \u201cIt doesn\u2019t make any sense,\u201d said the mechanic. \u201cI can\u2019t tell you why that seemed to fix the problem. In fact, there\u2019s no way that it <I>could<\/I> fix the problem. It doesn\u2019t make any sense at all.\u201d<\/P>\n<P>And yet, so far so good. Not that the Scripting Guy who writes this column is complaining. Instead of paying out several thousands dollars, he paid $124.25: $123.00 for labor and $1.25 for parts.<\/P>\n<P>Which, if you do the math, is <I>way<\/I> better than spending $2000 &#8211; $3000 to get your car fixed. <\/P>\n<P>Needless to say, the Scripting Guy who writes this column felt like celebrating his good fortune. And what better way to celebrate good news than to write a script that remaps network drives so these drives point to the IP address of the remote computer rather than the computer name:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nSet colDrives = objNetwork.EnumNetworkDrives<\/p>\n<p>For i = 0 to colDrives.Count-1 Step 2\n    strDriveLetter = colDrives.Item(i)\n    strNetworkPath = colDrives.Item(i + 1)<\/p>\n<p>    strNetworkPath = Replace(strNetworkPath, &#8220;\\\\&#8221;, &#8220;&#8221;)\n    arrPath = Split(strNetworkPath, &#8220;\\&#8221;)<\/p>\n<p>    strComputerName = arrPath(0)\n    strFolderName = arrPath(1)<\/p>\n<p>    Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputerName &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>    Set colItems = objWMIService.ExecQuery _\n        (&#8220;Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True&#8221;)<\/p>\n<p>    For Each objItem in colItems\n        For Each objAddress in objItem.IPAddress\n            strIPAddress = objAddress\n        Next\n    Next<\/p>\n<p>    objNetwork.RemoveNetworkDrive strDriveLetter<\/p>\n<p>    Wscript.Sleep 3000<\/p>\n<p>    strNewPath = &#8220;\\\\&#8221; &amp; strIPAddress &amp; &#8220;\\&#8221; &amp; strFolderName\n    objNetwork.MapNetworkDrive strDriveLetter, strNewPath\nNext\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"E4D\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note:<\/B> If you\u2019re running this script on Windows Vista, you need to with elevated privileges for the script to work correctly. Open a command prompt by right-clicking and selecting Run As Administrator, then run your script from the command prompt.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Let\u2019s see if we can figure out how this script works. To begin with, we should note that we started things off with the <B>On Error Resume Next<\/B> statement. Typically we don\u2019t do that in these articles; as a general rule we leave out error-handling in order to keep the scripts a little shorter and a little easier to read. In this case, however, we occasionally got some weird errors when calling the RemoveNetworkDrive method, errors that would cause the script to crash. <I>Why<\/I> did we get those errors? Well, to tell you the truth, we aren\u2019t really sure. But adding On Error Resume Next seemed to solve the problem, so \u2026.<\/P>\n<P>Although we assume that replacing a fuse in the Scripting Car would have solved the problem, too.<\/P>\n<P>As for the <I>real<\/I> code, our next step is to create an instance of the <B>Wscript.Network<\/B> object, then use the following line to return a collection of all the mapped network drives on the computer:<\/P><PRE class=\"codeSample\">Set colDrives = objNetwork.EnumNetworkDrives\n<\/PRE>\n<P>The <B>EnumNetworkDrives<\/B> method does a perfectly fine job of returning all the mapped drives on a computer. However, it does so in slightly-quirky fashion: the returned information comes back as a collection that looks like this:<\/P><PRE class=\"codeSample\">X:\n\\\\atl-fs-01\\public\nY:\n\\\\atl-fs-01\\users\nZ:\n\\\\atl-fs-01\\scripts\n<\/PRE>\n<P>As you can see, each mapped drive encompasses <I>two<\/I> items in the collection; one item (e.g., X:) representing the drive letter, the other item (\\\\atl-fs-01\\public) representing the network path. That\u2019s why we resort to this odd-looking For Next loop when we start iterating through the collection:<\/P><PRE class=\"codeSample\">For i = 0 to colDrives.Count-1 Step 2\n<\/PRE>\n<P>As you can see, we start the loop with the counter variable i equal to 0; that\u2019s because the first item in any array or collection always has the index number 0. We end the loop when we reach the number of items in the collection (<B>Count<\/B>) minus 1. Why minus 1? Because the very last item in the collection will be the network path; for purposes of the loop, we only care about the drive letter. That\u2019s also why we added the <B>Step 2<\/B> parameter, which causes us to hit every other item in the collection. That way we grab only the drive letters \u2013 X:, Y:, Z: \u2013 and avoid the problem of grabbing the same drive twice (first by drive letter, the second time by network path).<\/P>\n<P>Inside the loop, we start out by assigning the drive letter to a variable named strDriveLetter:<\/P><PRE class=\"codeSample\">strDriveLetter = colDrives.Item(i)\n<\/PRE>\n<P>We then use this line of code to assign the network path to a variable named strNetworkPath:<\/P><PRE class=\"codeSample\">strNetworkPath = colDrives.Item(i + 1)\n<\/PRE>\n<P>Note the index number we used here: <B>i + 1<\/B>. That\u2019s because the network path associated with a particular drive letter will always have an index number 1 more than the drive letter. If drive Y: has the index number 2 then the path \\\\atl-fs-01\\users will have the index number 3.<\/P>\n<P>A little weird, but that\u2019s the way it works.<\/P>\n<P>Of course, the Scripting Guys know that, in order to map a drive using a computer\u2019s IP address, it\u2019s helpful if you actually <I>know<\/I> the computer\u2019s IP address. One way to determine the IP address is to use the <B>Win32_NetworkAdapterConfiguration<\/B> class. Before we can use that class, however, we need to parse the computer name from the network path. That\u2019s what this block of code is for:<\/P><PRE class=\"codeSample\">strNetworkPath = Replace(strNetworkPath, &#8220;\\\\&#8221;, &#8220;&#8221;)\narrPath = Split(strNetworkPath, &#8220;\\&#8221;)<\/p>\n<p>strComputerName = arrPath(0)\nstrFolderName = arrPath(1)\n<\/PRE>\n<P>Let\u2019s see if we can explain what\u2019s going on here. To begin with, we use the VBScript <B>Replace<\/B> function to replace any instances of \\\\ with, well, nothing. That\u2019s going to turn a network path like \\\\atl-fs-01\\public into this:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>atl-fs-01\\public<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>We then use the <B>Split<\/B> function to split that value on the \\ character. That results in an array named arrPath, an array consisting of the following two items:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>atl-fs-01<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>public<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>We next assign item 0 (atl-fs-01) to a variable named strComputerName, then assign item 1 (public) to a variable named strFolderName. We now know two things: the name of the computer where the mapped folder resides, as well as the name of that mapped folder.<\/P>\n<P>Wait a second. As it turns out, we actually know <I>three<\/I> things: we also know that we now have enough information to connect to the remote computer atl-fs-01 and determine its IP address. That\u2019s what this block of code is for:<\/P><PRE class=\"codeSample\">Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputerName &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True&#8221;)\n<\/PRE>\n<P>All we\u2019re doing here is connecting to the WMI service on the remote computer, then retrieving a collection of network adapters (using the <B>Win32_NetworkAdapterConfiguration<\/B> class). Take careful note of the Where clause we included in our query:<\/P><PRE class=\"codeSample\">Where IPEnabled = True\n<\/PRE>\n<P>If you query the Win32_NetworkAdapterConfiguration class without using this Where clause you\u2019re likely to get back 10 or more network adapters; for better or worse, most of these will be virtual adapters, adapters that aren\u2019t tied to an actual network card and don\u2019t have an IP address. By limiting returned data to network adapters where the <B>IPEnabled<\/B> property is True we\u2019ll only get back network adapters that <I>do<\/I> have an IP address.<\/P>\n<P>That brings us to this block of code:<\/P><PRE class=\"codeSample\">For Each objItem in colItems\n    For Each objAddress in objItem.IPAddress\n        strIPAddress = objAddress\n    Next\nNext\n<\/PRE>\n<P>As it turns out, a network adapter can have more than one IP address; because of that the <B>IPAddress<\/B> property stores values as an array. In turn, that means that we need to use a For Each loop to get at each of these values. This block of code loops through the returned set of IP-enabled network adapters and then, for each adapter, loops through the assigned IP addresses. We have no idea of knowing which IP address is the \u201cbest\u201d so we simply take the last address we find and assign it to a variable named strIPAddress:<\/P><PRE class=\"codeSample\">strIPAddress = objAddress\n<\/PRE>\n<P>At long last, we\u2019re now ready to remap our first network drive. Except for one thing: there\u2019s no RemapNetworkDrives method. Instead, we have to disconnect the existing drive (the one mapped using the computer name) and then create a new network drive (using the same drive letter) that maps to the IP address instead. Step 1 in that process? Disconnecting the existing drive:<\/P><PRE class=\"codeSample\">objNetwork.RemoveNetworkDrive strDriveLetter\n<\/PRE>\n<P>Next we use the <B>Sleep<\/B> method to pause the script for 3 seconds (3,000 milliseconds); that helps ensure that the existing drive is disconnected before we try mapping a new drive to that drive letter. After a brief pause we then create a new network path, this one using the IP address (strIPAddress) rather than the computer name:<\/P><PRE class=\"codeSample\">strNewPath = &#8220;\\\\&#8221; &amp; strIPAddress &amp; &#8220;\\&#8221; &amp; strFolderName\n<\/PRE>\n<P>And once we have the network path we can then call the <B>MapNetworkDrive<\/B> method and map the drive to the IP address:<\/P><PRE class=\"codeSample\">objNetwork.MapNetworkDrive strDriveLetter, strNewPath\n<\/PRE>\n<P>That\u2019s going to take drive X:, which used to map to \\\\atl-fs-01\\public, and remap it to the IP address (e.g., \\\\192.168.1.1\\public). And then we loop around and repeat the process with the next mapped drive.<\/P>\n<P>So now that the Scripting Car problems have been resolved (we hope) does that mean that the Scripting Guy who writes this column no longer needs you to send him $2,000? Heavens no; where would you get an idea like <I>that<\/I>? Granted the Scripting Car seems to be running OK, but for some reason the Scripting Guy who writes this column isn\u2019t 100% confident that replacing a fuse that has absolutely nothing to do with the problem has somehow actually fixed the problem. And, even if it <I>has<\/I>, well, have we mentioned the fact that the Scripting Son will be headed off for college in another year? If we haven\u2019t, don\u2019t worry about it: no doubt you\u2019ll have plenty of chances to hear all about that (and plenty of time to mail in your $2,000) before the big day comes. <I>(<\/I><I>Ed<\/I><I>itor\u2019s Note: We\u2019re doing our best to keep the Scripting Guy\u2019s begging to a minimum, but it\u2019s a \u2026 challenge \u2026 to say the least. We would apologize to anyone who was feeling obligated to actually send this Scripting Guy money if we believed there was anyone like that out there.)<\/I><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I remap network drives so they point to the IP address of the remote computer rather than the computer name?&#8212; YM Hey, YM. You know, yesterday we started off this column by saying, \u201cNever mind,\u201d referring to the fact that, in a previous article, the Scripting Guy who writes this [&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":[3,185,12,5],"class_list":["post-64563","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-shared-folders-and-mapped-drives","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I remap network drives so they point to the IP address of the remote computer rather than the computer name?&#8212; YM Hey, YM. You know, yesterday we started off this column by saying, \u201cNever mind,\u201d referring to the fact that, in a previous article, the Scripting Guy who writes this [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64563","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=64563"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64563\/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=64563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}