Solved

VBScript Map drives based on site

Posted on 2008-10-31
48
654 Views
Last Modified: 2012-05-05
I'm using the following simple vbscript to map network drives.  Is there any way to either map a drive based on site assignment, group assignment, or what dc controller they are loggin onto.  Here's the problem everyone mapes to a similar share on their local file server and I want a way to do this through a lvb ogin script especialy with users moving form location to location quite often.
Set objNetwork = CreateObject("WScript.Network") 

objNetwork.MapNetworkDrive "F:","\\servername\share"

objNetwork.MapNetworkDrive "G:","\\servername\share"

Open in new window

0
Comment
Question by:georgedschneider
  • 24
  • 24
48 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 22855946
Hi, there's a couple of options here that perform operations based on the client's IP address subnet.

You could also check the domain controller from the DOS environment variable "LOGONSERVER".

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23749378.html

Regards,

Rob.
0
 

Author Comment

by:georgedschneider
ID: 22877246
If I run echo %logonserver% to get the logon server from the cm line how can I incorporate it into a scrip based on 9 possible servers to map a specific drive.  For example if echo %logonserver% = server1 then I want to mapt G to server1\share
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 22880030
Hi, you can check which domain controller used by using the following code.

If you need any more assistance mapping the drives, let me know.

Regards,

Rob.
Set objShell = CreateObject("WScript.Shell")

Set objNetwork = CreateObject("WScript.Network")

strLogonServer = UCase(objShell.ExpandEnvironmentStrings("%LOGONSERVER%")\)

Select Case strLogonServer

	Case UCase("DCSERVER1")

		strDrive = "G:"

		strPath = "\\FILESERVER1\Share1"

		objNetwork.MapNetworkDrive strDrive, strPath

	Case UCase("DCSERVER2")

		strDrive = "G:"

		strPath = "\\FILESERVER2\Share1"

		objNetwork.MapNetworkDrive strDrive, strPath

	Case UCase("DCSERVER3")

		strDrive = "G:"

		strPath = "\\FILESERVER3\Share1"

		objNetwork.MapNetworkDrive strDrive, strPath

	Case Else

		' No domain controller was used for authentication

End Select

Open in new window

0
 

Author Comment

by:georgedschneider
ID: 22900082
What does this line do:
strLogonServer = UCase(objShell.ExpandEnvironmentStrings("%LOGONSERVER%")\
0
 

Author Comment

by:georgedschneider
ID: 22900309
Never mind.  I figured it out that it converts it to upper case.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22900904
Yes, that's right, but you did point out that there's an extra backslash that shouldn't be there....

Line three should be this:
strLogonServer = UCase(objShell.ExpandEnvironmentStrings("%LOGONSERVER%"))

Regards,

Rob.
0
 

Author Comment

by:georgedschneider
ID: 22942559
Is there a way to map a drive based a computer name.  For examply in our naming convention at ecah site the computer name always starts with the two or three letter site identifier.  Based on that I'd like to map a specifc drive.  

Also is there a way to map based on group membership in AD.  I think the IP methodology might get  alittle messy.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22945508
Yes, there's ways to do both.....for the computername, a "two or three" letter identifier might get messy, but let's they're all three, you can use something like:

'==============
Set objNetwork = CreateObject("WScript.Network")
strSite = UCase(Left(objNetwork.ComputerName, 3))
Select Case strSite
      Case UCase("AAA")
            objNetwork.MapNetworkDrive "G:", "\\server\share1"
      Case UCase("BBB")
            objNetwork.MapNetworkDrive "G:", "\\server\share2"
End Select
'==============

For group membership, see the below code.

Regards,

Rob.
' THIS LINE MUST BE PLACED IN THE MAIN CODE TO ALLOW THE DICTIONARY

' OBJECT TO REMAIN PERSISTENT FOR THE ISMEMBEROFGROUP FUNCTION

Dim objMemberships

 

Set objNetwork = CreateObject("WScript.Network")

Set objADSysInfo = CreateObject("ADSystemInfo")

Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)

Set objShell = CreateObject("WScript.Shell")

 

If IsMemberOfGroup(objUser, "Test Group") = True Then

	objNetwork.MapNetworkDrive "G:", "\\server\share1"

End If

If IsMemberOfGroup(objUser, "Marketing") = True Then

	objNetwork.MapNetworkDrive "G:", "\\server\share2"

End If

 

Function IsMemberOfGroup(objADUser, strGroupCN)

	If IsEmpty(objMemberships) = True Then

		Set objMemberships = CreateObject("Scripting.Dictionary")

		objMemberships.Add LCase("ALL"), 0

		If IsNull(objADUser.MemberOf) = False Then

			If TypeName(objADUser.MemberOf) = "String" Then

				objMemberships.Add LCase(Mid(Split(objADUser.MemberOf, ",")(0), 4)), 0

			Else

				For Each strGroupName In objADUser.MemberOf

					objMemberships.Add LCase(Mid(Split(strGroupName, ",")(0), 4)), 0

				Next

			End If

		End If

	End If

	If objMemberships.Exists(LCase(strGroupCN)) = True Then

		IsMemberOfGroup = True

	Else

		IsMemberOfGroup = False

	End If

