Need some quick help with my vbs active directory login script

Code:
 'On Error Resume Next
arrGroups = CurrentUser.memberOf
If (Err.Number <> 0) Then
On Error GoTo 0
strGroups = ""
Else
On Error GoTo 0
strGroup = LCase(Join(arrGroups))
End If

That's your problem - you're trying to Join a string. If they're not part of a group you shouldn't really be proceeding with the code.
 
That's your problem - you're trying to Join a string. If they're not part of a group you shouldn't really be proceeding with the code.

Do you mean if the user is not part of a group?? well I have tested these users that are part of a group, the users who its not working with are members of two groups "domain users" which apparently does not count, and their divisional
group e.g. "Sales".

Whats the best way around this problem?? I really dont want to revert back to using batch files :(
 
arrGroups = currentUser.memberOf

Should return an array of strings representing each of the users groups. If it doesn't you won't be able to Join it - Type Mismatch :). You can Join elements of "" strings if you'd like but you can't join a String. At the very least the method should be be returning "CN=Sales" if it's not you need to establish why.

If you want to bypass the error using the code you've got now, which I wouldn't advise, you could

ReDim strGroups(1)
strGroups(0) = ""

That'll get you past the error but you'll still be stuck when it's comes to mapping unless you set up another condition which simply maps a drive accessible to everyone.
 
Last edited:
arrGroups = currentUser.memberOf

Should return an array representing each of the users groups. If it doesn't you won't be able to Join it - Type Mismatch :). You can Join elements of "" strings if you'd like but you can't join a String. At the very least the method should be be returning "CN=Sales" if it's not you need to establish why.

If you want to bypass the error using the code you've got now, which I wouldn't advise, you could

ReDim strGroups(1)
strGroups(0) = ""

That'll get you past the error but you'll still be stuck when it's comes to mapping unless you set up another condition which simply maps a drive accessible to everyone.


Thanks for the info :) however im a little lost with this one,lol, do you have an example of how the array should look and how the groups should be added?
 
Thanks for the info :) however im a little lost with this one,lol, do you have an example of how the array should look

How the array looks will depend on what's returned by memberOf, different users will have differing numbers of groups e.g. I am part of 31 AD groups.

As an example for a 2 group user called Clark Kent, the array would contain strings like these

CN=Superheroes,CN=Users,DC=DailyPlanet,DC=uk
CN=Users,CN=Users,DC=DailyPlanet,DC=uk

Try running code like
Code:
arrGroups = currentUser.memberOf
 
For each sGroup in arrGroups
myOutputFile.Writeline sGroup ' Put your preferred destination here. 
Next

That should give you an idea.

how the groups should be added?

There isn't really a reason for you to be adding anything to the array.
 
Im still having trouble exactly how this was supposed to work, sorry for this but coming from someone who has little experience with VBScript, and infact using arrays's im finding this hard :(

where exactly should

Code:
CN=Superheroes,CN=Users,DC=DailyPlanet,DC=uk
CN=Users,CN=Users,DC=DailyPlanet,DC=uk

be placed in my code?

also why is there two CN's in that, and 2x DC's?

If im coding this for mine, should it look like:

Code:
CN=Support, CN=Users,DC=Domain Controller, DC=?????" <What is that last bit?

do this for each group?


I also dont understand what this piece of code is supposed to do

Code:
arrGroups = currentUser.memberOf
 
For each sGroup in arrGroups
myOutputFile.Writeline sGroup ' Put your preferred destination here. 
Next

Outputs a file with what info exactly?

Sorry if im a pain :(
 
Im still having trouble exactly how this was supposed to work, sorry for this but coming from someone who has little experience with VBScript, and infact using arrays's im finding this hard :(

I'm sorry, from the way you worded your original post it seemed you knew VB but just not how to do with this particular scenario. If I'd known I would have elaborated a bit more in my previous answers.

http://www.msdn.net

Is a great place to start. You can find pretty much anything there with example code too. Also, it might be beneficial for you to search for some VB tutorials on the net to get a feel for how certain types of data and ways of storing it (arrays, collections and enumerations) are handled.

CN=Superheroes,CN=Users,DC=DailyPlanet,DC=uk
CN=Users,CN=Users,DC=DailyPlanet,DC=uk

The above is not code, sorry I should have been clearer. It's an example of two strings the memberOf method could return when you call it.

Let's pretend there's a method called GetCars which returns an array of cars people own stored on someone's database.

Eg.
arrayOfMyCars = GetCars

Then

arrayOfmyCars(0) would be "CAR=AUDI, MODEL=A3, REG=DY06GHA"
arrayOfmyCars(1) would be "CAR=FORD, MODEL=KA, REG=GY74RED"

and so on.

If the output was joined, like you've done in your script, it would be

"CAR=AUDI, MODEL=A3, REG=DY06GHA,CAR=FORD, MODEL=KA, REG=GY74RED"

Does that make sense? In your case you're simply pulling information from Active Directory, joining it together in one string and then checking to see if certain text occurs. You do not need to add to it or anything.

What you do need to do is to check that your call to memberOf is actually returning something you can use. If it's causing an error and you're ignoring it and replacing an expected type with a different one you're going to get problems ;)

Code:
 Set objNetwork = CreateObject("WScript.Network")
Set objUser = CreateObject("ADSystemInfo")
Set currentUser = GetObject("LDAP://" & objUser.UserName)
 
arrayofMyGroups = currentUser.memberOf
 
MsgBox "Listing groups for " & currentUser.Name
 
For Each sGroup In arrayofMyGroups
    MsgBox sGroup
Next

Try that instead. Using the above you'll get a mesage box for each of the groups that you're a member off and while you're there you can also see exactly what's being returned by memberOf.
 
Thanks so much for this :) ill let you know how I get on with it! that looks a lot clearer for me to understand :)
 
Hi, just returning back to this,

As you are aware when using the above script it chucks up errors if the user is a member of 0 groups as it will see this as empty, or if they are a member of 1 group it will see this a as a string therefore I have re-jigged the code as per below:

Code:
' VBScipt for Windows AD-Domain user login script with mapped network drive & mapped printer support----------'
' v1.1--------------------------------------------------------------------------------------------------------'
' dmoranda.co.uk copyright 2009-------------------------------------------------------------------------------'
' ------------------------------------------------------------------------------------------------------------'
Option Explicit

'Defines a list for the variable values
Dim wshNetwork
Dim ADSysInfo
Dim CurrentUser
Dim strGroups
Dim arrGroups

Const SUPPORT_GROUP	= "cn=support,ou=acc,ou=backoffice,dc=mydomain,dc=com"
Const FINANCE_GROUP = "cn=finance,ou=mky,ou=backoffice,dc=mydomain,dc=com"
Const PROVISIONING_GROUP = "cn=provisioning,ou=acc,ou=backoffice,dc=mydomain,dc=com"
Const SALES_GROUP = "cn=sales,ou=mky,ou=backoffice,dc=mydomain,dc=com"

Set wshNetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)



'Map Users home directory to h:\
wshNetwork.MapNetworkDrive "h:", "\\****\Users$\" & wshNetwork.UserName


On Error Resume Next
arrGroups = CurrentUser.GetEx("MemberOf")
If (Err.Number = 0) Then
On Error GoTo 0
strGroups = LCase(Join(arrGroups))

If InStr(strGroups, "SUPPORT_GROUP") Then
	wshNetwork.MapNetworkDrive "s:", "\\****\DATA"	
End If

If InStr(strGroups, "FINANCE_GROUP") Then
	wshNetwork.MapNetworkDrive "f:", "\\****\finance"
    wshNetwork.MapNetworkDrive "s:", "\\****\DATA"
End If

If InStr(strGroups, "PROVISIONING_GROUP") Then
	wshNetwork.MapNetworkDrive "s:", "\\****\DATA"	
End If

If InStr(strGroups, "SALES_GROUP") Then
	wshNetwork.MapNetworkDrive "s:", "\\****\DATA"
End If



End If
On Error GoTo 0

I have taken into account what has been said in this thread and also workarounds I have found on the web.

This hasnt been tested yet, I will be testing this afternoon hopefully but just wanted to see if I am doing it correctly. One thing I am unsure about is the following lines:

Code:
Const SUPPORT_GROUP	= "cn=support,ou=acc,ou=backoffice,dc=mydomain,dc=com"
Const FINANCE_GROUP = "cn=finance,ou=mky,ou=backoffice,dc=mydomain,dc=com"
Const PROVISIONING_GROUP = "cn=provisioning,ou=acc,ou=backoffice,dc=mydomain,dc=com"
Const SALES_GROUP = "cn=sales,ou=mky,ou=backoffice,dc=mydomain,dc=com"

How do I find out what the correct OU/OU/DC/DC? I know what the CN would be.

Also looking at the above, there was one error being thrown up, the last comment was the person managing to fix it, but all he said was

"OK, here is what I was doing wrong... I was putting as the search string
so InStr was compairing FINANCE_GROUP when it should have been looking at
cn=finance."

Does anyone know what he means by this?
 
Last edited:
How do I find out what the correct OU/OU/DC/DC? I know what the CN would be.
The are in Active Directory and will be determined by your companies naming and organization conventions.

How do I find out what the correct OU/OU/DC/DC? I know what the CN would be.

Also looking at the above, there was one error being thrown up, the last comment was the person managing to fix it, but all he said was

"OK, here is what I was doing wrong... I was putting as the search string
so InStr was compairing FINANCE_GROUP when it should have been looking at
cn=finance."
Does anyone know what he means by this?
Yes. You are comparing a String literal with a variable.

Consider the below

strName = "Nigel"
strFullName = "Nigel Blabbermouth"

InStr(strFullName, "strName")

Will return 0, let's call it False, because strName is not in strFullName ("Nigel Blabbermouth"). You are checking a literal string.

If InStr(strFullName, strName)

Will Return 1, let's call it True, because it's comparing

strName which is "Nigel" with strFullName("Nigel Blabbermouth")

Note: InStr returns the position of text within a String, 0 if not found or -1 for error. In this case it returns 0 or 1 as Nigel is at the start of our String to check.

As for the code try the below, I have only slightly amended it and have left comments so hopefully you'll be able understand the changes.

Code:
Dim ADSysInfo
Dim CurrentUser
Dim strGroups
Dim arrGroups
Dim objFso, objErrFile
 
' Constants
Const SUPPORT_GROUP    = "cn=support,ou=acc,ou=backoffice,dc=mydomain,dc=com"
Const FINANCE_GROUP = "cn=finance,ou=mky,ou=backoffice,dc=mydomain,dc=com"
Const PROVISIONING_GROUP = "cn=provisioning,ou=acc,ou=backoffice,dc=mydomain,dc=com"
Const SALES_GROUP = "cn=sales,ou=mky,ou=backoffice,dc=mydomain,dc=com"
 
'Create Objects
Set wshNetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
 
' Let's add some output objects for later on
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objErrFile = objFSO.CreateTextFile("C:\AD-Domain Script Errors.Log")
 
 
'Map Users home directory to h:\
wshNetwork.MapNetworkDrive "h:", "\\****\Users$\" & wshNetwork.UserName
 
 
On Error Resume Next
 
' Let's get our groups
arrGroups = CurrentUser.GetEx("MemberOf")
 
' Notice I have removed your code here and replaced it with a For..Else.
' Using this means we don't have to worry about CurrentUser.GetEx("MemberOf")
' returning nothing. Which means no [B]Join statement [/B]or Type mismatches because of it ;)
 
If Err = 0 Then ' Have we succeeded?
 
' Yes now lets iterate through each group individually and check it against our Constants
 
    For each group in arrGroups
 
        Select Case LCase(group) ' Make sure we're comparing the same case...
            Case SUPPORT_GROUP
                wshNetwork.MapNetworkDrive "s:", "\\****\DATA"    
            Case FINANCE_GROUP    
                wshNetwork.MapNetworkDrive "f:", "\\****\finance"
                wshNetwork.MapNetworkDrive "s:", "\\****\DATA"        
            Case PROVISIONING_GROUP
                wshNetwork.MapNetworkDrive "s:", "\\****\DATA"    
            Case SALES_GROUP
                wshNetwork.MapNetworkDrive "s:", "[URL="file://\\****\DATA"]\\****\DATA[/URL]"
            Case Else
                ' The group we're checking does not match any of the ones we have declared. Is this a problem??
                ' Now you can check if your constants are declared correctly. If you've made a typo
                ' in your declarations you will be able to see it using the log file.
                objErrFile.WriteLine "Unexpected Group: " & group
        End Select
 
    Next
Else
    Msgbox "An unexpected error has occured." & vbCrLf & "Description: " & Err.Description
 
End If
 
' Close the log file
objErrFile.Close
 
' Release objects
Set objFSO = Nothing
Set objErrFile = Nothing
Set wshNetwork = Nothing
Set ADSysInfo = Nothing
Set CurrentUser = Nothing
 
Last edited:
thanks so much for this help. I will go back over it once again tomorrow when i am in the office and will post my findings :)
 
How do I find out what the correct OU/OU/DC/DC? I know what the CN would be.

Good evening, I highly suggest you get the correct entried by using ADSIEDIT -finding the relevant object, right click > properties > go to the value named DistinguishedName and copying this

This will give you the exact OU path to use for mapping.

ADSIEDIT can be run by using adsiedit.msc from the run prompt but you may need to install it, I believe this is a freely available tool for download if you don't have it :)
 
Good evening, I highly suggest you get the correct entried by using ADSIEDIT -finding the relevant object, right click > properties > go to the value named DistinguishedName and copying this

This will give you the exact OU path to use for mapping.

ADSIEDIT can be run by using adsiedit.msc from the run prompt but you may need to install it, I believe this is a freely available tool for download if you don't have it :)

Thanks very much for this tip :) Im going to be working more on the script today in work. Will let you know the outcome.
 
Back
Top Bottom