Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Mapped network drives

Posted on 2013-02-06
12
Medium Priority
?
315 Views
Last Modified: 2013-02-06
I have this little script that maps network drives for me. I need to change it to also do the following. If any network drive in the 'arrpath' has already been mapped previously then don't map it again. I don't want to finish up having network drives mapped twice.


Thanks
Cheers


Set objFSO = CreateObject("Scripting.FileSystemObject")
arrDrives = Array("G:", "H:", "I:", "J:", "K:", "L:", "M:", "N:", "O:", "P:" ,"Q:", "R:", "S:", "T:", "U:", "V:", "W:", "X:", "Y:", "Z:")
arrPath = Array("transfer" , "admin" , "econtracts" , "hrteam" , "pmp$")
For each strpath in arrpath
For Each strDrive In arrDrives
If (objFSO.DriveExists(strDrive) = False) Then
strAvailable = strDrive
Exit For
End If
Next

Set objNetwork = CreateObject("WScript.Network")
strHomeServer = "\\tyrenet-14\" & strpath
objNetwork.MapNetworkDrive strAvailable, strHomeServer, True
Wscript.Echo "Mapped Drive " & strAvailable & " to " & strHomeServer
Next
0
Comment
Question by:Bianchi928
[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
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 57

Expert Comment

by:Bill Prew
ID: 38861463
You don't care which drive gets mapped to a particular share?

~bp
0
 

Author Comment

by:Bianchi928
ID: 38861497
We always reference mapped network drive by the name not the letter assigned to it. So I don't care. I just don't want to see double or triple mapped network drives
0
 
LVL 26

Expert Comment

by:Fred Marshall
ID: 38861562
I don't know how you're using the mapped drives .. so perhaps this is of little use.  Nonetheless, is there any concern about number of connections?  If so, here's a way that I use "normal user behavior" to limit the number of connections:

Instead of mapping a drive, create a shortcut on the desktop pointing to the most often used folder.  Then, when file access on the drive is needed, just open using the shortcut.
Most people will close the window when they're done - and this removes a connection.
In the right situations, this has very little downside.  Only you would know if this helps.
0
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!

 

Author Comment

by:Bianchi928
ID: 38861588
No, I don't want to do that. We have different mapped drives for different users and they are all persistent connections. The modification I need in my script is to check if the name in 'arrpath' is not already mapped before mapping it again.

Thanks
Cheers
0
 
LVL 26

Expert Comment

by:Fred Marshall
ID: 38861603
What I generally do is to unmap the drive and remap it with one script.  Seems like this accomplishes the same thing.  The downside is if there are potentially active users when the script runs.  So your way is better in that regard.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38861955
Hi, see how this code goes.

Regards,

Rob.

strServer = "\\tyrenet-14\"
arrDrives = Array("G:", "H:", "I:", "J:", "K:", "L:", "M:", "N:", "O:", "P:" ,"Q:", "R:", "S:", "T:", "U:", "V:", "W:", "X:", "Y:", "Z:")
arrPath = Array("transfer" , "admin" , "econtracts" , "hrteam" , "pmp$")

Set objNetwork = CreateObject("WScript.Network")
Set objExistingDrives = objNetwork.EnumNetworkDrives
Set dctExistingDrives = CreateObject("Scripting.Dictionary")
dctExistingDrives.CompareMode = vbTextCompare
For intDrive = 0 To objExistingDrives.Count - 1 Step 2
	strCurrentShare = Replace(LCase(objExistingDrives(intDrive + 1)), LCase(strServer), "")
	If dctExistingDrives.Exists(strCurrentShare) = False Then dctExistingDrives.Add strCurrentShare, objExistingDrives(intDrive)
Next

Set objFSO = CreateObject("Scripting.FileSystemObject")
For each strpath in arrpath
	If dctExistingDrives.Exists(strPath) = False Then
		For Each strDrive In arrDrives
			If (objFSO.DriveExists(strDrive) = False) Then
				strAvailable = strDrive
				Exit For
			End If
		Next
	
		Set objNetwork = CreateObject("WScript.Network") 
		strHomeServer = strServer & strpath
		objNetwork.MapNetworkDrive strAvailable, strHomeServer, True
		Wscript.Echo "Mapped Drive " & strAvailable & " to " & strHomeServer
	Else
		WScript.Echo strServer & strPath & " is already mapped to " & dctExistingDrives(strPath)
	End If
Next 

Open in new window

0
 

Author Comment

by:Bianchi928
ID: 38861982
Hi Rob,

Thanks for the reply. I ran the script.

Your script shows me a list of mapped drives. (as per attahed). One good things is that it didn't mapped 'transfer' again, but didn't map the others from the 'arrpath'

Cheers
output.bmp
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 800 total points
ID: 38861986
Yeah, I left in WScript.Quit.  I have removed that from the snippet above now.

Rob.
0
 

Author Closing Comment

by:Bianchi928
ID: 38862034
Saviour. You're the man. Thanks. Cheers
0
 
LVL 65

Expert Comment

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

Just to let you know, it uses objNetwork.EnumNetworkDrives to enumerate the currently connected network drives, that gives you the drive letter and share path of each, as objExistingDrives(intDrive) and objExistingDrives(intDrive + 1) respectively.  This is why you have to use Step 2 through the array.

That's how we check the existing connections, and avoid them is one matchs the arrPaths values.

Rob.
0
 

Author Comment

by:Bianchi928
ID: 38862095
Thanks for the clarification..I didn't know that objExistingDrives(intDrive + 1) will return me the path.
Thanks
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38862103
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

721 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