Solved

Verify/Delete multiple drive and printer mapping in VBScript

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

929 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

14 Experts available now in Live!

Get 1:1 Help Now