End Function

Open in new window

0
 

Author Comment

by:georgedschneider
ID: 22984840
I'll give this a try.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23113943
Hi there. Did you have any luck with the latest option?

Regards,

Rob.
0
 

Author Comment

by:georgedschneider
ID: 23120935
Sorry for the delay.  I'll being testing it out this week
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23220173
Hi, any luck so far?

Rob.
0
 

Author Comment

by:georgedschneider
ID: 23229924
In the link provided above there is sample of how to map a drive based on IP address.  in the code there is a line
strCsubnet = Left(StrIP,10).

What does this line do?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23229986
Well, say you've got
192.168.10.20
stored in StrIP.

This line will make
strCsubnet
equal to the first 10 characters of strIP, so it would become:
192.168.10

So you can then check which subnet the user is in....

Rob.
0
 

Author Comment

by:georgedschneider
ID: 23240733
I was havign an issue with te script not mapping the drive based on the subnet.  I'll give it another try when I return after the new year.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23253086
No problem.....let me know if you get stuck....
0
 

Author Comment

by:georgedschneider
ID: 23666267
Since I can't seem to get the IP address method to work I'll try to use the method based on logon server.  Is there anyway that could map a specifc drive based on computer name?
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 500 total points
ID: 23666281
Yes.  For computer name you'd use something like:

Set objNetwork = CreateObject("WScript.Network")
strComputerName = UCase(objNetwork.ComputerName)
Select Case strComputerName
   Case "PC1"
      objNetwork.MapNetworkDrive "G:", "\\server\share2"
   Case "PC2"
      objNetwork.MapNetworkDrive "F:", "\\server\share1"
End Select


Regards,

Rob.
0
 

Author Comment

by:georgedschneider
ID: 23675972
Another variable to this script is all the computers at each site will have similar mapped drives f, g, etc.  The excpetion to this is at each site a compute named scannerX where X is the location number their G drive will be differenct.  Also if you are a member of group1 the F drive will be different.  Is there a way to accompish this?
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 500 total points
ID: 23676121
Yes, there is.  Again, there are different ways of doing this stuff, but I've taken the approach where we check your site by the %LOGONSERVER% value.

What this does is first check which LOGONSERVER you've authenticated to.
Then it checks if you're a member of Group1
   If so, map F drive to fshare1, if not, map F drive to fshare2
Then it check if the computer name starts with SCANNER
   If so, it then checks the rest of the computer name after SCANNER and matches the numbers
      to map the scannershare to the G drive
   if not, then it will map the G drive to gshare1

This code process is duplicated per LOGONSERVER, so you can modify the DC server differences to suit.

Regards,

Rob.
' THIS LINE MUST BE PLACED IN THE MAIN CODE TO ALLOW THE DICTIONARY

' OBJECT TO REMAIN PERSISTENT FOR THE ISMEMBEROFGROUP FUNCTION

Dim objMemberships
 

Set objNetwork = CreateObject("WScript.Network")

Set objADSysInfo = CreateObject("ADSystemInfo")

Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)

Set objShell = CreateObject("WScript.Shell")
 

strComputerName = UCase(objNetwork.ComputerName)

strLogonServer = UCase(objShell.ExpandEnvironmentStrings("%LOGONSERVER%")\)

Select Case strLogonServer

	Case UCase("DCSERVER1")

		If IsMemberOfGroup(objUser, "Group1") = True Then

			objNetwork.MapNetworkDrive "F:", "\\server1\fshare1"

		Else

			objNetwork.MapNetworkDrive "F:", "\\server1\fshare2"

		End If

		If Left(strComputerName, 7) = "SCANNER" Then

			Select Case Mid(strComputerName, 8)

				Case "1"

					objNetwork.MapNetworkDrive "G:", "\\server1\scanner1"

				Case "2"

					objNetwork.MapNetworkDrive "G:", "\\server1\scanner2"

				Case "3"

					objNetwork.MapNetworkDrive "G:", "\\server1\scanner3"

			End Select

		Else

			' Otherwise this is a scanner computer

			objNetwork.MapNetworkDrive "G:", "\\server1\gshare1"

		End If

	Case UCase("DCSERVER2")

		If IsMemberOfGroup(objUser, "Group1") = True Then

			objNetwork.MapNetworkDrive "F:", "\\server2\fshare1"

		Else

			objNetwork.MapNetworkDrive "F:", "\\server2\fshare2"

		End If

		If Left(strComputerName, 7) = "SCANNER" Then

			Select Case Mid(strComputerName, 8)

				Case "1"

					objNetwork.MapNetworkDrive "G:", "\\server2\scanner1"

				Case "2"

					objNetwork.MapNetworkDrive "G:", "\\server2\scanner2"

				Case "3"

					objNetwork.MapNetworkDrive "G:", "\\server2\scanner3"

			End Select

		Else

			' Otherwise this is a scanner computer

			objNetwork.MapNetworkDrive "G:", "\\server2\gshare1"

		End If

	Case UCase("DCSERVER3")

		If IsMemberOfGroup(objUser, "Group1") = True Then

			objNetwork.MapNetworkDrive "F:", "\\server3\fshare1"

		Else

			objNetwork.MapNetworkDrive "F:", "\\server3\fshare2"

		End If

		If Left(strComputerName, 7) = "SCANNER" Then

			Select Case Mid(strComputerName, 8)

				Case "1"

					objNetwork.MapNetworkDrive "G:", "\\server3\scanner1"

				Case "2"

					objNetwork.MapNetworkDrive "G:", "\\server3\scanner2"

				Case "3"

					objNetwork.MapNetworkDrive "G:", "\\server3\scanner3"

			End Select

		Else

			' Otherwise this is a scanner computer

			objNetwork.MapNetworkDrive "G:", "\\server3\gshare1"

		End If

	Case Else

		' No domain controller was used for authentication

