?
Solved

Verify/Delete multiple drive and printer mapping in VBScript

Posted on 2008-06-16
4
Medium Priority
?
1,465 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 1000 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

Technology Partners: 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 script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

741 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