[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Select Case maps drive and disconnects

Posted on 2009-04-16
4
Medium Priority
?
380 Views
Last Modified: 2012-08-13
The below login script will map the H: drive with the Select Case statement, and then it will remove it immediately.  The other drives map just fine but the H: fails.
'This script checks for the exsistence of network drives and writes them to 
'c:\documents and settings\%userpfrofile%\OldDriveslistDrives.txt
'This happens only the first time that it runs. 
'The script then deletes all network drives and maps the new drives
'The new drives are mapped one of two ways:
'1. Under the section labled ALL USERS these drives map for everyone
'2. Under the section labled BY GROUP these drives map only for groups
'---------------------------------------------------------------------------------------------------------------
 
On Error Resume Next
 
Set WshS = WScript.CreateObject("WScript.Shell")
strDirectory = WshS.ExpandEnvironmentStrings("%UserProfile%") & "\oldDrivesPrinters1"
'wscript.echo strDirectory
strOldDrivesFile = "\listDrives.txt"
strOldPrintersFile = "\listOldPrinters.txt"
strText = ""
	
Set objSysInfo = CreateObject("ADSystemInfo")      'Get user distinguished name 
Set objNetwork = CreateObject("Wscript.Network")   
strUserPath = "LDAP://" & objSysInfo.UserName           
Set objUser = GetObject(strUserPath)               'Bind to the user account in Active Directory 
If TypeName(objUser.MemberOf) = "Empty" Then
	'WScript.Echo "You are not a member of any groups."
ElseIf TypeName(objUser.MemberOf) = "String" Then
	strGroupName = Mid(Split(objUser.MemberOf, ",")(0), 4)
	CheckGroup strGroupName
Else
	For Each strGroup In objUser.MemberOf       'Walk through the groups that user is a member of
		strGroupName = Mid(Split(strGroup, ",")(0), 4)              'Get the group name
	    CheckGroup strGroupName
	Next
End If
 
getCurrentDrives
ListAndRemoveCurrentPrinters
 
'--------------------------Remove all mapped drives--------------------------------------------------------------
 
Set CheckDrive = objNetwork.EnumNetworkDrives()
strDriveLetter = UCase("z:" ) ' Force UPPER case
Dim intdrive, bforce, bUpdateProfile
 
bforce = true
bUpdateProfile = true
 
For intDrive = 0 To CheckDrive.Count - 1 Step 2 
If CheckDrive.Item(intDrive) <> strDriveLetter Then
objNetwork.RemoveNetworkDrive CheckDrive.Item(intDrive), bforce, bUpdateProfile
wscript.sleep 300
End if
Next 
objNetwork.RemoveNetworkDrive "Z:", bforce, bUpdateProfile
'-----------------------------------------------------------------------------------------------------------------
 
'-----------------------------------------------------------------------------------------------------------------
 
 
'---------------------------------------ALL USERS-----------------------------------------------------------------
'--------------------------Add drives in this section that will map for all users---------------------------------
objNetwork.MapNetworkDrive "p:", "\\crdc01\public$"
objNetwork.MapNetworkDrive "s:", "\\crdc01\shared$"
objNetwork.MapNetworkDrive "u:", "\\crdc01\users$"
 
'-----------------------------------------------------------------------------------------------------------------
'--------------------------Add all Printers in this section ------------------------------------------------------
 
'Define network printers to be connected
Dim colNewPrinters (9)
colNewPrinters (0) = "\\crdc01\DHI-Dell M5200"
colNewPrinters (1) = "\\crdc01\DHI-(Color)Dell 3100cn"
'colNewPrinters (2) = "\\crdc01\DHI-Brandi-HP LJ 4V"
colNewPrinters (2) = "\\crdc01\DHI-HP LJ 5000"
colNewPrinters (3) = "\\crdc01\RE-Admin-HP LJ 4000"
colNewPrinters (4) = "\\crdc01\RE-Chris(Color)-HP CLJ 4600"
colNewPrinters (5) = "\\crdc01\RE-FrontDesk-HP LJ 4050"
colNewPrinters (6) = "\\crdc01\RE-Sales-Dell 5200"
colNewPrinters (7) = "\\crdc01\RE-Color Copier-Cannon C6800"
colNewPrinters (8) = "\\crdc01\RE-Admin-Plotter-HP DJ 600"
 
 
For Each strNewPrinter In colNewPrinters
	objNetwork.AddWindowsPrinterConnection strNewPrinter
Next
 
'-----------------------------------------------------------------------------------------------------------------
'
'For Each strGroup in objUser.MemberOf       'Walk through the groups that user is a member of
'    strGroupPath = "LDAP://" & strGroup       
'    Set objGroup = GetObject(strGroupPath)  'Bind to the group in Active Directory   
'    strGroupName = objGroup.CN              'Get the group name
'
'    strGroupName = lcase(strGroupName)      'Set group name to all lowercase
'   
'-------------------------------------BY GROUP--------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------
 
Sub CheckGroup(strGroupName)
	On error resume next
	Set objNetwork = CreateObject("WScript.Network")
	Select Case LCase(strGroupName)
		Case "yardi users"   'If in this group map the drive on next line
			objNetwork.MapNetworkDrive "h:", "\\dhisvr\programs"
	End Select
End Sub
 
'-----------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------
'Next
 
sub getCurrentDrives()
	Set objFSO = CreateObject("Scripting.FileSystemObject")
        If objFSO.FileExists(strDirectory & strOldDrivesFile) Then
        
        else
		createFolderAndFileIfNotExist strOldDrivesFile
 
		Set objDrive = objNetwork.EnumNetworkDrives 
 
		' Extra section to troubleshoot
		If objDrive.Count = 0 Then 
		'WScript.Echo " No Drives Mapped "
		End If 
		
		' Here is the where the script reads the array
		For intDrive = 0 To (objDrive.Count -1) Step 2
			intNetLetter = IntNetLetter +1
			appendToFile objDrive.Item(intDrive) & " " & objDrive.Item(intDrive +1), strOldDrivesFile
		Next
	end if 	
	
end sub
 
sub ListAndRemoveCurrentPrinters()
	Set objFSO = CreateObject("Scripting.FileSystemObject")
        If objFSO.FileExists(strDirectory & strOldPrintersFile) Then
        
        else
		createFolderAndFileIfNotExist strOldPrintersFile
 
		Set objOldPrinters = objNetwork.EnumPrinterConnections
		
		For nPrinter = 0 To objOldPrinters.Count - 1 Step 2
			strPrinter = objOldPrinters.Item ( nPrinter + 1 )
			If Left ( strPrinter, 4) = "\\cr" Then
				appendToFile strPrinter, strOldPrintersFile 
				objNetwork.RemovePrinterConnection strPrinter, True, True  'removes printer
			end if 
		Next
		wscript.echo "Your default printer may have changed. Please verify your default printer is correct."
	end if 	
end sub
 
 
 
 
sub createFolderAndFileIfNotExist(strFile)
 
	
	exsist = 0
 
	' Create the File System Object
	Set objFSO = CreateObject("Scripting.FileSystemObject")
 
	' Check that the strDirectory folder exists
	If objFSO.FolderExists(strDirectory) Then
		exsist = 1
		exit sub
	Else
		Set objFolder = objFSO.CreateFolder(strDirectory)
	End If
 
 
	If objFSO.FileExists(strDirectory & strFile) Then
	Set objFolder = objFSO.GetFolder(strDirectory)
	Else
	Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
	End If 
 
	set objFile = nothing
	set objFolder = Nothing
	exsist = 1
 
end sub
 
sub appendToFile(strText, strFile)
        Set objFSO = CreateObject("Scripting.FileSystemObject")
	' OpenTextFile Method needs a Const value
	' ForAppending = 8 ForReading = 1, ForWriting = 2
	Const ForAppending = 8
 
	Set objTextFile = objFSO.OpenTextFile (strDirectory & strFile, ForAppending, True)
 
	' Key Section to write the strText to the file.
	' Writes strText every time you run this VBScript
	objTextFile.WriteLine(strText)
	objTextFile.Close
 
end sub
 
wscript.quit

Open in new window

0
Comment
Question by:bbanis2k
  • 3
4 Comments
 
LVL 12

Expert Comment

by:Krys_K
ID: 24161839
Hi There
To answer your question:
In your script, your logic is:
  • Bind To user
  • Check the group memberships
  • If user is in any groups, you call a Sub (CheckGroup)
  • If user is in the group called yardi users you then map H:
  • Then you call a sub getCurrentDrives
  • Then you call ListAndRemoveCurrentPrinters
  • Then you enumerate all mapped drives (H is now mapped at this point) and remove all mapped drives and log them
  • ...... Script continues onto other things
Hope this helps
Regards
Krystian
0
 
LVL 12

Expert Comment

by:Krys_K
ID: 24190208
To add to my previous post, what you need to do is move the logic about a bit to then map H: after you do the removing of all mapped drives.
If you need help with this then let me know
Regards
Krystian
0
 

Author Comment

by:bbanis2k
ID: 24190919
Can you post the code?  I want to award points after testing your code.
0
 
LVL 12

Accepted Solution

by:
Krys_K earned 2000 total points
ID: 24200695
Hi There
I've moved the section where you check the users group membership down to when the drives get mapped in one go. This will prevent the H drive from being mapped before the drives all get removed.
This should solve the problem you asked about
With Regards,
Krystian

'This script checks for the exsistence of network drives and writes them to 
'c:\documents and settings\%userpfrofile%\OldDriveslistDrives.txt
'This happens only the first time that it runs. 
'The script then deletes all network drives and maps the new drives
'The new drives are mapped one of two ways:
'1. Under the section labled ALL USERS these drives map for everyone
'2. Under the section labled BY GROUP these drives map only for groups
'---------------------------------------------------------------------------------------------------------------
 
	On Error Resume Next
	 
	Set WshS = WScript.CreateObject("WScript.Shell")
	strDirectory = WshS.ExpandEnvironmentStrings("%UserProfile%") & "\oldDrivesPrinters1\"
	'wscript.echo strDirectory
	strOldDrivesFile = "listDrives.txt"
	strOldPrintersFile = "listOldPrinters.txt"
	strText = ""
		
	Set objSysInfo = CreateObject("ADSystemInfo")      'Get user distinguished name 
	Set objNetwork = CreateObject("Wscript.Network")   
	
	getCurrentDrives
	ListAndRemoveCurrentPrinters
	 
'--------------------------Remove all mapped drives--------------------------------------------------------------
	 
	Set CheckDrive = objNetwork.EnumNetworkDrives()
	strDriveLetter = UCase("z:" ) ' Force UPPER Case
	Dim intdrive, bforce, bUpdateProfile
	 
	bforce = true
	bUpdateProfile = true
	 
	For intDrive = 0 To CheckDrive.Count - 1 Step 2 
	If CheckDrive.Item(intDrive) <> strDriveLetter Then
	objNetwork.RemoveNetworkDrive CheckDrive.Item(intDrive), bforce, bUpdateProfile
	wscript.sleep 300
	End if
	Next 
	objNetwork.RemoveNetworkDrive "Z:", bforce, bUpdateProfile
'-----------------------------------------------------------------------------------------------------------------
	 
'-----------------------------------------------------------------------------------------------------------------
	 
	 
'---------------------------------------ALL USERS-----------------------------------------------------------------
'--------------------------Add drives in this section that will map for all users---------------------------------
 
	strUserPath = "LDAP://" & objSysInfo.UserName           
	Set objUser = GetObject(strUserPath)               'Bind to the user account in Active Directory 
	
		For Each strGroup In objUser.MemberOf       'Walk through the groups that user is a member of
			strGroupName = Mid(Split(strGroup, ",")(0), 4)              'Get the group name
		    
		' Maps the H: Drive if user is a member of Yardi Users
		    CheckGroup strGroupName
		Next
 
	objNetwork.MapNetworkDrive "p:", "\\crdc01\public$"
	objNetwork.MapNetworkDrive "s:", "\\crdc01\shared$"
	objNetwork.MapNetworkDrive "u:", "\\crdc01\users$"
	 
'-----------------------------------------------------------------------------------------------------------------
'--------------------------Add all Printers in this section ------------------------------------------------------
	 
'Define network printers to be connected
	Dim colNewPrinters (9)
	colNewPrinters (0) = "\\crdc01\DHI-Dell M5200"
	colNewPrinters (1) = "\\crdc01\DHI-(Color)Dell 3100cn"
	'colNewPrinters (2) = "\\crdc01\DHI-Brandi-HP LJ 4V"
	colNewPrinters (2) = "\\crdc01\DHI-HP LJ 5000"
	colNewPrinters (3) = "\\crdc01\RE-Admin-HP LJ 4000"
	colNewPrinters (4) = "\\crdc01\RE-Chris(Color)-HP CLJ 4600"
	colNewPrinters (5) = "\\crdc01\RE-FrontDesk-HP LJ 4050"
	colNewPrinters (6) = "\\crdc01\RE-Sales-Dell 5200"
	colNewPrinters (7) = "\\crdc01\RE-Color Copier-Cannon C6800"
	colNewPrinters (8) = "\\crdc01\RE-Admin-Plotter-HP DJ 600"
	 
	 
	For Each strNewPrinter In colNewPrinters
		objNetwork.AddWindowsPrinterConnection strNewPrinter
	Next
	 
'-----------------------------------------------------------------------------------------------------------------
'
'For Each strGroup in objUser.MemberOf       'Walk through the groups that user is a member of
'    strGroupPath = "LDAP://" & strGroup       
'    Set objGroup = GetObject(strGroupPath)  'Bind to the group in Active Directory   
'    strGroupName = objGroup.CN              'Get the group name
'
'    strGroupName = lcase(strGroupName)      'Set group name to all lowercase
'   
'-------------------------------------BY GROUP--------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------
 
 
Sub CheckGroup(strGroupName)
	On error resume next
	Set objNetwork = CreateObject("WScript.Network")
	Select Case LCase(strGroupName)
		Case "yardi users"   'If in this group map the drive on next line
			objNetwork.MapNetworkDrive "h:", "\\dhisvr\programs"
	End Select
End Sub
 
sub getCurrentDrives()
	Set objFSO = CreateObject("Scripting.FileSystemObject")
        If objFSO.FileExists(strDirectory & strOldDrivesFile) Then
        
        Else
			createFolderAndFileIfNotExist strOldDrivesFile
	 
			Set objDrive = objNetwork.EnumNetworkDrives 
	 
			' Extra section to troubleshoot
			If objDrive.Count = 0 Then 
				'WScript.Echo " No Drives Mapped "
			End If 
		
		' Here is the where the script reads the Array
			For intDrive = 0 To (objDrive.Count -1) Step 2
				intNetLetter = IntNetLetter +1
				appendToFile objDrive.Item(intDrive) & " " & objDrive.Item(intDrive +1), strOldDrivesFile
			Next
		End If 	
	
End Sub
 
sub ListAndRemoveCurrentPrinters()
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If objFSO.FileExists(strDirectory & strOldPrintersFile) Then
        
    Else
		createFolderAndFileIfNotExist strOldPrintersFile
 
		Set objOldPrinters = objNetwork.EnumPrinterConnections
		
		For nPrinter = 0 To objOldPrinters.Count - 1 Step 2
			strPrinter = objOldPrinters.Item ( nPrinter + 1 )
			If Left ( strPrinter, 4) = "\\cr" Then
				appendToFile strPrinter, strOldPrintersFile 
				objNetwork.RemovePrinterConnection strPrinter, True, True  'removes printer
			end if 
		Next
		wscript.echo "Your default printer may have changed. Please verify your default printer is correct."
	end if 	
end Sub
 
sub createFolderAndFileIfNotExist(strFile)
 
	
	exsist = 0
 
	' Create the File System Object
	Set objFSO = CreateObject("Scripting.FileSystemObject")
 
	' Check that the strDirectory folder exists
	If objFSO.FolderExists(strDirectory) Then
		exsist = 1
		exit sub
	Else
		Set objFolder = objFSO.CreateFolder(strDirectory)
	End If
 
 
	If objFSO.FileExists(strDirectory & strFile) Then
	Set objFolder = objFSO.GetFolder(strDirectory)
	Else
	Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
	End If 
 
	set objFile = nothing
	set objFolder = Nothing
	exsist = 1
 
end sub
 
sub appendToFile(strText, strFile)
        Set objFSO = CreateObject("Scripting.FileSystemObject")
	' OpenTextFile Method needs a Const value
	' ForAppending = 8 ForReading = 1, ForWriting = 2
	Const ForAppending = 8
 
	Set objTextFile = objFSO.OpenTextFile (strDirectory & strFile, ForAppending, True)
 
	' Key Section to write the strText to the file.
	' Writes strText every time you run this VBScript
	objTextFile.WriteLine(strText)
	objTextFile.Close
 
end sub
 
wscript.quit

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

873 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