End Select
 

Function IsMemberOfGroup(objADUser, strGroupCN)

	If IsEmpty(objMemberships) = True Then

		Set objMemberships = CreateObject("Scripting.Dictionary")

		objMemberships.Add LCase("ALL"), 0

		If IsNull(objADUser.MemberOf) = False Then

			If TypeName(objADUser.MemberOf) = "String" Then

				objMemberships.Add LCase(Mid(Split(objADUser.MemberOf, ",")(0), 4)), 0

			Else

				For Each strGroupName In objADUser.MemberOf

					objMemberships.Add LCase(Mid(Split(strGroupName, ",")(0), 4)), 0

				Next

			End If

		End If

	End If

	If objMemberships.Exists(LCase(strGroupCN)) = True Then

		IsMemberOfGroup = True

	Else

		IsMemberOfGroup = False

	End If

End Function

Open in new window

0
 

Author Comment

by:georgedschneider
ID: 23761867
I'll give this a shot on monday.
0
 

Author Comment

by:georgedschneider
ID: 23807686
I'm getting a syntax eror on line 11
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23811396
Whoops! There was an extra slash on the end of that line....

Try this.....

Rob.
' THIS LINE MUST BE PLACED IN THE MAIN CODE TO ALLOW THE DICTIONARY

' OBJECT TO REMAIN PERSISTENT FOR THE ISMEMBEROFGROUP FUNCTION

Dim objMemberships

 

Set objNetwork = CreateObject("WScript.Network")

Set objADSysInfo = CreateObject("ADSystemInfo")

Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)

Set objShell = CreateObject("WScript.Shell")

 

strComputerName = UCase(objNetwork.ComputerName)

strLogonServer = UCase(objShell.ExpandEnvironmentStrings("%LOGONSERVER%"))

Select Case strLogonServer

	Case UCase("DCSERVER1")

		If IsMemberOfGroup(objUser, "Group1") = True Then

			objNetwork.MapNetworkDrive "F:", "\\server1\fshare1"

		Else

			objNetwork.MapNetworkDrive "F:", "\\server1\fshare2"

		End If

		If Left(strComputerName, 7) = "SCANNER" Then

			Select Case Mid(strComputerName, 8)

				Case "1"

					objNetwork.MapNetworkDrive "G:", "\\server1\scanner1"

				Case "2"

					objNetwork.MapNetworkDrive "G:", "\\server1\scanner2"

				Case "3"

					objNetwork.MapNetworkDrive "G:", "\\server1\scanner3"

			End Select

		Else

			' Otherwise this is a scanner computer

			objNetwork.MapNetworkDrive "G:", "\\server1\gshare1"

		End If

	Case UCase("DCSERVER2")

		If IsMemberOfGroup(objUser, "Group1") = True Then

			objNetwork.MapNetworkDrive "F:", "\\server2\fshare1"

		Else

			objNetwork.MapNetworkDrive "F:", "\\server2\fshare2"

		End If

		If Left(strComputerName, 7) = "SCANNER" Then

			Select Case Mid(strComputerName, 8)

				Case "1"

					objNetwork.MapNetworkDrive "G:", "\\server2\scanner1"

				Case "2"

					objNetwork.MapNetworkDrive "G:", "\\server2\scanner2"

				Case "3"

					objNetwork.MapNetworkDrive "G:", "\\server2\scanner3"

			End Select

		Else

			' Otherwise this is a scanner computer

			objNetwork.MapNetworkDrive "G:", "\\server2\gshare1"

		End If

	Case UCase("DCSERVER3")

		If IsMemberOfGroup(objUser, "Group1") = True Then

			objNetwork.MapNetworkDrive "F:", "\\server3\fshare1"

		Else

			objNetwork.MapNetworkDrive "F:", "\\server3\fshare2"

		End If

		If Left(strComputerName, 7) = "SCANNER" Then

			Select Case Mid(strComputerName, 8)

				Case "1"

					objNetwork.MapNetworkDrive "G:", "\\server3\scanner1"

				Case "2"

					objNetwork.MapNetworkDrive "G:", "\\server3\scanner2"

				Case "3"

					objNetwork.MapNetworkDrive "G:", "\\server3\scanner3"

			End Select

		Else

			' Otherwise this is a scanner computer

			objNetwork.MapNetworkDrive "G:", "\\server3\gshare1"

		End If

	Case Else

		' No domain controller was used for authentication

