MapNetworkDrive – Basic VBscript
Our objective is to map the H: to a share called ‘\printers’. In my example the server is called ‘\\server’, therefore the full UNC path is \\server\printers. We will be using Microsoft’s VBScript and the code executes on any WSH client. The best environment would be Windows Server 2003 with an XP client. However, to practice MapNetworkDrive scripts, you could create a share on your own desktop computer.
Pre-requisites
You need a computer with a network share. Therefore go to Line 2 of my script and change the name of the server from ‘\\server’ to the name of your server.
Either make sure that your shared folder is called ‘\printers’, or alternatively, alter the word ‘\printers’ in the script to match the name of your UNC share.
Copy and paste – save as MapDisk.vbs with conent:
Set objNetwork = CreateObject(“WScript.Network”)
objNetwork.MapNetworkDrive “H:” , “\\server\printers”
Note:
- VBScript is an object based scripting language. On line 1 you can see how the code creates objNetwork. Line 2 then applies the famous MapNetworkDrive method to achieve the actual drive mapping.
- If you run the script a second time, then you get WSH error message code 80070055. The solution is launch Windows Explorer, and then just right click on: home on ‘alan’ (H:) and select: Disconnect. (See diagram opposite). Now you can run the script again. Alternatively, you could create a fancy script, which maps to the first free drive letter, however that is difficult task.
- You could try mapping to a different drive letter, there is nothing magical about the H:\. MapNetworkDrive works just as well with the J:\ or Z:\.
- I reduced this first example script to the bare minimum. All I wanted was for you to gain success mapping a network drive. The next examples are more typical of production Microsoft VBScripts because they introduce variables, comments and a header.
loading...
loading...
