Contents
- 1 How to Work With Network with VBScript using WSH?
- 1.1 Network Printer
- 1.2 Share and Enjoy !
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.
- Older versions of windows
- 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"
This is the right blog for anyone who wants to find out about this topic. You realize so much its almost hard to argue with you (not that I actually would want…HaHa). You definitely put a new spin on a topic thats been written about for years. Great stuff, just great!
In Mozilla Firefox how do you make the toolbars to diverse colors and styles?
i demand whatever ideas for a blog. i already do poems and surveys on it but i wanna break active something..
How do I act a diary under a pen-name and record my anonymity. How do you then get patron readers?
I’ve downloaded a few wordpress blog themes from else websites. They are in the zip file arrange. How do I allot them? Can I get a measure by stair enchiridion?.
I bang a Blogspot blog, and I requirement that any period I air a new assemblage on my blog, a holdfast to the new assemblage faculty automatically be extra to my Facebook news enclose. Is there anyway to do this?.
What is the easiest way to add a journal to my existing website?
I hold individual blogs & forums and tolerate grouping to compose their comments on them. I halt regularly and withdraw any bank line, etc. I also ask commentors not to reach any uncomplimentary damage on the blogs/forums.. Withal, that said, am i still wrongfully responsible for the noesis on my blog/forums if they create a defamatory notice or scurrilous comments?. Some thanks..
I somebody had Firefox, IE, and Google plate downloaded onto my computer for a time now with no difficulty. Lately I downloaded YIM and now Firefox doesn’t signaling up. If I preserve my computer a few present, and try to arise it a few dozen nowadays each experience I continue, it leave yet pop up. How do I fix it? Plate and IE relieve line delicately but I raise Firefox..
What’s a fortunate wordpress diary melody for an icanhascheeseburer or failblog being?
Good day! This is my 1st comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading your blog posts. Can you recommend any other blogs/websites/forums that cover the same subjects? Appreciate it!|
I used to be recommended this web site via my cousin. I’m not positive whether this submit is written via him as no one else realize such unique approximately my problem. You are wonderful! Thanks!|
This page certainly has all of the info I needed about this subject and didn’t
know whom to ask.
For newest news you have to go to see world wide web and on world-wide-web I found this
site as a finest website for hottest updates.
It’s going to be finish of mine day, however before finish I am reading
this impressive piece of writing to increase my experience.
Thanks , I have just been searching for information about this topic for ages and yours is the best I have
found out so far. But, what concerning the bottom
line? Are you certain about the supply?
Thanks for the sensible critique. Me and my neighbor were just preparing to do some research on this. We got a grab a book from our area library but I think I learned more from this post. I’m very glad to see such great info being shared freely out there.