End Select

 

Function IsMemberOfGroup(objADUser, strGroupCN)

	If IsEmpty(objMemberships) = True Then

		Set objMemberships = CreateObject("Scripting.Dictionary")

		objMemberships.Add LCase("ALL"), 0

		If IsNull(objADUser.MemberOf) = False Then

			If TypeName(objADUser.MemberOf) = "String" Then

				objMemberships.Add LCase(Mid(Split(objADUser.MemberOf, ",")(0), 4)), 0

			Else

				For Each strGroupName In objADUser.MemberOf

					objMemberships.Add LCase(Mid(Split(strGroupName, ",")(0), 4)), 0

				Next

			End If

		End If

	End If

	If objMemberships.Exists(LCase(strGroupCN)) = True Then

		IsMemberOfGroup = True

	Else

		IsMemberOfGroup = False

	End If

End Function

Open in new window

0
 

Author Comment

by:georgedschneider
ID: 23817449
I'm trying to break down each line of the function and what it does so I better understand it.  Any help would be most apprecaited.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 65

Expert Comment

by:RobSampson
ID: 23841682
Hi, here is the code commented.

Regards,

Rob.
' THIS LINE MUST BE PLACED IN THE MAIN CODE TO ALLOW THE DICTIONARY

' OBJECT TO REMAIN PERSISTENT FOR THE ISMEMBEROFGROUP FUNCTION

Dim objMemberships

 

' Set network and active directory objects to retrieve information

Set objNetwork = CreateObject("WScript.Network")

Set objADSysInfo = CreateObject("ADSystemInfo")

' Get active directory object for the currently logged on user

Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)

' Set shell object to retrieve information

Set objShell = CreateObject("WScript.Shell")
 

' Obtain the current computer name

strComputerName = UCase(objNetwork.ComputerName)

' Retrieve the environment variable value of LOGONSERVER

strLogonServer = UCase(objShell.ExpandEnvironmentStrings("%LOGONSERVER%"))

' See what generates a match for the LOGONSERVER value using a Select Case statement

Select Case strLogonServer

	Case UCase("DCSERVER1")

		' Call the custom IsMemberOfGroup function to see if the current user is a member of Group1

		If IsMemberOfGroup(objUser, "Group1") = True Then

			objNetwork.MapNetworkDrive "F:", "\\server1\fshare1"

		Else

			objNetwork.MapNetworkDrive "F:", "\\server1\fshare2"

		End If

		' First check if the first seven letters of the current computer are "SCANNER"

		' so that we can map specific drives

		If Left(strComputerName, 7) = "SCANNER" Then

			' Now check that the part of the computer name after SCANNER is one of the below

			Select Case Mid(strComputerName, 8)

				Case "1"

					objNetwork.MapNetworkDrive "G:", "\\server1\scanner1"

				Case "2"

					objNetwork.MapNetworkDrive "G:", "\\server1\scanner2"

				Case "3"

					objNetwork.MapNetworkDrive "G:", "\\server1\scanner3"

			End Select

		Else

			' Otherwise this is *NOT* a scanner computer

			objNetwork.MapNetworkDrive "G:", "\\server1\gshare1"

		End If

	Case UCase("DCSERVER2")

		' Call the custom IsMemberOfGroup function to see if the current user is a member of Group2

		If IsMemberOfGroup(objUser, "Group2") = True Then

			objNetwork.MapNetworkDrive "F:", "\\server2\fshare1"

		Else

			objNetwork.MapNetworkDrive "F:", "\\server2\fshare2"

		End If

		' First check if the first seven letters of the current computer are "SCANNER"

		' so that we can map specific drives

		If Left(strComputerName, 7) = "SCANNER" Then

			' Now check that the part of the computer name after SCANNER is one of the below

			Select Case Mid(strComputerName, 8)

				Case "1"

					objNetwork.MapNetworkDrive "G:", "\\server2\scanner1"

				Case "2"

					objNetwork.MapNetworkDrive "G:", "\\server2\scanner2"

				Case "3"

					objNetwork.MapNetworkDrive "G:", "\\server2\scanner3"

			End Select

		Else

			' Otherwise this is a scanner computer

			objNetwork.MapNetworkDrive "G:", "\\server2\gshare1"

		End If

	Case UCase("DCSERVER3")

		' Call the custom IsMemberOfGroup function to see if the current user is a member of Group3

		If IsMemberOfGroup(objUser, "Group3") = True Then

			objNetwork.MapNetworkDrive "F:", "\\server3\fshare1"

		Else

			objNetwork.MapNetworkDrive "F:", "\\server3\fshare2"

		End If

		' First check if the first seven letters of the current computer are "SCANNER"

		' so that we can map specific drives

		If Left(strComputerName, 7) = "SCANNER" Then

			' Now check that the part of the computer name after SCANNER is one of the below

			Select Case Mid(strComputerName, 8)

				Case "1"

					objNetwork.MapNetworkDrive "G:", "\\server3\scanner1"

				Case "2"

					objNetwork.MapNetworkDrive "G:", "\\server3\scanner2"

				Case "3"

					objNetwork.MapNetworkDrive "G:", "\\server3\scanner3"

			End Select

		Else

			' Otherwise this is a scanner computer

			objNetwork.MapNetworkDrive "G:", "\\server3\gshare1"

		End If

	Case Else

		' No domain controller was used for authentication

