Programming4us
         
 
 
Windows

Scripting Windows 7 with WSH : Programming the WshNetwork Object

5/3/2011 11:31:33 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 -----------------
- Scripting Windows 7 with WSH : Programming the WshShell Object (part 2)
- Scripting Windows 7 with WSH : Programming the WshShell Object (part 1)
- Scripting Windows 7 with WSH : Programming the WScript Object
- Scripting Windows 7 with WSH : Programming Objects
- Scripting Windows 7 with WSH : Scripts and Script Execution
- Adding Macs to Your Windows 7 Network : Letting Windows Computers See Your Mac Shares
- Adding Macs to Your Windows 7 Network : Using a Mac to Make a Remote Desktop Connection to Windows 7
- Adding Macs to Your Windows 7 Network : Connecting to a Windows Shared Folder
- Adding Macs to Your Windows 7 Network : Connecting to the Windows Network
- Windows 7 : Controlling and Customizing Your Website (part 5) - Viewing the Server Logs
- Windows 7 : Controlling and Customizing Your Website (part 4) - Disabling Anonymous Access
- Windows 7 : Controlling and Customizing Your Website (part 3) - Working Without a Default Document
- Windows 7 : Controlling and Customizing Your Website (part 2) - Setting the Website’s Default Document
- Windows 7 : Controlling and Customizing Your Website (part 1)
- Windows 7 : Adding Folders and Files to the Default Website (part 3) - Adding a Folder to the Default Website
- Windows 7 : Adding Folders and Files to the Default Website (part 2) - Changing the Default Website Home Page
- Windows 7 : Adding Folders and Files to the Default Website (part 1) - Setting Permissions on the Default Website Folder
- Turning Windows 7 into a Web Server : Understanding the Default Website
- Turning Windows 7 into a Web Server : Accessing Your Website
- Windows 7 : Installing Internet Information Services
 
 
Most View
- The Art of SEO : Determining Keyword Value/Potential ROI
- Windows Azure: Building a Secure Backup System (part 1)
- Windows Server 2008 : Install and Configure the DNS Server Role
- Windows Phone 7 : Working with the Calendar - Deleting Appointments
- Parallel Programming Drivers
- Exchange Server 2010 : Planning for Unified Messaging (part 2)
- Developing Applications for Windows Phone 7 : Visual Grammar
- A Brief History of Legacy .NET Distributed Technologies : .NET Remoting
- Microsoft ASP.NET 4 : Ajax - Extender Controls (part 1) - The AutoComplete Extender
- Windows 7 : Creating a Windows Network - Choosing a Network and Cabling System
Top 10
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 3) - Configuring Recipient Filtering
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 2)
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 1)
- Implementing Edge Services for an Exchange Server 2007 Environment : Installing and Configuring the Edge Transport Server Components
- What's New in SharePoint 2013 (part 7) - BCS
- What's New in SharePoint 2013 (part 6) - SEARCH
- What's New in SharePoint 2013 (part 6) - WEB CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 5) - ENTERPRISE CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 4) - WORKFLOWS
- What's New in SharePoint 2013 (part 3) - REMOTE EVENTS