+ Post New Thread
Results 1 to 7 of 7
Scripts Thread, VBS to run based on AD group in Coding and Web Development; Hi, I am trying to use the below code to return an echo based on the machines AD group. When ...
  1. #1

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    11,840
    Blog Entries
    6
    Thank Post
    592
    Thanked 1,043 Times in 920 Posts
    Rep Power
    224

    VBS to run based on AD group

    Hi,

    I am trying to use the below code to return an echo based on the machines AD group. When i run it nothing at all happens. The machine is in the group listed in the file. Can anyone help please?

    Thanks

    Code:
    on error resume next
    lgCnt=oP.Count
    Do While lgCnt>0
    lgCnt=lgCnt-2
    Loop 
    Set WShell = CreateObject("WScript.Shell")
    
    
    If IsComputerMember("C204") Then
    
    Wscript.Echo "204"
    
    
    If IsComputerMember("C205") Then
    
    Wscript.Echo "205"
    
    
    'End If
    
    
    ' *****************
    ' ***	End 	***
    ' *****************
    
    
    
    Function IsMember(strGroup)
    ' Function to test for group membership,
    ' returns True If the user or computer is a member of the group.
    If IsEmpty(objGroupList) Then
    Call LoadGroups
    End If
    IsMember = objGroupList.Exists(strGroup)
    End Function
    
    Function IsComputerMember(sGroup)
    Dim oGroup
    on error resume next
    Set oGroup = GetObject("WinNT://" & strDomain & "/" & sGroup & ",group")
    IsComputerMember = CBool(oGroup.IsMember(objComputer2.ADsPath & "$"))
    Set oGroup = Nothing
    If not Err.Number = 0 Then
    'isComputerMember could not locate group
    end if
    on error goto 0
    End Function
    
    Sub LoadGroups() '------------------------------------------------
    ' Subroutine to populate dictionary object with group memberships.
    ' objUser is the user or computer object, with global scope.
    ' objGroupList is a dictionary object, with global scope.
    
    Dim arrbytGroups, j, arrstrGroupSids(), objGroup
    
    Set objGroupList = CreateObject("Scripting.Dictionary")
    objGroupList.CompareMode = vbTextCompare
    
    objUser.GetInfoEx Array("tokenGroups"), 0
    arrbytGroups = objUser.Get("tokenGroups")
    If TypeName(arrbytGroups) = "Byte()" Then
    ReDim arrstrGroupSids(0)
    arrstrGroupSids(0) = OctetToHexStr(arrbytGroups)
    Set objGroup = GetObject("LDAP://<SID=" & arrstrGroupSids(0) _
    & ">")
    objGroupList(objGroup.sAMAccountName) = True
    Set objGroup = Nothing
    Exit Sub
    End If
    If UBound(arrbytGroups) = -1 Then
    Exit Sub
    End If
    ReDim arrstrGroupSids(UBound(arrbytGroups))
    For j = 0 To UBound(arrbytGroups)
    arrstrGroupSids(j) = OctetToHexStr(arrbytGroups(j))
    Set objGroup = GetObject("LDAP://<SID=" & arrstrGroupSids(j) _
    & ">")
    objGroupList(objGroup.sAMAccountName) = True
    Next
    Set objGroup = Nothing
    End Sub '------------------------------------------------------------

  2. IDG Tech News

  3. #2

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    11,840
    Blog Entries
    6
    Thank Post
    592
    Thanked 1,043 Times in 920 Posts
    Rep Power
    224
    bump, anyone please?

  4. #3

    Join Date
    Jan 2006
    Location
    Surburbia
    Posts
    2,039
    Thank Post
    69
    Thanked 275 Times in 218 Posts
    Rep Power
    97
    If you're going to keep using scripts then get yourself on some scripting training. I don't do VBS but at a glance that looks like an excerpt from some bigger script and I *think* half of it is redundant (the group sid caching) and objComputer2 doesn't reference anything, so it wouldn't work.

  5. #4
    browolf's Avatar
    Join Date
    Jun 2005
    Location
    Mars
    Posts
    1,392
    Blog Entries
    41
    Thank Post
    77
    Thanked 77 Times in 64 Posts
    Rep Power
    33
    Quote Originally Posted by FN-GM View Post
    bump, anyone please?
    you need to comment out "on error resume next" in order to fix all the errors, otherwise it just ignores them and you think you don't have any you don't need the 2nd instance of that either.

  6. #5

    jinnantonnixx's Avatar
    Join Date
    Mar 2011
    Location
    In the Calamatorium.
    Posts
    895
    Thank Post
    38
    Thanked 171 Times in 128 Posts
    Rep Power
    111
    Try using messagebox instead of echos.

    Next, put messageboxes announcing themselves as the program proceeds as a kind of primitive debugger. If it's a startup script, you can write logs to the registry.


    It's not clear what you want to do, but I wonder if you can do the task by filtering GPOs by group membership?

    For example, machines in group '204' will run a certain script, machines in group '205' will run another if you implement policy filtering.

    How to Implement Group Policy Security Filtering
    Last edited by jinnantonnixx; 6th February 2012 at 05:11 PM.

  7. #6

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    7,027
    Thank Post
    1,088
    Thanked 641 Times in 598 Posts
    Rep Power
    209
    Your "If ... Else" statements naffed up


    If IsComputerMember("C204") Then Wscript.Echo "204" If IsComputerMember("C205") Then Wscript.Echo "205" 'End If

    Your End If was commented out and depending on how you were doing your If statement you were missing some lines of code either Else or End If

    Personally I would of used Select Case something like below

    Select Case IsComputerMember()
    Case "C204":
    MsgBox "C204"
    Case "C205":
    MsgBox "C205"
    Case Else:
    MsgBox "An error occured"
    End Select

  8. #7

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    11,840
    Blog Entries
    6
    Thank Post
    592
    Thanked 1,043 Times in 920 Posts
    Rep Power
    224
    Quote Originally Posted by browolf View Post
    you need to comment out "on error resume next" in order to fix all the errors, otherwise it just ignores them and you think you don't have any you don't need the 2nd instance of that either.
    i have tried removing it, still nothing happens

    Quote Originally Posted by jinnantonnixx View Post
    Try using messagebox instead of echos.

    Next, put messageboxes announcing themselves as the program proceeds as a kind of primitive debugger. If it's a startup script, you can write logs to the registry.


    It's not clear what you want to do, but I wonder if you can do the task by filtering GPOs by group membership?

    For example, machines in group '204' will run a certain script, machines in group '205' will run another if you implement policy filtering.

    How to Implement Group Policy Security Filtering
    I currently filter GPO but for a few reasons i want to move away from this.

    As for everyone else thanks for the comments i will have another dabble. I might see if i can find something better in powershell.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 18
    Last Post: 4th December 2011, 11:44 PM
  2. Script to run based on computer name
    By FN-GM in forum Scripts
    Replies: 11
    Last Post: 16th February 2011, 10:09 AM
  3. Replies: 9
    Last Post: 6th October 2010, 03:54 PM
  4. VBS - wshell.run based on file contents?
    By ahuxham in forum Scripts
    Replies: 14
    Last Post: 17th November 2009, 04:10 PM
  5. ISA 2004 - Upstream proxy based on user group?
    By Ravening_Wolf in forum Networks
    Replies: 0
    Last Post: 11th December 2006, 02:48 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •