Flylib.com

Books Software

 
 
 

Chapter 4: Networking Resources

Chapter 4: Networking Resources

Networked environments play an important role in today's corporate computer systems. WSH provides the ability to programmatically manipulate network resources. This functionality allows for operations to be performed that would be impossible using the old command-line programs, such as net use .

These operations are exposed through the WScript.Network object. This object provides methods that can perform network-related functions, such as network share and printer connection, and they can also query network- related information, such as user name and computer name .

4.1 Identifying a User

Problem

You want to find out who is currently logged on the local computer.

Solution

You can create an instance of the WScript.Network object and reference the UserName property:

'create an instance of the Wscript.Network object Set objNetwork = CreateObject("Wscript.Network") 'output user name Wscript.Echo "You are logged on as "& objNetwork.UserName

Discussion

The WScript.Network object exposes a number of properties that can be used to determine the computer, user, and domain information for the local computer. Table 4-1 lists Network object properties.

Table 4-1: Network Object Properties

PROPERTY

DESCRIPTION

ComputerName

Name of the local computer.

UserName

Name of the user logged into the local computer.

UserDomain

Name of the domain the user is currently logged into.

Returns the computer name if it is not a member of the domain.

On the Windows 9 x /ME platforms you may encounter problems referencing the UserName property in logon scripts. The problem is that the user ID doesn't get assigned immediately during the logon process.

To ensure the user name of the logged-on user is returned correctly in these instances, create a loop that continuously assigns the UserName property to a variable until it returns a valid user ID, as demonstrated in the following code snippet:

'create an instance of the Wscript.Network object Dim objNetwork, strName Set objNetwork = CreateObject("Wscript.Network") On Error Resume Next 'loop until a user ID is assigned While strName = "" strName = objNetwork.UserName Wscript.Sleep 100 WEnd

4.2 Finding an Available Network Drive

Problem

You want to find the next available network drive.

Solution

You can use the Scripting File System Object's DriveExists method to determine if a drive exists. The following function returns the next available drive:

'ReturnNextDrive.vbs Function ReturnNextDrive() Dim nF, objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") 'loop through

drives

starting from D: For nF = ASC("d") To ASC("z") 'if drive doesn't exist, it's available If Not objFSO.DriveExists(Chr(nF) & ":") Then ReturnNextDrive = Chr(nF) & ":" Exit Function End If Next End Function

Discussion

The ReturnNextDrive function listed in the Solution script returns the next available drive letter. If all drives are connected, it returns a Empty variant. It assumes the first potentially available drive starts at the D: drive.

The function uses the FSO library to determine if the drives exist. See Chapter 5 for more information on FSO- related operations.

The WScript.Network object's EnumNetworkDrives property returns a collection of connected network drives. The collection returned is a WSH collection, which operates in a slightly different manner than other object collections. The collection is represented as an array with items starting at 0 offset. For each connected drive, the array contains an element for the drive letter and an element for the share name . If you have two connected network drive shares (H: and P:), the collection array would contain four elements: the first element contains the H: drive, the second element contains the share H: is connected to in UNC format, the third element contains the P: drive, and the fourth element contains the share P: is connected to.

Use the EnumNetworkDrives property's Count property to determine the number of elements in the collection. The number represents double the number of connected shares.

The following code snippet demonstrates how to list details on the first share as well as listing details on all connected shares:

'listshares.vbs 'lists connected network shares Dim objNetwork, objShares, nf Set objNetwork = CreateObject("Wscript.Network") Set objShares = objNetwork.EnumNetworkDrives 'output the first connected share and associated drive letter Wscript.Echo "Drive " & objShares(0) & " is connected to " & objShares(1) 'loop through all connected shares and output details For nf = 0 To objShares.Count - 1 Step 2 Wscript.Echo objShares(nf) & " is connected to " & objShares(nf + 1) Next