Solved

Verify/Delete multiple drive and printer mapping in VBScript

Posted on 2008-06-16
4
1,463 Views
Last Modified: 2010-04-21
I'm in the process of configuring my first VBScript and reached an area in which I can map/verify a single drive and printer mapping.  However i now need to perform the same function using multiple drive and printer mappings.  any assist appreciated... 24hrs into vb scripting 101 ;)

see below for an excerpt of what i was able to put together using various samples...


' Start of Script

'Initialize variables
Option Explicit
Dim strDriveLetter1, strRemotePath1; strPrinter
Dim objNetwork, objShell
Dim CheckDrive, AlreadyConnected, intDrive

' Set variables.
strDriveLetter1 = "P:"
strRemotePath1 = "\\FileServer1\FilePath1"
strPrinter1 = "\\PrintServer1\Printer1"

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set CheckDrive = objNetwork.EnumNetworkDrives()

' Compare drive mappings
On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter1 _
Then AlreadyConnected =True
Next

' Check drive mappings and see if the drive is already mapped.
' If yes then disconnects
If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter1
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1

' Verify drive mappings
objShell.PopUp "Drive " & strDriveLetter1 & _
"Disconnected, then connected successfully."
Else
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objShell.PopUp "Drive " & strDriveLetter1 & _
" connected successfully." End if

' Map network printers
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strPrinter

WScript.Quit

' End of Script
0
Comment
Question by:LFC4Llife
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
ID: 21797755
Hi, there's a few different ways of doing such a thing, and there's a lot of examples on EE.  Here's one version that maps multiple network drives, based on the details placed in arrDrives

You can apply the same array based technique for printers too....

Regards,

Rob.
Option Explicit
Dim objNetwork, objShell
'
'Dim Remove Drive
Dim bForce, bUpdateProfile
'
'Dim arrDrives for drives and shares
Dim arrDrives, strDriveDetails, strDrive, strShare
'
'Declare Remove Drive
bForce = "True"
bUpdateProfile = "True"
'
 
Set objNetwork = CreateObject("WScript.Network") 
 
'Declare drive array, separated by the tilde (~) to it's share
arrDrives = Array(_
      "X:~\\server1\itsoftware", _
      "G:~\\server2\it", _
      "Z:~\\server1\allstaffshare", _
      "Y:~\\server2\" & objNetwork.UserName & "$"
 
 
'On Error Resume Next
' Section to map the network drive
'
'--------------------Main------------------------------------
 
For Each strDriveDetails In arrDrives
      strDrive = Split(strDriveDetails, "~")(0)
      strShare = Split(strDriveDetails, "~")(1)
      
      ' Removes strDriveLetter, with bForce, pUpdate Profile
      'On Error Resume Next
      objNetwork.RemoveNetworkDrive strDrive, bforce, bUpdateProfile
 
      ' Map the drive
      objNetwork.MapNetworkDrive strDrive, strShare, bUpdateProfile
Next
'
WScript.Quit

Open in new window

0
 

Author Comment

by:LFC4Llife
ID: 21797797
Thanks... I'm running out now but will check when I gte back...
0
 

Author Closing Comment

by:LFC4Llife
ID: 31467696
Thanks works like a charm...
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21809949
No problem . Thanks for the grade.

Regards,

Rob.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question