DESKTOP

Windows Vista : Programming the WshNetwork Object

12/20/2012 3:28:38 AM

WshNetwork is a generic name for an object that enables you to work with various aspects of the Windows network environment. You can determine the computer name and username, you can enumerate the mapped network drives, you can map new network drives, and more. The next couple of sections show you how to work with this object.

Referencing the WshNetwork Object

WshNetwork refers to the Network object exposed via the Automation interface of WScript. This means you use CreateObject to return this object, as shown here:

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

From here, you use the WshNetwork variable to access the object’s properties and methods.

WshNetwork Object Properties

The WshNetwork object supports three properties:

ComputerNameReturns the network name of the computer
UserDomainReturns the network domain name of the current user
UserNameReturns the username of the current user

Mapping Network Printers

The WshNetwork object supports several methods for working with remote printers. For example, to map a network printer to a local printer resource, use the WshNetwork object’s AddWindowsPrinterConnection method:

WshNetwork.AddPrinterConnection strPrinterPath
						

WshNetworkThe WshNetwork object
strPrinterPathThe UNC path to the network printer

Here’s an example:

Set objWshNetwork = WScript.CreateObject("WScript.Network")
objWshNetwork.AddWindowsPrinterConnection "\\ZEUS\printer"

To remove a remote printer mapping, use the WshNetwork object’s RemovePrinterConnection method:

WshNetwork.RemovePrinterConnection strPrinterPath [, bForce] [, bUpdateProfile]


					  

WshNetworkThe WshNetwork object
strPrinterPathThe UNC path to the network printer
bForceIf True, the resource is removed even if it is currently being used
bUpdateProfileIf True, the printer mapping is removed from the user’s profile

Here’s an example:

Set objWshNetwork = WScript.CreateObject("WScript.Network")
objWshNetwork.RemovePrinterConnection "\\ZEUS\inkjet"

Mapping Network Drives

The WshNetwork object supports several methods for mapping network drives. To map a shared network folder to a local drive letter, use the WshNetwork object’s MapNetworkDrive method:

WshNetwork.MapNetworkDrive strLocalName, strRemoteName, [bUpdateProfile], [strUser], [strPassword]


					  

WshNetworkThe WshNetwork object
strLocalNameThe local drive letter to which the remote share will be mapped (for example, F:)
strRemoteNameThe UNC path for the remote share
bUpdateProfileIf True, the drive mapping is stored in the user’s profile
strUserUse this value to enter a username that might be required to map the remote share (if you’re logged on as a user who doesn’t have the proper permissions, for example)
strPasswordUse this value to enter a password that might be required to map the remote drive

Here’s an example:

Set objWshNetwork = WScript.CreateObject("WScript.Network")
objWshNetwork.MapNetworkDrive "Z:", "\\ZEUS\SharedDocs"

To remove a mapped network drive, use the WshNetwork object’s RemoveNetworkDrive:

WshNetwork.RemoveNetworkDrive strName, [bForce], [bUpdateProfile]

WshNetworkThe WshNetwork object.
strNameThe name of the mapped network drive you want removed. If you use a network path, all mappings to that path are removed; if you use a local drive letter, only that mapping is removed.
bForceIf True, the resource is removed even if it is currently being used.
bUpdateProfileIf True, the network drive mapping is removed from the user’s profile.

Here’s an example:

Set objWshNetwork = WScript.CreateObject("WScript.Network")
objWshNetwork.RemoveNetworkDrive "Z:"
Other  
 
Top 10
Review : Sigma 24mm f/1.4 DG HSM Art
Review : Canon EF11-24mm f/4L USM
Review : Creative Sound Blaster Roar 2
Review : Philips Fidelio M2L
Review : Alienware 17 - Dell's Alienware laptops
Review Smartwatch : Wellograph
Review : Xiaomi Redmi 2
Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8