Section 9.12. Network Object

9.12. Network Object

The Wscript Network object provides access to network resources and information. The ID for the object is Wscript.Network .

To create an instance of the Wscript Network object, pass the argument Wscript.Network to the Wscript.CreateObject method:

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

Table 9-15 lists the Network object properties

Table 9-15. Network object properties

Property

Description

ComputerName

Name of computer

UserName

Name of user logged into the machine

UserDomain

Name of domain user is currently logged into


The following example displays the name of the user logged into the machine:

 Set objNetwork = CreateObject("Wscript.Network") Wscript.Echo "You are logged on as " & objNetwork.UserName 

9.12.1. EnumNetworkDrives

EnumNetworkDrives returns a collection of the currently connected network drives . The collection that is returned is a special WSH collection; it doesn't operate the same way as other object collections.

For each connected drive, the collection contains an item for the drive letter and another item for the connected share name. If you have three connected network drives ( K: , S: , and Y: ), the collection contains six elements, the first element being the K: drive, the second being the share to which K: is connected, and so on.

 Set objNetwork = Wscript.CreateObject("Wscript.Network")  Set objShares = objNetwork.EnumNetworkDrives( ) Wscript.Echo "Drive " & objShares(0) & " is connected to " & objShares(1) 

The following example returns the next available network drive. If all drives are connected, it returns a blank string. It assumes your network drives start at F :.

 Function ReturnNextDrive( ) Dim nF, objNetwork, objShares, intNextDrive Set objNetwork = Wscript.CreateObject("Wscript.Network")  Set objShares = objNetwork.EnumNetworkDrives( ) intNextDrive = 0 For nF = 0 To objShares.Count - 1 Step 2    If intNextDrive <> Asc(objShares(nF)) - 70 Then       ReturnNextDrive = Chr(intNextDrive + 70) & ":"       Exit Function    End If    intNextDrive = intNextDrive + 1 Next ReturnNextDrive = "" End Function 

9.12.2. MapNetworkDrive

The MapNetworkDrive method connects a drive to a network share:

 objNetwork.MapNetworkDrive   strDrive   ,   strRemoteShare   , _      [   bUpdateProfile   ], [   strUser   ], [   strPassword   ] 

Table 9-16 lists the parameters for the MapNetworkDrive method.

Table 9-16. MapNetworkDrive parameters

Parameter

Description

strDrive

The local drive letter to connect the network share to.

strRemoteShare

Name of remote share using UNC format; e.g., \\server\sharename .

bUpdateProfile

Optional Boolean parameter that indicates if drive connection is remembered for next session.

strUser

Optional username to use when connecting to remote share.

StrPassword

Optional password to use when connecting to remote share. This is used with the strUser parameter.


If you attempt to connect to a drive that is already connected to a share, an error will occur. Therefore, the On Error Resume Next statement is required beforehand to ensure the script is completed.

Usually when users log on, they are connected to their home share. This is usually identified by their network user ID. The following example connects the H: drive to the user's home share:

 Set objNetwork = Wscript.CreateObject("Wscript.Network") objWshNetwork.MapNetworkDrive "H:", _          "\THOR\" & objWSHNetwork.UserName & "$" , True 

9.12.3. RemoveNetworkDrive

The RemoveNetworkDrive method disconnects a specified network share:

 objNetwork.RemoveNetworkDrive   strName   , [   bForceDisconnect   ],           [   bUpdateProfile   ] 

The RemoveNetworkDrive method has two optional parameters, the first being a Boolean ForceDisconnect flag. If set to True, it will forcefully disconnect the drive, even if it is currently in use. The second parameter specifies whether the user's profile is to be updated.

If there is a possibility of a drive being mapped to another network connection and you wish to connect it to a different network share, first delete the existing connection using the RemoveNetworkDrive method.

The following (forcefully) removes the T : drive connection:

 objWshNetwork.RemoveNetworkDrive "T:", True, True objWshNetwork.MapNetworkDrive "P:", _         "\THOR\PublicArea", True 



Windows XP in a Nutshell
Windows XP in a Nutshell, Second Edition
ISBN: 0596009003
EAN: 2147483647
Year: 2003
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net