CHSCLM
asked on
Vbscript multiple groups not working, not working at all
I have the following logon script which worked ish using the if statement but not for mutilpe groups once it hit the first true statement it finished but then i read i shoudl be using select case but i cant get this to work at all, i'm new to vbs script which you'll probably see, attached code
Const information_systems = "cn=information systems"
Const accounts = "cn=accounts"
Const chs_cleared_access = "cn=chs cleared access"
Const north = "cn=north"
Const south = "cn=south"
Const north_east = "cm=north east"
ON ERROR RESUME NEXT
Set wshNetwork = CreateObject("WScript.Network")
Set clDrives = WshNetwork.EnumNetworkDrives
For i = 0 to clDrives.Count -1 Step 2
WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
Next
wscript.sleep 300
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
strGroups = LCase(Join(CurrentUser.MemberOf))
Set objGroup = GetObject(StrGroupPath)
strGroupName = LCase(objGroup.CN)
'If InStr(strGroups, information_systems) Then
select case strGroupName
case "information systems"
wshNetwork.MapNetworkDrive "R:","\\ukchsc01ds01\datasuite"
wshNetwork.MapNetworkDrive "U:","\\Ukchsc01ds01\distributionNet"
wshNetwork.MapNetworkDrive "V:","\\Ukchsc01es01\clientapps"
wshNetwork.MapNetworkDrive "w:","\\Ukchsc01es01\serverApps"
'ElseIf InStr(strGroups, accounts) Then
Case "accounts"
wshNetwork.MapNetworkDrive "R:","\\ukchsc01ds01\datasuite"
wshNetwork.MapNetworkDrive "T:","\\ukchsc01ds01\companyaccounts"
wshnetwork.MapnetworkDrive "S:","\\ukchsc01ds01\companyadmin"
'ElseIf InStr(strGroups, chs_cleared_access) Then
Case "chs cleared access"
wshNetwork.MapNetworkDrive "R:","\\ukchsc01ds01\datasuite"
'ElseIf InStr(strGroups, north) Then
Case "north"
WshNetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4650"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hpdesignjet"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hp2840"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Stores"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Directors"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4730-office"
WshNetwork.SetDefaultPrinter "\\ukchsc01es01\hp4650"
'ElseIf InStr(strGroups, south) Then
Case "south"
WshNetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4650"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hpdesignjet"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hp2840"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Stores"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Directors"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4730-office"
WshNetwork.SetDefaultPrinter "\\ukchsc01es01\hp4730-office"
'ElseIf InStr(strGroups, north east) Then
Case "north_east"
WshNetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4650"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hpdesignjet"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hp2840"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Stores"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Directors"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4730-office"
WshNetwork.SetDefaultPrinter "\\ukchsc01es01\hp2600-Directors"
End select
Also, add an echo on your variable strGroupName. Make sure the echo represents the exact phrase you're using inside your "case" statements.
ASKER
says error line 23 cha 1?
Sorry add echo could you explain that,?
Sorry add echo could you explain that,?
so just before your select case strGroupName statement, add:
wscript.echo strGroupName
What is the error it's saying at 23 cha1?
wscript.echo strGroupName
What is the error it's saying at 23 cha1?
Is this your whole entire login script or is this only a function within your login script? On line 23 you're trying to set objgroup to a "GetObject" when you haven't defined the variable contents... are you defining that in a different sub?
ASKER
this is the whole script, i've copy and pasted from examples. do i need to remove that?
the idea of it is to map the drives and printers according to the groups and thats it.
the idea of it is to map the drives and printers according to the groups and thats it.
I'll modify the code that you've got up there, but yes it's got some loose ends. I'm heading to lunch right now, I'll see what I can do once I get back. Someone else might be able to do it sooner, so keep your eyes on this post.
Hi, you can give this a shot. CurrentUser.MemberOf, if I remember rightly, is empty if the user is not a member of any groups, a string if there is only one group, or an array if multiple groups. So I've adjusted the code for that, and also moved the mapping to a sub, to check based on each group name.
Within that sub, I can't remember if you'll to add CN= to each of the group names to check or not....
To test, above each of these lines
CheckGroup strGroup
you could add
MsgBox "Checking for " & strGroup
and you'll see how the group is defined for the user.
Regards,
Rob.
Within that sub, I can't remember if you'll to add CN= to each of the group names to check or not....
To test, above each of these lines
CheckGroup strGroup
you could add
MsgBox "Checking for " & strGroup
and you'll see how the group is defined for the user.
Regards,
Rob.
Set wshNetwork = CreateObject("WScript.Network")
Set clDrives = WshNetwork.EnumNetworkDrives
For i = 0 to clDrives.Count -1 Step 2
WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
Next
wscript.sleep 300
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
If TypeName(CurrentUser.MemberOf) = "Empty" Then
'WScript.Echo CurrentUser.CN & " is not a member of any groups."
ElseIf TypeName(CurrentUser.MemberOf) = "String" Then
strGroup = CurrentUser.MemberOf
CheckGroup strGroup
Else
For Each strGroup In CurrentUser.MemberOf
CheckGroup strGroup
Next
End If
'If InStr(strGroups, information_systems) Then
Sub CheckGroup(strGroupName)
Set wshNetwork = CreateObject("WScript.Network")
select case strGroupName
case "information systems"
wshNetwork.MapNetworkDrive "R:","\\ukchsc01ds01\datasuite"
wshNetwork.MapNetworkDrive "U:","\\Ukchsc01ds01\distributionNet"
wshNetwork.MapNetworkDrive "V:","\\Ukchsc01es01\clientapps"
wshNetwork.MapNetworkDrive "w:","\\Ukchsc01es01\serverApps"
'ElseIf InStr(strGroups, accounts) Then
Case "accounts"
wshNetwork.MapNetworkDrive "R:","\\ukchsc01ds01\datasuite"
wshNetwork.MapNetworkDrive "T:","\\ukchsc01ds01\companyaccounts"
wshnetwork.MapnetworkDrive "S:","\\ukchsc01ds01\companyadmin"
'ElseIf InStr(strGroups, chs_cleared_access) Then
Case "chs cleared access"
wshNetwork.MapNetworkDrive "R:","\\ukchsc01ds01\datasuite"
'ElseIf InStr(strGroups, north) Then
Case "north"
WshNetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4650"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hpdesignjet"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hp2840"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Stores"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Directors"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4730-office"
WshNetwork.SetDefaultPrinter "\\ukchsc01es01\hp4650"
'ElseIf InStr(strGroups, south) Then
Case "south"
WshNetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4650"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hpdesignjet"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hp2840"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Stores"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Directors"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4730-office"
WshNetwork.SetDefaultPrinter "\\ukchsc01es01\hp4730-office"
'ElseIf InStr(strGroups, north east) Then
Case "north_east"
WshNetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4650"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hpdesignjet"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hp2840"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Stores"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Directors"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4730-office"
WshNetwork.SetDefaultPrinter "\\ukchsc01es01\hp2600-Directors"
End Select
End Sub
ASKER
Thanks, i tried that out,
It didnt connect any drives i put the msgbox before the select case staement and it showed up if i put it inside the statement i.e when it should connect the drive it doesnt appear as if the script is bomming out before then?
It didnt connect any drives i put the msgbox before the select case staement and it showed up if i put it inside the statement i.e when it should connect the drive it doesnt appear as if the script is bomming out before then?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
OK, I just realised that each group is enumerated at
CN=Group Name,OU=Users,OU=TestOU,DC =domain,DC =com
so thats why the select case statement never matched anything.
So I've changed both instances of this line:
CheckGroup strGroup
to this
CheckGroup Mid(Split(strGroup, ",")(0), 4)
Which will now pass only the group name, without the CN= part, and other OU parts.
Regards,
Rob.
CN=Group Name,OU=Users,OU=TestOU,DC
so thats why the select case statement never matched anything.
So I've changed both instances of this line:
CheckGroup strGroup
to this
CheckGroup Mid(Split(strGroup, ",")(0), 4)
Which will now pass only the group name, without the CN= part, and other OU parts.
Regards,
Rob.
Set wshNetwork = CreateObject("WScript.Network")
Set clDrives = WshNetwork.EnumNetworkDrives
For i = 0 to clDrives.Count -1 Step 2
WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
Next
wscript.sleep 300
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
If TypeName(CurrentUser.MemberOf) = "Empty" Then
'WScript.Echo CurrentUser.CN & " is not a member of any groups."
ElseIf TypeName(CurrentUser.MemberOf) = "String" Then
strGroup = CurrentUser.MemberOf
CheckGroup Mid(Split(strGroup, ",")(0), 4)
Else
For Each strGroup In CurrentUser.MemberOf
CheckGroup Mid(Split(strGroup, ",")(0), 4)
Next
End If
'If InStr(strGroups, information_systems) Then
Sub CheckGroup(strGroupName)
Set wshNetwork = CreateObject("WScript.Network")
select case strGroupName
case "information systems"
wshNetwork.MapNetworkDrive "R:","\\ukchsc01ds01\datasuite"
wshNetwork.MapNetworkDrive "U:","\\Ukchsc01ds01\distributionNet"
wshNetwork.MapNetworkDrive "V:","\\Ukchsc01es01\clientapps"
wshNetwork.MapNetworkDrive "w:","\\Ukchsc01es01\serverApps"
'ElseIf InStr(strGroups, accounts) Then
Case "accounts"
wshNetwork.MapNetworkDrive "R:","\\ukchsc01ds01\datasuite"
wshNetwork.MapNetworkDrive "T:","\\ukchsc01ds01\companyaccounts"
wshnetwork.MapnetworkDrive "S:","\\ukchsc01ds01\companyadmin"
'ElseIf InStr(strGroups, chs_cleared_access) Then
Case "chs cleared access"
wshNetwork.MapNetworkDrive "R:","\\ukchsc01ds01\datasuite"
'ElseIf InStr(strGroups, north) Then
Case "north"
WshNetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4650"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hpdesignjet"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hp2840"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Stores"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Directors"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4730-office"
WshNetwork.SetDefaultPrinter "\\ukchsc01es01\hp4650"
'ElseIf InStr(strGroups, south) Then
Case "south"
WshNetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4650"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hpdesignjet"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hp2840"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Stores"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Directors"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4730-office"
WshNetwork.SetDefaultPrinter "\\ukchsc01es01\hp4730-office"
'ElseIf InStr(strGroups, north east) Then
Case "north_east"
WshNetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4650"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hpdesignjet"
wshNetwork.AddwindowsPrinterConnection "\\ukchsc01es01\hp2840"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Stores"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp2600-Directors"
wshnetwork.AddWindowsPrinterConnection "\\ukchsc01es01\hp4730-office"
WshNetwork.SetDefaultPrinter "\\ukchsc01es01\hp2600-Directors"
End Select
End Sub
ASKER
Brilliant, Thanks!
If no runtime error, does nothing map?