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
These operations are exposed through the
WScript.Network
object. This object provides
You want to find out who is currently logged on the local computer.
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
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.
|
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
'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
You want to find the
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
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
The function uses the FSO library to determine if the drives exist. See Chapter 5 for more information on FSO-
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
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
'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