| |
WSH Shell Info
The WSHShellInfo component is a Windows
Script Component that exposes some functionality of the WshNetwork,
WshEnvironment object to any environment that supports COM, such as Microsoft
Visual Basic. |
Methods
 |
EnvVar( strWhere, strWhat ) |
|
Returns the environment variable
specified by strWhere and strWhat |
 |
strWhere: Where the environment resides. |
 |
strWhat: The name of the environment variable to retrieve. |
|
 |
ExpandEnv( strEnv ) |
|
Expands the environment variable specified by
strEnv |
 |
strEnv: Environment variable to expand. |
|
 |
GetSpecialFolder( strFolder ) |
|
|
 |
ComputerName() |
|
WshNetwork.ComputerName property |
|
 |
UserName() |
|
WshNetwork.UserName property. |
|
 |
UserDomain() |
|
WshNetwork.UserDomain property |
|
 |
GetNetworkDrive( intDriveNum ) |
|
Returns the drive letter for the drive
specified by intDriveNum |
 |
intDriveNum: Integer ranging from 1 to NetworkDriveCount |
|
 |
GetNetworkMapping( intDriveNum ) |
|
Returns the mapping for the drive specified
by intDriveNum |
 |
intDriveNum: Integer ranging from 1 to
NetworkDriveCount |
|
 |
GetNetworkDrives() |
|
Returns an array containing all of the network drive
letters |
|
 |
GetNetworkMappings() |
|
Returns an array of all the network drive mapping info. |
|
 |
MapNetworkDrive( strLocalResource, strRemoteShare,
blnUpdateProfile, strUserName, strPassword ) |
|
Maps strRemoteShare to
strLocalResource. Returns an error string if it fails. Otherwise an empty string. |
 |
strLocalResource:Name of local resource |
 |
strRemoteShare: Name of the remote resource to map. |
 |
blnUpdateProfile:Set to true to store the mapping in the
user profile. |
 |
strUserName: Use to specify a name other than the current
user. |
 |
strPassword: The password for the user specified by
strUserName |
|
 |
RemoveNetworkDrive( strName, blnForce, blnUpdateProfile ) |
|
Rremoves the resource connection
specified by strName. Returns an error string if it fails otherwise an empty string |
 |
strName:Local or remote name (depending on how it is
mapped). |
 |
blnForce: If true it removes the connection whether or not
it is used. |
 |
blnUpdateProfile: If true it removes the mapping from the
user profile. |
|
 |
GetPrinterPort( intPrinterNum ) |
|
Returns a string containing the port
of the specified printer. An error string is returned if it fails. |
 |
intPrinterNum: An integer from 1 to ConnectedPrinterCount |
|
 |
GetPrinterMapping( intPrinterNum ) |
|
Returns a string containing the
network mapping for the specified printer. |
 |
intPrinterNum: An integer ranging from 1 to
ConnectedPrinterCount |
|
 |
GetPrinterPorts() |
|
Returns an array containing the ports of all connected
printers. |
|
 |
GetPrinterMappings() |
|
Returns an array containing the mapping info for all
connected printers. |
|
 |
AddPrinterConnection( strLocalResource, strRemoteName,
blnUpdateProfile, strUserName, strPassword ) |
|
Maps the remote printer specified by strRemoteName
to the local resource name strLocalResource |
 |
strRemoteName: Local printer resource. |
 |
blnUpdateProfile: If true, the mapping is stored in
the user profile. |
 |
strUser:The user name specified if using the
credentials of someone other than the current user. |
 |
strPassword: The password specified if using the
credentials of someone other than the current user. |
|
 |
AddWindowsPrinterConnectionNT( strPrinterPath ) |
|
Adds a printer connection to Windows. |
 |
strPrinterPath: Path to the printer. |
|
 |
AddWindowsPrinterConnection9x( strPrinterPath,
strDriverName, strPort ) |
|
Adds a printer connection to Windows. |
 |
strPrinterPath: Path to the printer. |
 |
strDriverName: Name of the driver to use. |
 |
