Solved

Script to detect if mapped drive already exists

Posted on 2008-06-19
4
667 Views
Last Modified: 2010-04-21
Hello,

I have a script running that is mapping a network drive but in some cases where the mapped drive already exists, the script generates a "Local Device already in use" error. below is the code for the script and it is line 49 that it hangs up on when trying to map the V: drive. Any help is appreciated!

Set objSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("Wscript.Network")
 
strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)
 
If TypeName(objUser.MemberOf) = "Empty" Then
      strGroup = ""
ElseIf TypeName(objUser.MemberOf) = "String" Then
      strGroup = objUser.MemberOf
      CheckGroup strGroup
Else
      For Each strGroup In objUser.MemberOf
            CheckGroup strGroup
      Next
End If
 
Sub CheckGroup(strGroupDN)
    strGroupPath = "LDAP://" & strGroupDN
    Set objGroup = GetObject(strGroupPath)
    strGroupName = objGroup.CN
    'wscript.echo strGroupName
 
    Select Case strGroupName
               
        Case "Office Admin Dept"
            objNetwork.MapNetworkDrive "X:", "\\cityview\home\shared\_Administration Department"
 
      Case "Building Department"
            objNetwork.MapNetworkDrive "X:", "\\cityview\home\shared\_Building Department"
 
      Case "Clerks and Bylaw Dept"
            objNetwork.MapNetworkDrive "X:", "\\cityview\home\shared\_Clerks Department"
 
      Case "Finance Dept"
            objNetwork.MapNetworkDrive "X:", "\\cityview\home\shared\_Finance Department"
 
      Case "Fire Dept"
            objNetwork.MapNetworkDrive "X:", "\\cityview\home\shared\_Fire Department"      
 
      Case "Public Works and Roads Dept"
            objNetwork.MapNetworkDrive "X:", "\\cityview\home\shared\_Public Works Department"
 
      Case "Recreation Department"
            objNetwork.MapNetworkDrive "X:", "\\cityview\home\shared\_Recreation Department"

'Map V: Drive for CityView - for members part of the CityView users Group
      Case "CityView Users"
            objNetwork.MapNetworkDrive "V:", "\\cityview\cityview"  

  End Select
End Sub
0
Comment
Question by:Schnizzle
  • 2
4 Comments
 
LVL 38

Assisted Solution

by:Shift-3
Shift-3 earned 150 total points
ID: 21822187
It's simpler just to disconnect each drive before mapping it.  This also saves trouble later if you change the paths of any of the drives.

You could add the code below to each of the Select statements.

Alternately, you could remove the If statement in order to disconnect all network drives.  This way you only have to do it once, though it could cause a problem if users have custom drives mapped.


Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colDrives = objWMIService.ExecQuery("Select * From Win32_NetworkConnection")
 
For Each objDrive in colDrives
	If UCase(objDrive.LocalName) = "X:" Then
		objNetwork.RemoveNetworkDrive objDrive.LocalName, True, True
	End If
Next

Open in new window

0
 
LVL 10

Accepted Solution

by:
Dxpert earned 350 total points
ID: 21822200
Just add "On Error Resume Next" in the first line of your code. That will supress the error messsages and allow the script to continue.


	On Error Resume Next
 
	strGroupPath = "LDAP://" & strGroupDN
	Set objGroup = GetObject(strGroupPath)
	strGroupName = objGroup.CN
	'wscript.echo strGroupName
 
	Select Case strGroupName
 
		Case "Office Admin Dept"
			objNetwork.MapNetworkDrive "X:", "\\cityview\home\shared\_Administration Department"
 
		Case "Building Department"
			objNetwork.MapNetworkDrive "X:", "\\cityview\home\shared\_Building Department"
 
		Case "Clerks and Bylaw Dept"
			objNetwork.MapNetworkDrive "X:", "\\cityview\home\shared\_Clerks Department"
 
		Case "Finance Dept"
			objNetwork.MapNetworkDrive "X:", "\\cityview\home\shared\_Finance Department"
 
		Case "Fire Dept"
			objNetwork.MapNetworkDrive "X:", "\\cityview\home\shared\_Fire Department"      
 
		Case "Public Works and Roads Dept"
			objNetwork.MapNetworkDrive "X:", "\\cityview\home\shared\_Public Works Department"
 
		Case "Recreation Department"
			objNetwork.MapNetworkDrive "X:", "\\cityview\home\shared\_Recreation Department"
 
		'Map V: Drive for CityView - for members part of the CityView users Group
		Case "CityView Users"
			objNetwork.MapNetworkDrive "V:", "\\cityview\cityview"  
 
	End Select
	
End Sub

Open in new window

0
 

Author Closing Comment

by:Schnizzle
ID: 31468759
Thanks Guys. I used the On error resume next but the other suggestion is good too. Anyway, i'm all sorted now! thanks for your quick response.
0
 
LVL 10

Expert Comment

by:Dxpert
ID: 21822467
Thanks! Glad I could help ;-)
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check version 13 75
Only allow one site in Internet Explorer on XenApp. 9 64
Server 2012 management. 5 24
Creating An Intelligent Dropdown, Part Deux! 9 28
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

772 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