End Select

 

' This is the custom IsMemberOfGroup function that accepts two parameters

' 1: the bound object of the Active Directory user to check the membership of

' 2: the name of the group to look for

Function IsMemberOfGroup(objADUser, strGroupCN)

	' This function requires that objMemberships is declared globally outside of this function

	If IsEmpty(objMemberships) = True Then

		' Create a dictionary object to store the groups this user is a member of

		Set objMemberships = CreateObject("Scripting.Dictionary")

		' Add ALL so that you can use IsMemberOfGroup(objUser, "ALL") and define global settings

		objMemberships.Add LCase("ALL"), 0

		' The "Type" of objADUser.MemberOf can be either Null (no groups), String (one group only), or an array (multiple groups)

		If IsNull(objADUser.MemberOf) = False Then

			If TypeName(objADUser.MemberOf) = "String" Then

				objMemberships.Add LCase(Mid(Split(objADUser.MemberOf, ",")(0), 4)), 0

			Else

				For Each strGroupName In objADUser.MemberOf

					objMemberships.Add LCase(Mid(Split(strGroupName, ",")(0), 4)), 0

				Next

			End If

		End If

	End If

	' Now check whether the dictionary holds a match for the group being searched for, and return the result

	If objMemberships.Exists(LCase(strGroupCN)) = True Then

		IsMemberOfGroup = True

	Else

		IsMemberOfGroup = False

	End If

End Function

Open in new window

0
 

Author Comment

by:georgedschneider
ID: 23878569
Is there a way instead of inputing the computer names into the login script to check the names to have this pulled from a text file?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23904072
I'm not sure I follow.....

As a logon script, it will always run on the current computer that is logging on.  There is no need to pull computer names from a login script.....

Rob.
0
 

Author Comment

by:georgedschneider
ID: 23907492
In the script with the follwing line is run  If Left(strComputerName, 7) = "SCANNER" to check part of the computer name and if the first seven chracters is equal to Scanner it maps a different drive from what is normaly mapped.  If I hvae a group of computers that will have a different drive mapped than what is normally mapped is their a way to create a text file that contains theses computer names and for the script to read the computer nmaes int his file and if the local computer name is equal to one of the names in the file then map a different drive.  Otherwise I could have several expections.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23914226
OK, I get it now.  Do you want to map the same target location for all computer names that specified in that file, or do you want to map a different location for each different computer name?

If you want to map different locations, then I suggest that you format the text file like this:
COMPUTERA;\\server1\path1
COMPUTERB;\\server1\path2

or, if you even want to specify the drive letter:
COMPUTERA;F:;\\server1\path1
COMPUTERB;G:;\\server1\path2

Let me know which way you want to do it, and I'll re-write it a bit for you.

Regards,

Rob.
0
 

Author Comment

by:georgedschneider
ID: 23921634
In the logon script i have the following lines:
Case UCase("\\DC1")
objNetwork.MapNetworkDrive "F:", strlogonserver&"\share1"
objNetwork.MapNetworkDrive "G:", strlogonserver&"\share2"

Basically what I want to do is map a different G drive for computers in the text file.

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23924229
OK, so I take it you want to map the same target location for all computer names that specified in that file.

I'm not sure if you still want the SCANNER checks, so I've taken that out....

Try this code.

Regards,

Rob.
' THIS LINE MUST BE PLACED IN THE MAIN CODE TO ALLOW THE DICTIONARY

' OBJECT TO REMAIN PERSISTENT FOR THE ISMEMBEROFGROUP FUNCTION

Dim objMemberships
 

' Specify the text file location where computer names are in the file

' for alternate mapping of the G Drive

strComputers = "\\server1\share1\computers.txt"

strCompList = ";"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Const intForReading = 1

Set objFile = objFSO.OpenTextFile(strComputers, intForReading, False)

While Not objFile.AtEndOfStream

	strCompList = strCompList & UCase(objFile.ReadLine) & ";"

Wend

objFile.Close

 

' Set network and active directory objects to retrieve information

Set objNetwork = CreateObject("WScript.Network")

Set objADSysInfo = CreateObject("ADSystemInfo")

' Get active directory object for the currently logged on user

Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)

' Set shell object to retrieve information

Set objShell = CreateObject("WScript.Shell")
 

' Obtain the current computer name

strComputerName = UCase(objNetwork.ComputerName)

' Retrieve the environment variable value of LOGONSERVER

strLogonServer = UCase(objShell.ExpandEnvironmentStrings("%LOGONSERVER%"))

' See what generates a match for the LOGONSERVER value using a Select Case statement

