Working with Networks and Network Resources

[ LiB ]

Working with Networks and Network Resources

If your computer is connected to a home or corporate network, you can also create JScripts that interact with and control network resources ( assuming that you have security privileges over those resources). To make this happen, you need to work with the WshNetwork object. This object provides access to several properties that provide information about the network to which a computer is attached. These properties include:

  • UserName . Returns the account name of the user currently logged on.

  • ComputerName . Returns the name of the computer as it will appear to other network users.

  • UserDomain . Returns the name of the domain to which the computer is currently logged in.

In addition to these properties, the WshNetwork object also provides access to a number of methods. Using these methods , your JScripts can connect to and access network drives and printers. These methods include:

  • EnumNetworkDrives . Returns information about current network drive connections.

  • MapNetworkDrive . Establishes a connection to a network drive or folder.

  • RemoveNetworkDrive . Deletes a connection to a network drive or folder.

  • EnumPrinterConnection . Returns information about current network printer connections.

  • AddPrinterConnection . Establishes a connection to a network printer.

  • RemovePrinterConnection . Deletes a connection to a network printer.

  • SetDefaultPrinter . Establishes a printer as the printer to which all print jobs are automatically submitted.


A network drive is a disk drive that network users can access in order to store or retrieve files. A network folder is a single folder whose contents have been shared over a network. A network printer is a printer device that accepts and prints print jobs from users connected to a network.By establishing connections to network drives,folders,and printers, you make them look and act as if they are connected to your computer locally.

The following JScript statements demonstrate how to set up a connection to a network drive or folder:

 var WshNetwork = WScript.CreateObject("WScript.Network"); WshNetwork.MapNetworkDrive("x:", "\\FileSvr\d"); 

In this example, a network connection is established to the D drive on a computer named FileSvr . The connection is represented by the drive letter assignment of x: , which will be displayed in the My Computer dialog along with all locally connected network drives, as demonstrated in Figure 7.8.

Figure 7.8. Connections to network drives and folders are identified by the depiction of a network cable connection beneath the icon.


Remember that to connect to a network resource you must use the Universal Naming Convention (UNC). Also remember that the \ character represents a special character in JScript and that you must escape it by preceding it with another \ when you want to use it. Therefore, in order to specify a UNC address of \\FileSvr\d in a JScript, you must write it out as \\\\FileSvr\\d .

If you no longer need access to a network drive, you may want to delete its connection. You can do this using the WshNetwork object's RemoveNetworkDrive() method, as demonstrated here:

 var WshNetwork = WScript.CreateObject("WScript.Network"); WshNetwork.RemoveNetworkDrive("x:"); 

In this example, the WshNetwork object is instantiated , and then the previously established network drive connection is deleted.

[ LiB ]

Learn JavaScript In a Weekend
Learn JavaScript In a Weekend, Second Edition
ISBN: 159200086X
EAN: 2147483647
Year: 2003
Pages: 84 © 2008-2017.
If you may any questions please contact us: