Network drives and printers
In Windows environment we often need to use drives located on other computers on the network.
These network drives, of course, should be shared (allowed to use remotely). To use a shared drive we need to
map it. We can map a network share to a drive letter by using some methods of the WshNetwork object.
The same object WshNetwork allows us to work with network printers as well.
WshNetwork object
To create an instance of the WshNetwork object we need to do something very similar to what we were doing
when we were creating an instance of the WshShell object. Namely,
we need to use the CreateObject method of the WScript object with parameter
"WScript.Network".
var nwobj = WScript.CreateObject("WScript.Network");
Once an instance of the object created we can use its properties and methods.
WshNetwork properties and methods
| Property | Description |
| ComputerName |
Returns the name of the computer system. |
| UserDomain |
Returns a user's domain name. |
| UserName |
Returns the name of a user. |
| Method |
Description |
| AddWindowsPrinterConnection |
Adds a Windows-based printer connection to your computer system. |
| AddPrinterConnection |
Adds a remote MS-DOS-based printer connection to your computer system. |
| EnumNetworkDrives |
Returns the current network drive mapping information. |
| EnumPrinterConnection |
Returns the current network printer mapping information. |
| MapNetworkDrive |
Adds a shared network drive to your computer system. |
| RemoveNetworkDrive |
Removes a shared network drive from your computer system. |
| RemovePrinterConnection |
Removes a shared network printer connection from your computer system. |
| SetDefaultPrinter |
Assigns a remote printer the role Default Printer. |
Working with network shares
There are three methods of the WshNetwork object that work with network shares:
- MapNetworkDrive()
- RemoveNetworkDrive()
- EnumNetworkDrives()
Let's take a closer look at these methods.
MapNetworkDrive()
The general syntax of the method is
MapNetworkDrive(LocalName, RemoteName, [UpdateProfile], [User], [Password])
where
LocalNameString value indicating the name by which the mapped drive will be known locally.
RemoteNameString value indicating the share's UNC name (\\xxx\yyy).
UpdateProfile
Optional. Boolean value indicating whether the mapping information is stored in the current user's profile.
If UpdateProfile is supplied and has a value of true, the mapping is stored in the user profile
(the default is false).
User
Optional. String value indicating the user name. You must supply this argument if you are mapping a network drive using
the credentials of someone other than the current user.
Password
Optional. String value indicating the user password. You must supply this argument if you are mapping a network drive using
the credentials of someone other than the current user.
For example, to map a network share myshare located on the computer remote_comp to the letter
"Y" and make this mapping persistent (that is, restored when the same user logs on again) we need to execute code like this
var nwobj = WScript.CreateObject("WScript.Network");
nwobj.MapNetworkDrive("X:", "\\\\remote_comp\\myshare", true);
RemoveNetworkDrive()
The general syntax of the method is
RemoveNetworkDrive(Name, [Force], [UpdateProfile])
where
Name
String value indicating the name of the mapped drive you want to remove. The Name parameter can be either a local
name or a remote name depending on how the drive is mapped.
Force
Optional. Boolean value indicating whether to force the removal of the mapped drive. If Force is supplied and its
value is true, this method removes the connections whether the resource is used or not.
UpdateProfile
Optional. String value indicating whether to remove the mapping from the user's profile. If UpdateProfile is supplied
and its value is true, this mapping is removed from the user profile. UpdateProfile is false by
default.
For example, to disconnect the drive mapped in the previous example even if it is currently using, but leave this connection
persistent we need to execute
fsobj.RemoveNetworkDrive("X:", true);
EnumNetworkDrives()
This method takes no arguments and returns the collection containing the current network drive mapping information.
This collection is an array that associates pairs of items — network drive local names and their associated UNC names.
Even-numbered items in the collection represent local names of logical drives. Odd-numbered items represent the associated
UNC share names. The first item in the collection is at index zero (0).
We can treat this collection exactly the way we did before with collections of Files and Folders,
or we can use a shorter version by accessing the collection elements through method item() or
just using parenthesis with the element index inside after the collection name:
var nw = WScript.CreateObject("WScript.Network");
var nwDrives = nw.EnumNetworkDrives();
var msg = "Network drives:\n";
for(i=0;i<nwDrives.length;i+=2){
msg += nwDrives(i) + " = " + nwDrives.item(i+1) + "\n";
}
WScript.Echo(msg);
Network printers
The WshNetwork object has several methods to work with network printers. These methods are ...
EnumPrinterConnections()
This method takes no arguments and returns the current network printer mapping information. Like the
EnumNetworkDrives, this method returns a collection. This collection is an array that associates pairs of
items — network printer local names and their associated UNC names. Even-numbered items in the collection represent
printer ports. Odd-numbered items represent the networked printer UNC names. The first item in the collection is at index
zero (0). We can use this method exactly the same way we used method EnumNetworkDrives (see example above).
AddPrinterConnection
This methods adds a remote MS-DOS-based printer connection to your computer system. The general syntax is
AddPrinterConnection(LocalName, RemoteName [,UpdateProfile][,User][,Password])
where
LocalName
Sting value indicating the local name to assign to the connected printer.
RemoteName
Sting value indicating the name of the remote printer.
UpdateProfile
Optional. Boolean value indicating whether the printer mapping is stored in the current user's profile. If
UpdateProfile is supplied and is true, the mapping is stored in the user profile. The default value is
false.
User
Optional. String value indicating the user name. If you are mapping a remote printer using the profile of someone
other than current user, you can specify User and Password.
Password
Optional. String value indicating the user password. If you are mapping a remote printer using the profile of someone
other than current user, you can specify User and Password.
The AddPrinterConnection method adds a network printer to an MS-DOS printer port, such as LPT1. You cannot use
this method to add a remote Windows-based printer connection. To add a remote Windows-based printer connection, use the
AddWindowsPrinterConnection method.
To map a MS-DOS based printer to the local LPT1 port we need to use code like this
var nw = WScript.CreateObject("WScript.Network");
nw.AddPrinterConnection ("LPT1", "\\\\Server\\Print1");
AddWindowsPrinterConnection
Adds a Windows-based printer connection to your computer system. Using this method is similar to using the Printer option on
Control Panel to add a printer connection. Unlike the AddPrinterConnection method, this method allows you to
create a printer connection without directing it to a specific port, such as LPT1. If the connection fails, an error is
thrown. In Windows 9x/Me, the printer driver must already be installed on the machine for the
AddWindowsPrinterConnection method to work. If the driver is not installed, Windows returns an error message.
The general syntax of the method is
AddWindowsPrinterConnection(PrinterPath)
where PrinterPath is a String value indicating the path to the printer connection.
The following example shows how to connect a Windows printer \\printserv\DefaultPrinter
var nw = WScript.CreateObject("WScript.Network");
var PrinterPath = "\\\\printserv\\DefaultPrinter";
nw.AddWindowsPrinterConnection(PrinterPath);
RemovePrinterConnection
This method removes a shared network printer connection from your computer system. The RemovePrinterConnection
method removes both Windows and MS-DOS based printer connections. If the printer was connected using the method
AddPrinterConnection, you need to provide the printer's local name as an argument. If the printer was connected
using the AddWindowsPrinterConnection method or was added manually (using the Add Printer wizard), then
we need to pass the printer's UNC name. The general syntax of the method is
RemovePrinterConnection(Name, [Force], [UpdateProfile])
where
Name
String value indicating the name that identifies the printer. It can be a UNC name (in the form \\xxx\yyy) [for Windows
printers] or a local name (such as LPT1) [for MS-DOS printers].
Force
Optional. Boolean value indicating whether to force the removal of the mapped printer. If set to true (the
default is false), the printer connection is removed whether or not a user is connected.
UpdateProfile
Optional. Boolean value. If set to true (the default is false), the change is saved in the user's
profile.
This code disconnects the printers connected in the two previous examples
var nw = WScript.CreateObject("WScript.Network");
var PrinterPath = "\\\\PRN-CORP1\\B41-4523-A";
nw.RemovePrinterConnection(PrinterPath, true, true);
nw.RemovePrinterConnection("LPT1");
SetDefaultPrinter()
This method assigns a remote printer the role Default Printer.
SetDefaultPrinter(PrinterName)
where PrinterName is a string value indicating the remote printer's UNC name.
The SetDefaultPrinter method fails when using a DOS-based printer connection. We cannot use the
SetDefaultPrinter method to determine the name of the current default printer
The following example connects a Windows-based printer and makes it the Default Printer
var nw = WScript.CreateObject("WScript.Network");
var PrinterPath = "\\\\research\\library1";
nw.AddWindowsPrinterConnection(PrinterPath);
nw.SetDefaultPrinter(PrinterPath);
References: