Solved

Verify/Delete multiple drive and printer mapping in VBScript

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now