Logo
PREGNANCY
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
 
 
Windows Vista

Programming the WshNetwork Object

3/20/2011 10:42:52 PM

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")

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:"

objWshNetwork.RemovePrinterConnection "\\ZEUS\inkjet"

Other -----------------
- Supporting Desktop Applications : Repair a Corrupted Operating System (part 4)
- Supporting Desktop Applications : Repair a Corrupted Operating System (part 3) - Complete PC Backup and Restore
- Supporting Desktop Applications : Repair a Corrupted Operating System (part 2) - System Restore
- Supporting Desktop Applications : Repair a Corrupted Operating System (part 1)
- Maintain Desktop Applications (part 2) - Using Group Policy to Manage Application Compatibility
- Maintain Desktop Applications (part 1) - New Program Compatibility Wizard
- Supporting Desktop Applications : Troubleshoot Software Restrictions
- Support Deployed Applications
- Configure Network Security (part 2 ) - Windows Firewall
- Configure Network Security (part1 ) - Secure Files and Printer Shares with Access Control Lists (ACLs)
- Configure and Troubleshoot Remote Access (part 2) - Troubleshooting Windows Vista Remote Access Connections
- Configure and Troubleshoot Remote Access (part 1) - Remote Client Access Connections
- Configure and Troubleshoot Wireless Networking (part 3) - Troubleshooting Wireless Connections
- Configure and Troubleshoot Wireless Networking (part 2) - Wireless Security
- Configure and Troubleshoot Wireless Networking (part 1) - Managing Wireless Connectivity in the Enterprise
- Troubleshoot Resource Access and Connectivity Issues (part 2)
- Troubleshoot Resource Access and Connectivity Issues (part 1) - Troubleshooting TCP/IP Configuration
- Configure and Troubleshoot Network Services at the Client Level
- Configure and Troubleshoot Network Protocols (part 3) - Configuring TCP/IP Version 6
- Configure and Troubleshoot Network Protocols (part 2) - WINS & NAT
 
 
Most view of day
- Microsoft Visio 2010 : Visualizing Your Data - Creating Data Graphic Legends
- Windows Server 2012 : Managing Users with Local Security and Group Policies (part 1) - Viewing Policies with the Group Policy Management Console, Creating New Group Policies
- Troubleshooting Stop Messages : Common Stop Messages (part 3)
- Using the Windows 7 Libraries : CONSIDERING USER-DEFINED COLLECTIONS
- Advanced Windows 7 Programming : Working in the Background - DEVELOPING TRIGGER-START SERVICES (part 4)
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 5) - Editing an Existing To-Do Item
- Windows Server 2012 : Configuring IPsec (part 6) - Configuring connection security rules - Creating a custom rule, Configuring authenticated bypass
- Windows Phone 8 : Developing for the Phone - The Phone Experience (part 1) - Orientation
- Windows Server 2012 : Deploying Storage Spaces (part 2) - Understanding Storage Spaces - Fixed vs. thin provisioning
- Windows Server 2012 : DHCP,IPv6 and IPAM - Understanding the Components of an Enterprise Network
Top 10
- Configuring and Troubleshooting IPv6 in Windows Vista (part 4) - Troubleshooting IPv6 Connectivity
- Configuring and Troubleshooting IPv6 in Windows Vista (part 3) - Configuring IPv6 in Windows Vista Using Netsh , Other IPv6 Configuration Tasks
- Configuring and Troubleshooting IPv6 in Windows Vista (part 2) - Configuring IPv6 in Windows Vista Using the User Interface
- Configuring and Troubleshooting IPv6 in Windows Vista (part 1) - Displaying IPv6 Address Settings
- Deploying IPv6 : IPv6 Enhancements in Windows Vista
- Games and Windows 7 : Games for Windows - LIVE (part 2) - Accessing Games for Windows - LIVE from within Compatible Games
- Games and Windows 7 : Games for Windows - LIVE (part 1) - Using the Games for Windows - LIVE Marketplace
- Sharepoint 2013 : Client-side Programming - Working with the REST API (part 3)
- Sharepoint 2013 : Client-side Programming - Working with the REST API (part 2) - Working with the REST API in JavaScript
- Sharepoint 2013 : Client-side Programming - Working with the REST API (part 1) - Understanding REST fundamentals
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro