Solved

Verify/Delete multiple drive and printer mapping in VBScript

Posted on 2008-06-16
4
1,462 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reboot server with scheduled time and week base 4 65
Button function on table is in trouble 3 35
Index Match Formula VBA 6 52
VB script help 23 36
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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