Select Case strLogonServer

	Case UCase("DCSERVER1")

		' Call the custom IsMemberOfGroup function to see if the current user is a member of Group1

		If IsMemberOfGroup(objUser, "Group1") = True Then

			objNetwork.MapNetworkDrive "F:", "\\server1\fshare1"

		Else

			objNetwork.MapNetworkDrive "F:", "\\server1\fshare2"

		End If

		' First check if the computer is in the text file

		If InStr(strCompList, ";" & strComputerName & ";") > 0 Then

			objNetwork.MapNetworkDrive "G:", "\\server1\scannershare"

		Else

			' Otherwise this is *NOT* in the text file

			objNetwork.MapNetworkDrive "G:", "\\server1\gshare1"

		End If

	Case UCase("DCSERVER2")

		' Call the custom IsMemberOfGroup function to see if the current user is a member of Group2

		If IsMemberOfGroup(objUser, "Group2") = True Then

			objNetwork.MapNetworkDrive "F:", "\\server2\fshare1"

		Else

			objNetwork.MapNetworkDrive "F:", "\\server2\fshare2"

		End If

		' First check if the computer is in the text file

		If InStr(strCompList, ";" & strComputerName & ";") > 0 Then

			objNetwork.MapNetworkDrive "G:", "\\server1\scannershare"

		Else

			' Otherwise this is *NOT* in the text file

			objNetwork.MapNetworkDrive "G:", "\\server1\gshare1"

		End If

	Case UCase("DCSERVER3")

		' Call the custom IsMemberOfGroup function to see if the current user is a member of Group3

		If IsMemberOfGroup(objUser, "Group3") = True Then

			objNetwork.MapNetworkDrive "F:", "\\server3\fshare1"

		Else

			objNetwork.MapNetworkDrive "F:", "\\server3\fshare2"

		End If

		' First check if the computer is in the text file

		If InStr(strCompList, ";" & strComputerName & ";") > 0 Then

			objNetwork.MapNetworkDrive "G:", "\\server1\scannershare"

		Else

			' Otherwise this is *NOT* in the text file

			objNetwork.MapNetworkDrive "G:", "\\server1\gshare1"

		End If

	Case Else

		' No domain controller was used for authentication

End Select

 

' This is the custom IsMemberOfGroup function that accepts two parameters

' 1: the bound object of the Active Directory user to check the membership of

' 2: the name of the group to look for

Function IsMemberOfGroup(objADUser, strGroupCN)

	' This function requires that objMemberships is declared globally outside of this function

	If IsEmpty(objMemberships) = True Then

		' Create a dictionary object to store the groups this user is a member of

		Set objMemberships = CreateObject("Scripting.Dictionary")

		' Add ALL so that you can use IsMemberOfGroup(objUser, "ALL") and define global settings

		objMemberships.Add LCase("ALL"), 0

		' The "Type" of objADUser.MemberOf can be either Null (no groups), String (one group only), or an array (multiple groups)

		If IsNull(objADUser.MemberOf) = False Then

			If TypeName(objADUser.MemberOf) = "String" Then

				objMemberships.Add LCase(Mid(Split(objADUser.MemberOf, ",")(0), 4)), 0

			Else

				For Each strGroupName In objADUser.MemberOf

					objMemberships.Add LCase(Mid(Split(strGroupName, ",")(0), 4)), 0

				Next

			End If

		End If

	End If

	' Now check whether the dictionary holds a match for the group being searched for, and return the result

	If objMemberships.Exists(LCase(strGroupCN)) = True Then

		IsMemberOfGroup = True

	Else

		IsMemberOfGroup = False

	End If

End Function

Open in new window

0
 

Author Comment

by:georgedschneider
ID: 23940672
I added the follwoing code to remove all mapped drives before the select case and commented out the on error resume statement

Set colDrives = objNetwork.EnumNetworkDrives
For i = 0 to colDrives.Count-1 Step 2
    objNetwork.RemoveNetworkDrive colDrives.Item(i)
Next

I received an error on a few machines that the device is in use by an active process and cannot be disconnected.



0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23954010
Hmmmm....would you have a program on those computers that is monitoring the network drives?  Does a file open at login by some program?

Rob.
0
 

Author Comment

by:georgedschneider
ID: 23995373
The tetx file I'm creating to contain the computers names is called computer.txt and is formated with just the computr names in it.  DO i need to add anythign else to the text file just as a coma or semicolon at the end of each line?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 23995866
In the text file, you need the computer names only, one per line, nothing else, and no blank lines.  For example:

PC1
PC2
PC3

without any blank lines at the start or end.

Rob.
0
 

Author Comment

by:georgedschneider
ID: 24021532
Just a craxy thought or an alternate to using a tetx file.  Since all the workstation that are needed are located in an an sub OU called Workstation in the training sub OU in the parent OU called Corprate is there a way to utilize this for the laternate drive mapping?
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 500 total points
ID: 24024103
Yes, you can definately check which OU a computer is in.....

This is an example of that....

Regards,

Rob.
Set objNetwork = CreateObject("WScript.Network")

Set objSysInfo = CreateObject("ADSystemInfo")

strComputerDN = LCase(objSysInfo.ComputerName)

strOUPart = LCase("OU=Workstation,OU=Training,OU=Corporate")