strPort: Specified the port to attach the printer to.
(LPT1 default) |
|
 |
RemovePrinterConnection( strPrinterName, blnForce,
blnUpdateProfile ) |
|
Removes the connection specified byu
strPrinterName |
 |
strPrinterName: Either the local name or the remote name,
depending on mapping. |
 |
blnForce: Forces the removal whether the resource is used
or not. |
 |
blnUpdateProfile: If true, the user's profile is updated. |
|
Properties
 |
NetworkDriveCount |
|
(Read only) The number of accessable network drives |
|
 |
NetworkPrinterCount |
|
(Read only) The number of connected printers. |
Source
<?XML version="1.0" standalone="yes" ?>
<?component debug="true" error="true" ?>
<component id="WSHShellInfo">
<registration
description="WSH Shell Object Info"
progid="WSHShellInfo.Info"
version="1"
classid="{CD4BF880-D44F-11d4-BB17-00C0F033CD85}"
/>
<public>
<method name="EnvVar">
<parameter name="strWhere" />
<parameter name="strWhat" />
</method>
<method name="ExpandEnv">
<parameter name="strEnv" />
</method>
<method name="GetSpecialFolder">
<parameter name="strFolder" />
</method>
<method name="ComputerName" />
<method name="UserName" />
<method name="UserDomain" />
<method name="GetNetworkDrive">
<parameter name="intDriveNum" />
</method>
<method name="GetNetworkMapping">
<parameter name="intDriveNum" />
</method>
<method name="GetNetworkDrives"/>
<method name="GetNetworkMappings"/>
<method name="MapNetworkDrive">
<parameter name="strLocalResource" />
<parameter name="strRemoteShare" />
<parameter name="blnUpdateProfile" />
<parameter name="strUserName" />
<parameter name="strPassword" />
</method>
<method name="RemoveNetworkDrive">
<parameter name="strName" />
<parameter name="blnForce" />
<parameter name="blnUpdateProfile" />
</method>
<comment>
/*
* Printer Methods
*/
</comment>
<method name="GetPrinterPort">
<parameter name="intPrinterNum" />
</method>
<method name="GetPrinterMapping">
<parameter name="intPrinterNum" />
</method>
<method name="GetPrinterPorts" />
<method name="GetPrinterMappings" />
<method name="AddPrinterConnection">
<parameter name="strLocalResource" />
<parameter name="strRemoteName" />
<parameter name="blnUpdateProfile" />
<parameter name="strUserName" />
<parameter name="strPassword" />
</method>
<method name="AddWindowsPrinterConnectionNT">
<parameter name="strPrinterPath" />
</method>
<method name="AddWindowsPrinterConnection9x">
<parameter name="strPrinterPath" />
<parameter name="strDriverName" />
<parameter name="strPort"/>
</method>
<method name="RemovePrinterConnection">
<parameter name="strName" />
<parameter name="blnForce" />
<parameter name="blnUpdateProfil" />
</method>
<method name="SetDefaultPrinter">
<parameter name="strName" />
</method>
<comment>
/*
* Properties
*/
</comment>
<property name="NetworkDriveCount" get />
<property name="ConnectedPrinterCount" get />
<property name="LOG_SUCCESS" get />
<property name="LOG_ERROR" get />
<property name="LOG_WARNING" get />
<property name="LOG_INFORMATION" get />
<property name="LOG_AUDIT_SUCCESS" get />
<property name="LOG_AUDIT_FAILURE" get />
<public>
<script language="VBScript">
<![CDATA[
Option Explicit
Const get_LOG_SUCCESS = 0
Const get_LOG_ERROR = 1
Const get_LOG_WARNING = 2
Const get_LOG_INFORMATION = 4
Const get_LOG_AUDIT_SUCCESS = 8
Const get_LOG_AUDIT_FAILURE = 16
Function EnvVar(strWhere, strWhat)
Dim wsh : Set wsh = CreateObject("WScript.Shell")
Dim env : Set env = wsh.Environment(strWhere)
EnvVar = env(strWhat)
Set env = Nothing
Set wsh = Nothing
End Function
Function ExpandEnv(strEnv)
Dim wsh : Set wsh = CreateObject("WScript.Shell")
ExpandEnv = wsh.ExpandEnvironmentStrings(strEnv)
Set wsh = Nothing
End Function
Function GetSpecialFolder(strFolder)
Dim wsh : Set wsh = CreateObject("WScript.Shell")
Dim Str
str = wsh.SpecialFolders(strFolder)
If Not (str = "") Then
GetSpecialFolder = Str
Else
GetSpecialFolder = "Not available"
End If
Set wsh = Nothing
End Function
Function ComputerName()
Dim net : Set net = CreateObject("WScript.Network")
ComputerName = net.ComputerName
Set net = Nothing
End Function
Function UserName()
Dim net : Set net = CreateObject("WScript.Network")
UserName = net.UserName
Set net = Nothing
End Function
Function UserDomain()
Dim net : Set net = CreateObject("WScript.Network")
UserDomain = net.UserDomain
Set net = Nothing
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Network Drive Functions
'
Function GetNetworkDrive( intDriveNum )
If Not IsNumeric(intDriveNum) Then
GetNetworkDrive = "Invalid Drive Number"
Exit Function
ElseIf intDriveNum <= 0 Then
GetNetworkDrive = "Invalid Drive Number"
Exit Function
End If
Dim net : Set net = CreateObject("WScript.Network")
Dim d : Set d = net.EnumNetworkDrives()
Dim n : n = d.Count
If intDriveNum > (n / 2) Then
GetNetworkDrive = "Drive ID Out Of Range"
Else
GetNetworkDrive = d.Item( (intDriveNum - 1) * 2 )
End If
Set d = Nothing
Set net = Nothing
End Function
Function GetNetworkMapping( intDriveMum )
If Not IsNumeric(intDriveNum) Then
GetNetworkMapping = "Invalid Drive ID"
Exit Function
ElseIf intDriveNum <= 0 Then
GetNetworkMapping = "Invalid Drive ID"
Exit Function
End If
Dim net : Set net = CreateObject("WScript.Network")
Dim d : Set d = net.EnumNetworkDrives()
Dim n : n = d.Count
If intDriveNum > (n / 2) Then
GetNetworkMapping = "Drive ID Out Of Range"
Else
GetNetworkMapping = d.Item( (intDriveNum + (intDriveNum - 1)) )
End If
Set d = Nothing
Set net = Nothing
End Function
Function GetNetworkDrives()
Dim sDrives()
Dim i, j : j = 0
Dim net : Set net = CreateObject("WScript.Network")
Dim d : Set d = net.EnumNetworkDrives()
Dim n : n = d.Count
ReDim sDrives( (n / 2) )
For i = 0 To (n - 2) Step 2
sDrives(j) = d.Item(i)
j = j + 1
Next
Set d = Nothing
Set net = Nothing
GetNetworkDrives = sDrives
End Function
Function GetNetworkMappings()
Dim sMaps()
Dim i, j : j = 0
Dim net : Set net = CreateObject("WScript.Network")
Dim d : Set d = net.EnumNetworkDrives()
Dim n : n = d.Count
ReDim sMaps( (n / 2) )
For i = 0 To (n - 1) Step 2
sMaps(j) = d.Item(i)
j = j + 1
Next
Set d = Nothing
Set net = Nothing
GetNetworkMappings = sMaps
End Function
Function MapDriveToShare( strLocalResource, strRemoteShare, blnUpdateProfile, strUserName, strPassword )
If CheckString(strLocalResource) = "" Or CheckString(strRemoteShare) = "" Then
MapDriveToShare = "Invalid Resource Or Share"
Exit Function
End If
blnUpdateProfile = CheckBool(blnUpdateProfile)
strUserName = CheckString(strUserName)
strPassword = CheckString(strPassword)
Dim net : Set net = CreateObject("WScript.Network")
Call net.MapNetworkDrive(strLocalResource, strRemoteShare, blnUpdateProfile, strUserName, strPassword)
Set net = Nothing
If Err.Number <> 0 Then
MapDriveToShare = Err.Description
Else
MapDriveToShare = ""
End If
End Function
Function RemoveNetworkDrive( strName, bForce, blnUpdateProfile )
If CheckString(strName) = "" Then
RemoveNetworkDrive = "Invalid Resource Name"
Exit Function
End If
bForce = CheckBool(bForce)
blnUpdateProfile = CheckBool(blnUpdateProfile)
Dim net : Set net = CreateObject("WScript.Network")
Call Err.Clear()
Call net.RemoveNetworkDrive(strName, bForce, blnUpdateProfile)
Set net = Nothing
If Err.Number <> 0 Then
RemoveNetworkDrive = Err.Description
Else
RemoveNetworkDrive = ""
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Printer methods
'
Function GetPrinterPort( intPrinterNum )
If Not IsNumeric(intPrinterNum) Then
GetPrinterPort = "Invalid Printer ID"
Exit Function
End If
Dim net : Set net = CreateObject("WScript.Network")
Dim p : Set p = net.EnumPrinterConnections()
Dim n : n = p.Count
If n = 0 Then
GetPrinterPort = "No Printers"
Exit Function
ElseIf intPrinterNum > (n / 2) Then
' This is a problem because of the way that
' the Collection returned by EnumPrinterConnections
' is made up of alternating ports Then mapping info.
' So there are Collection.Count / 2 ports.
GetPrinterPort = "Printer ID Out Of Range"
ElseIf intPrinterNum <= 0 Then
GetPrinterPort = "Invalid Printer ID"
Else
GetPrinterPort = p.Item( (intPrinterNum - 1) * 2 )
End If
Set p = Nothing
Set net = Nothing
End Function
Function GetPrinterMapping( intPrinterNum )
If Not IsNumeric(intPrinterNum) Then
GetPrinterMapping = "Invalid Printer ID"
Exit Function
ElseIf intPrinterNum <= 0 Then
GetPrinterMapping = "Invalid Printer ID"
Exit Function
End If
Dim net : Set net = CreateObject("Wscript.Network")
Dim p : Set p = net.EnumPrinterConnections()
Dim n : n = p.Count
If n = 0 Then
GetPrinterMapping = "No Printers"
ElseIf intPrinterNum > (n / 2) Then
GetPrinterMapping = "Printer ID Out Of Range"
Else
GetPrinterMapping = p.Item( (intPrinterNum + (intPrinterNum - 1)) )
End If
Set p = Nothing
Set net = Nothing
End Function
'''''''''''''''''''''''''''''''
' GetPrinterPorts() And GetPrinterMappings() Each return
' a Variant containing an Array.
'
Function GetPrinterPorts()
Dim sPorts()
Dim i, j : j = 0
Dim net : Set net = CreateObject("WScript.Network")
Dim p : Set p = net.EnumPrinterConnections()
Dim n : n = p.Count
ReDim sPorts( (n / 2) )
For i = 0 To (n - 2) Step 2
sPorts(j) = p.Item(i)
j = j + 1
Next
Set p = Nothing
Set net = Nothing
GetPrinterPorts = sPorts
End Function
Function GetPrinterMappings()
Dim sMaps()
Dim i, j : j = 0
Dim net : Set net = CreateObject("WScript.Network")
Dim p : Set p = net.EnumPrinterConnections()
Dim n : n = p.Count
ReDim sMaps( (n / 2) )
For i = 1 To (n - 1) Step 2
sMaps(j) = p.Item(i)
j = j + 1
Next
Set p = Nothing
Set net = Nothing
GetPrinterMappings = sMaps
End Function
Function AddPrinterConnection( strLocalResource, strRemoteName, blnUpdateProfile, strUserName, strPassword)
If CheckString(strLocalResource) = "" Or CheckString(strRemoteName) = "" Then
AddPrinterConnection = "Invalid Resource Or Remote Name"
Exit Function
End If
blnUpdateProfile = CheckBoolean(blnUpdateProfile)
strUserName = CheckString(strUserName)
strPassword = CheckString(strPassword)
Dim net : Set net = CreateObject("WScript.Network")
Call Err.Clear()
Call net.AddPrinterConnection(strLocalResource, strRemoteName, blnUpdateProfile, strUserName, strPassword)
Set net = Nothing
If Err.Number <> 0 Then
AddPrinterConnection = Err.Description
Else
AddPrinterConnection = ""
End If
End Function
Function AddWindowsPrinterConnectionNT( strPrinterPath )
If CheckString( strPrinterPath ) = "" Then
AddWindowsPrinterConnectionNT = "Invalid Printer Path"
Exit Function
End If
Dim net : Set net = CreateObject("WScript.Network")
Call Err.Clear()
Call net.AddWindowsPrinterConnection( strPrinterPath )
Set net = Nothing
If Err.Number <> 0 Then
AddWindowsPrinterConnectionNT = Err.Description
Else
AddWindowsPrinterConnectionNT = ""
End If
End Function
Function AddWindowsPrinterConnection9x( strPrinterPath, strDriverName, strPort )
If CheckString(strPrinterPath) = "" Or CheckString(strDriverName) = "" Then
AddWindowsPrinterConnection9x = "Invalid Printer Path Or Driver Name"
Exit Function
End If
Dim net : Set net = CreateObject("WScript.Network")
Call Err.Clear()
Call net.AddWindowsPrinterConnection( strPrinterPath, strDriverName, strPort )
Set net = Nothing
If Err.Number <> 0 Then
AddWindowsPrinterConnection9x = Err.Description
Else
AddWindowsPrinterConnection9x = ""
End If
End Function
Function RemovePrinterConnection( strName, blnForce, blnUpdateProfile )
If CheckString(strName) = "" Then
RemovePrinterConnection = "Invalid Printer Name"
Exit Function
End If
blnForce = CheckBoolean(blnForce)
blnUpdateProfile = CheckBoolean(blnUpdateProfile)
Dim net : Set net = CreateObject("WScript.Network")
Call Err.Clear()
Call net.RemovePrinterConnection(strName, blnForce, blnUpdateProfile)
Set net = Nothing
If Err.Number <> 0 Then
RemovePrinterConnection = Err.Description
Else
RemovePrinterConnection = ""
End If
End Function
Function SetDefaultPrinter( strName )
If CheckString(strName) = "" Then
SetDefaultPrinter = "Invalid Printer Name"
Exit Function
End If
Dim net : Set net = CreateObject("WScript.Network")
Call Err.Clear()
Call net.SetDefaultPrinter(strName)
Set net = Nothing
If Err.Number <> 0 Then
SetDefaultPrinter = Err.Description
Else
SetDefaultPrinter = ""
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Property Procedures
'
Function Get_NetworkDriveCount()
Dim net : Set net = CreateObject("WScript.Network")
Dim netDrives : Set netDrives = net.EnumNetworkDrives
Get_NetworkDriveCount = (netDrives.Count / 2)
Set netDrives = Nothing
Set net = Nothing
End Function
Function Get_ConnectedPrinterCount()
Dim net : Set net = CreateObject("WScript.Network")
Dim prnt : Set prnt = net.EnumPrinterConnections()
Get_ConnectedPrinterCount = (prnt.Count / 2)
Set prnt = Nothing
Set net = Nothing
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Data validation functions used to check the validity
' of passed in Parameters.
'
Function CheckString( s )
If VarType(s) <> vbString Then
CheckString = ""
ElseIf Trim(s) = "" Then
CheckString = ""
Else
CheckString = s
End If
End Function
Function CheckBool( b )
If VarType(b) <> vbBoolean Then
If VarType(b) = vbString Then
If b = "True" Or b = "true" Then
CheckBool = True
Else
CheckBool = False
End If
ElseIf IsNumeric(b) Then
If b <> 0 Then
CheckBool = True
Else
CheckBool = False
End If
Else
CheckBool = False
End If
Else
CheckBool = b
End If
End Function
]]>
</script>
</component>
|