Working with network resources

The following code fragments in VBScript, you must copy the text box of  "Program" step.

Consider few methods of automation of network resources by using VBScript.

To work with local network resources object WshNetwork is used, with this object, you can connect and disconnect network drives and printers.

Displaying information about your computer:

Dim objNet


'Create object WshNetwork

Set objNet = WScript.CreateObject("WScript.Network")

MsgBox "Computer:" & objNet.ComputerName

MsgBox "Domain:" & objNet.UserDomain

MsgBox "User:" & objNet.UserName


How to work with network printers:

Dim objNet


'Create object WshNetwork

Set objNet = WScript.CreateObject("WScript.Network")

'Adding network printer to local LPT1 port \\Server1\Epson

'objNet.AddPrinterConnection "LPT1","\\Server1\Epson"


Dim strPath

strPath = "\\Server2\DefaultPrinter"

'Adding printer connection

'objNet.AddWindowsPrinterConnection strPath


'Setting printer by default

objNet.SetDefaultPrinter "\\Server1\Epson"


strPath = "\\Server3\DefaultPrinter"

'Delete printer from the strPath

objNet.RemovePrinterConnection strPath, true, true


'Create collection with connected printers information

Set objPrinters = objNet.EnumPrinterConnections

For lngI=0 To objPrinters.Count-1

       MsgBox objPrinters.Item(lngI) 'output lngI element of collection



How to work with network drives:

Dim objNet


'Create object WshNetwork

Set objNet = WScript.CreateObject("WScript.Network")


Dim objDrivers, lngI

'Create collection with information about network drives

Set objDrivers = objNet.EnumNetworkDrives


For lngI=0 To objDrivers.Count-1

        MsgBox objDrivers.Item(lngI) 'output lngI element of collection



'Connect drive Q to network resource \\Server1\Programs

objNet.MapNetWorkDrive "Q:","\\Server1\Programs"


'Delete network drive Q

objNet.RemoveNetworkDrive "Q:"