Export (0) Print
Expand All

WshNetwork Object

Microsoft® Windows® 2000 Scripting Guide

The WshNetwork object provides your scripts with the ability to work with network drives and printers. It also provides your scripts with access to the name of the computer they are currently running on, as well as the domain and user names of the account under which they are running. This makes WshNetwork extremely useful in scripts that take actions based on such information as the name of the user logging on or the domain in which the user account resides.

The WshNetwork object can be used to accomplish some of the same tasks as the net.exe command-line tool. For example, the net name command returns the name of the user and the computer; the net use command is used to map and unmap network drives. These similarities make WshNetwork useful for converting legacy batch files and logon scripts that use net.exe to a WSH-based solution.

Table 3.21 lists the capabilities provided by the WshNetwork object, along with the methods and properties that your scripts can use to access this functionality.

Table 3.21 Capabilities Provided by the WshNetwork Object

Category

Method or Property

Working with network drives

MapNetworkDrive

EnumNetworkDrives

RemoveNetworkDrive

Working with network printers

AddPrinterConnection

AddWindowsPrinterConnection

EnumPrinterConnections

SetDefaultPrinter

RemovePrinterConnection

Obtaining information about the currently logged-on user

ComputerName

UserDomain

UserName

WshNetwork is part of the Windows Script Host Object Model, wshom.ocx. The object model for the WshNetwork object is shown in Figure 3.15. In addition to the methods and properties provided by the WshNetwork object, WshNetwork also exposes a WshCollection object. The WshCollection object is not created using CreateObject but is automatically created and returned by the WshNetwork objects EnumNetworkDrives and EnumPrinterConnections methods.

Figure 3.15 shows the properties and methods of the WshNetwork object.

Figure 3.15 WshNetwork Object Model

sas_wsh_140c

Accessing the WshNetwork Object

The WshNetwork object is a COM object, and an instance of the object can be created by the following code statement:



Set objNetwork = WScript.CreateObject("WScript.Network")

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft