How to Work With Network with VBScript using WSH?

0
130
Spread the love
  • 28
  • 34
  • 56
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    165
    Shares

WSHNetwork
WSHNetwork

How to Work With Network with VBScript using WSH?

The WSHNetwork properties are read-only so we can not alter or modify them. We can access them by creating an object of WSHNetwork.

  • The UserName property fetches the currently logged in username as String.
  • The UserDomain property returns a user’s domain name. note that if the USERDOMAIN environment variable is not set, like the default operating system -windows 98 or ME, the userdomain will not work properly.
  • The computerName property returns the name of the computer system.
Option Explicit
Dim MyWSHNetwork
Set MyWSHNetwork= WScript.CreateObject("WScript.Network")
msgbox "UserName" & MyWSHNetwork.UserName
msgbox "UserDomain" & MyWSHNetwork.UserDomain
msgbox "ComputerName" & MyWSHNetwork.ComputerName

EnumNetworkDrives()

We can use the WSHNEtwork object’s EnumNetworkDrives() method to retrieve information about the connections on the mapped network drive connections.

Option Explicit
Dim MyWSHNetwork
Set MyWSHNetwork= WScript.CreateObject("WScript.Network")
MyObjDriveList=MyWSHNetwork.EnumNetworkDrives
For i=0 to MyObjDriveList-1
msgbox MyObjDriveList.Item(i)
Next

EnumNetworkDrives() return a collection. Hence we need to iterate over it to get individual items. The first item in the collection is always in the index-0. The items follow the UNC model. UNC stands for Universal Naming Convention

How to map connection to network drives?

Syntax

WSHNetwork.MapNetworkDrive(LocalName,NetworkName,[changeProfile],[userName],[password])

Where-

  • LocalName is the drive letter to be assigned during the connection. It is a string value.
  • NetworkName is the network name in UNC format(\\xxx\yyy)
  • ChangeProfile is the optional Boolean parameter. If set true, the mapped drive connection is stored in the user’s profile. The default setting is false.
  • UserName is also an optional parameter. It just shows on which user account the drive will be mapped. It is also a String value.
  • Password is also an optional parameter as String.

Example

Dim WSHNet
Set WSHNet= WScript.CreateObject("WScript.Network")
WSHNet.MepNetworkDrive "L","MyFileServer\D"

Here D drive is shared over the network and mapped.

Note- An attempt to map a nonshared network drive will generate an error.

Disconnecting Network Drive

RemoveNetworkDrive() method removes a shared network drive from the computer system.

Syntax:

WSHNetwork.RemoveNetworkDrive(Name,[force],[ChangeProfile])

Where

  • The name is the drive letter that needs to be disconnected.
  • force is an optional boolean parameter to determine if the disconnection happens forcefully. If set to yes, the method will terminate irrespective of the resource in use or not.
  • ChangeProfile is the optional Boolean parameter to determine if the deleted drive (mapped drive) will also be deleted from the user’s profile. The default value is false.
Dim WSHNet 
Set WSHNet= WScript.CreateObject("WScript.Network") 
WSHNet.RemoveNetworkDrive "L"

Network Printer

Enumerating Network Printer

The WSHNetwork object provides EnumPrinterConnection() method to get information about all current printers connected over a particular network.

Syntax of EnumPrinterConnection() method
objListofPrinters=WSHNetwork.EnumPrinterConnection

EnumPrinterConnection() method returns a collection of printer objects. This collection is an indexed array(index starting from zero). Elements get added to the array in pair as printer and port.

Example

Dim WSHNet 
Set WSHNet= WScript.CreateObject("WScript.Network") 
Set collectionofPrinter=WSHNet.EnumPrinterCollections
for i=0 to collectionofPrinter.count-1
 msgbox collectionofPrinter.Item(i)
Next

AddPrinterConnection() Method

AddPrinterConnection() method is used to setup printer connection to remote non windows printers. This method provides ability to establish connection to MS-DOS printer connection.

Syntax of AddPrinterConnection() method
WSHNet.AddPrinterConnection(LocalName or remoteMachineName,NetworkName,[changeProfile],[userName],[password]))

Where

  • LocalName or remoteMachineName is the name represented in String that will be assigned to the printer connection.
  • NetworkName is the printer’s network name represented in String.
  • changeProfile is an optional boolean parameter. If set to True, it represents the connection that will be stored in the profile. False represents that the connection will not be stored in the profile.
  • userName is also an optional parameter. It represents that on which username the printer connection will be made.
  • password is the password associated with the userName. It is also an optional parameter.

Example

Dim WSHNet 
Set WSHNet= WScript.CreateObject("WScript.Network") 
WSHNet.AddPrinterConnection "MyPrinter","\\MyServer\Printer"

AddWindowsPrinterConnection() method

AddWindowsPrinterConnection() method enables us to connect a windows printer. This method does not need port information. It can have two different forms.

  1. Older versions of windows
  2. Newer versions of windows

For older versions of windows, the syntax is as follows

WSHNetwork.AddWindowsPrinterConnection(Printerpath,DriverName,[port])

For newer versions of windows, the syntax is as follows

WSHNetwork.AddWindowsPrinterConnection(Printerpath)

Where

  • Printerpath is the path of the printer represented in String.
  • DriverName is specific to the printer’s software driver represented in String.
  • Port is an optional parameter, specifies the port needs to be used during a connection. It is also represented in String.

If the connection fails an error code is generated.

Dim WSHNet 
Set WSHNet= WScript.CreateObject("WScript.Network") 
WSHNet.AddWindowsPrinterConnection "\\MyPrinterserver\MyPrinter","XYZ Driver",7202
or
WSHNet.AddWindowsPrinterConnection "\\MyPrinterserver\MyPrinter"

How to remove a Printer Connection in VBScript or UFT?

WSHNetwork provides the RemovePrinterConnection() method to remove or disconnect an existing network printer connection. It removes the window-based and MS-DOS-based printer connections.

If the printer was connected using the method AddPrinterConnection() then the name of the printer should be the local name.

If the printer was connected using the method AddWindowsPrinterConnection() or added manually then the name of the printer should be as per UNC naming standard. like \\xyz\yyy

Syntax of RemovePrinterConnection()

WSHNetwork.RemovePrinterConnection(PrinterName,[force],[changeProfile])

Where

  • PrinterName is the UNC string representation of the name of the printer.
  • force is an optional parameter specifies if we need to apply force to remove it. In the case of force is true, the method will remove a connection even if the connection is being actively used. The default is false.
  • changeProfile is also an optional boolean parameter specifies if the change will be made to the user’s profile. The default is false.

Example

Dim WSHNet 
Set WSHNet= WScript.CreateObject("WScript.Network") 
WSHNet.RemovePrinterConnection "\\MyPrinterserver\MyPrinter"

How to set a default Printer in VBScript or UFT?

WSHNetwork provides SetDefaultPrinter() method to set a default printer. SetDefaultPrinter() method will fail if used with DOS-based printer connection.

Syntax of SetDefaultPrinter
WSHNet.SetDefaultPrinter(PrinterName)

Where

  • PrinterName is the String representation of the name of the printer that will be set as a default printer.
Dim WSHNet 
Set WSHNet= WScript.CreateObject("WScript.Network") 
WSHNet.AddWindowsPrinterConnection "\\MyPrinterserver\MyPrinter"
WSHNet.SetDefaultPrinter "\\MyPrinterserver\MyPrinter"
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 28
  • 34
  • 56
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    165
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here