If InStr(strComputerDN, strOUPart) > 0 Then

	MsgBox "Your computer is in " & strOUPart

	objNetwork.MapNetworkDrive "F:", "\\server1\fshare1"

Else

	MsgBox "Your computer is not in " & strOUPart

End If

Open in new window

0
 

Author Comment

by:georgedschneider
ID: 24033833
Do you think this would be a better way than using a text file with computer names since this would make it a little more dynamic without the need to manual update a text file?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24034111
Yes, anything more dynamic is always better, as you don't have to worry about updating scripts of files when computers are added or changed.  There are lots of ways to do things like this, you just need to figure out the easiest way to identify which AD object are to do what, and where they are located.  If your AD is structured well to suit your needs, then the above code is a good approach.

Regards,

Rob.
0
 

Author Comment

by:georgedschneider
ID: 24054212
When I run the following it fials to workl correctly.  InStr(strComputerDN, strOUPart) it fails to work correctly beacuse InSTR(strcomputerDN, strOUPart) = 0.  Why would this be when I ran tre script you provided correctly but when I changed the OU assignment for another purpose it produces a diffeent vlaue.
Set objNetwork = CreateObject("WScript.Network")

Set objSysInfo = CreateObject("ADSystemInfo")

strComputerDN = LCase(objSysInfo.ComputerName)

strOUPart = LCase("OU=Windows XP,OU=Workstations")

If InStr(strComputerDN, strOUPart) > 0 Then

	MsgBox "Your computer is in " & strOUPart

	objNetwork.MapNetworkDrive "F:", "\\server1\fshare1"

Else

	MsgBox "Your computer is not in " & strOUPart

End If

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24054867
Hi.  From what I can see with this
"OU=Windows XP,OU=Workstations"

you have placed the OU order in forwards order, so in ADUC you see
domain.com/Main Office/Windows XP/Workstations

However, when you query the distinguished name of an AD object, you always get the OU path in reverse order (except for the domain DC bits).  I'm not sure why it's like that, it just is....

So, if you want to reference, you would need to use
OU=Workstations,OU=Windows XP,OU=Main Office,DC=Domain,DC=Com

so if you're just look for part of that string, use
"OU=Workstations,OU=Windows XP"

Regards,

Rob.
0
 

Author Comment

by:georgedschneider
ID: 24064581
Stupid me had a space in the strOUPart = LCase("OU=Windows XP,OU=Workstations") after the comma which in the StrComputerDN Satementthere are no spaces between part of the distniguished name.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24083073
Oh yeah, it takes space literally, so if you don't have a space in the OU name, don't put it after the comma.  So did that work out?

Regards,

Rob.
0
 

Author Comment

by:georgedschneider
ID: 24091854
Here's the stange issue.  Below are two scripts that I ran the same code.  The sample script is run as it own and produces a meesage box indicating the distinguished name of my computer and correctly identifies that my computer is in the OU=windows xp,OU=workstations.

The second script which is my main login script produces a empty message box instead of the DN of my computer and incorrectly identifes that my computer is not not in the OU=windowwsxp,OU=workstations.

InSTR(strComputerDN, strOUPart) in the main login script is actualy 0 in the second script instead of 1 like the sample script which makes no sense since the code is the same.
'Sample Script

Set objNetwork = CreateObject("WScript.Network")

Set objSysInfo = CreateObject("ADSystemInfo")

strComputerDN = LCase(objSysInfo.ComputerName)

strOUPart = LCase("OU=Windows XP,OU=Workstations")

MsgBox strComputerDN

If InStr(strComputerDN, strOUPart) > 0 Then

	MsgBox "Your computer is in " & strOUPart
 

Else

	MsgBox "Your computer is not in " & strOUPart

End If
 
 
 
 

'Main Login Script

On Error Resume Next
 

Dim objMemberships
 
 

Set objNetwork = CreateObject("Wscript.Network")

Set objShell = CreateObject("WScript.Shell")

Set colDrives = objNetwork.EnumNetworkDrives

Set objADSysInfo = CreateObject("ADSystemInfo")

Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
 
 

strLogonServer = UCase(objShell.ExpandEnvironmentStrings("%LOGONSERVER%"))

strcomputername = UCase(objnetwork.computername)
 

For i = 0 to colDrives.Count-1 Step 2

	if left(colDrives.Item(i),2) <> "H:" then 

	objNetwork.RemoveNetworkDrive colDrives.Item(i), true, true

	End If

Next
 

If IsMemberOfGroup(objUser, "Group1") = True  then 

objnetwork.MapNetworkDrive "P:", "\\Server\Share"

end if
 

If IsMemberOfGroup(objUser, "Group2") = True  then 

objnetwork.MapNetworkDrive "L:", "\\Server\Share\Subdir"

end if
 

If IsMemberOfGroup(objUser, "Group3") = True  then 

objnetwork.MapNetworkDrive "Q:", "\\Server\Share"

end if
 
 

strComputerDN = LCase(objSysInfo.ComputerName)

strOUPart = LCase("OU=Windows XP,OU=Workstations")

MsgBox strComputerDN

If InStr(strComputerDN,strOUPart) > 0 Then

	MsgBox "Your computer is in " & strOUPart
 

Else

	MsgBox "Your computer is not in " & strOUPart

End If
 

Select Case strLogonServer

	Case UCase("\\DC1")

		If Left(strComputerName, 4) = "XXXX" or Left(strComputerName,4) = "YYYY" or Left(strComputerName,8) = "ZZZZZZZZ"then

		objNetwork.MapNetworkDrive "F:", strlogonserver&"\share1"

		Else

		objNetwork.MapNetworkDrive "F:", strlogonserver&"\altshare1"

		End If

		objNetwork.MapNetworkDrive "G:", strlogonserver&"\share2"

		

	

	Case UCase("\\DC2")

		objNetwork.MapNetworkDrive "F:", strlogonserver&"\share1"

		objNetwork.MapNetworkDrive "G:", strlogonserver&"\share2"
 

	Case UCase("\\DC3")

		objNetwork.MapNetworkDrive "F:", strlogonserver&"\share1"

		objNetwork.MapNetworkDrive "G:", strlogonserver&"\share2"
 

	Case UCase("\\DC4")

		objNetwork.MapNetworkDrive "F:", strlogonserver&"\share1"

		objNetwork.MapNetworkDrive "G:", strlogonserver&"\share2"
 

	Case UCase("\\DC5")

		objNetwork.MapNetworkDrive "F:", strlogonserver&"\share1"

		objNetwork.MapNetworkDrive "G:", strlogonserver&"\share2"
 

	Case UCase("\\DC6")

		objNetwork.MapNetworkDrive "F:", strlogonserver&"\share1"

		objNetwork.MapNetworkDrive "G:", strlogonserver&"\share2"
 

	Case UCase("\\DC7")

		objNetwork.MapNetworkDrive "F:", strlogonserver&"\share1"

		objNetwork.MapNetworkDrive "G:", strlogonserver&"\share2"

	

	Case UCase("\\DC8")

		objNetwork.MapNetworkDrive "F:", strlogonserver&"\share1"

		objNetwork.MapNetworkDrive "G:", strlogonserver&"\share2"
 

	Case UCase("\\DC9")

		objNetwork.MapNetworkDrive "F:", strlogonserver&"\share1"

		objNetwork.MapNetworkDrive "G:", strlogonserver&"\share2"
 

	Case Else

		'No domain controller was used for authentication

End Select
 

Function IsMemberOfGroup(objADUser, strGroupCN)

	If IsEmpty(objMemberships) = True Then

		Set objMemberships = CreateObject("Scripting.Dictionary")

		objMemberships.Add LCase("ALL"), 0

		If IsNull(objADUser.MemberOf) = False Then

			If TypeName(objADUser.MemberOf) = "String" Then

				objMemberships.Add LCase(Mid(Split(objADUser.MemberOf, ",")(0), 4)), 0

			Else

				For Each strGroupName In objADUser.MemberOf

					objMemberships.Add LCase(Mid(Split(strGroupName, ",")(0), 4)), 0

				Next

			End If

		End If

	End If

	If objMemberships.Exists(LCase(strGroupCN)) = True Then

		IsMemberOfGroup = True

	Else

		IsMemberOfGroup = False

	End If

End Function

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24093012
Wow!  Odd.....so in the main script, where you have this:
strComputerDN = LCase(objSysInfo.ComputerName)
strOUPart = LCase("OU=Windows XP,OU=Workstations")
MsgBox strComputerDN

If you change
MsgBox strComputerDN

To
MsgBox "The result of comparing"  & VbCrLf & strComputerDN & vbCrf & "with" & vbCrLF & strOUPart & vbCrLF & "is" & InStr(strComputerDN, strOUPart)

you will see the two strings, and the result of the InStr test....

Regards,

Rob.
0
 

Author Comment

by:georgedschneider
ID: 24098126
Attached is a screen shot of the message box that is produced.  Is still displays a balnk for the computer name and 0 for the Instr.

I find this to be very starnge since the code is the same.
error.doc
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24102703
OH! I see the problem!

Change this
strComputerDN = LCase(objSysInfo.ComputerName)

to this
strComputerDN = LCase(objADSysInfo.ComputerName)

The difference is, that in the first script, you have this
Set objSysInfo = CreateObject("ADSystemInfo")

but in the second, you have this
Set objADSysInfo = CreateObject("ADSystemInfo")

Notice the "object" after they Set keyword is different.  This means that if you have that wront when trying to use
strComputerDN = LCase(objSysInfo.ComputerName)

strComputerDN will be empty, hence why you see

The result of comparing
with
ou=windows xp,ou=workstations
is0

instead of

The result of comparing
cn=mycomputer,ou=windows xp,ou=workstations,dc=domain,dc=com
with
ou=windows xp,ou=workstations
is0

We would have been alerted to the fact that the "object" doesn't exist, if we had On Error Resume Next commented out.

Regards,

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24143335
Hey thanks for the grade.  I hope you found one or more of these techniques useful.

Regards,

Rob.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

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