Site Maintenance this evening The site will be offline for hosting changes. Time TBC

+ 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
    15,935
    Thank Post
    886
    Thanked 1,692 Times in 1,471 Posts
    Blog Entries
    12
    Rep Power
    446

    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. #2

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    15,935
    Thank Post
    886
    Thanked 1,692 Times in 1,471 Posts
    Blog Entries
    12
    Rep Power
    446
    bump, anyone please?

  3. #3

    Join Date
    Jan 2006
    Location
    Surburbia
    Posts
    2,178
    Thank Post
    74
    Thanked 307 Times in 243 Posts
    Rep Power
    115
    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.

  4. #4
    browolf's Avatar
    Join Date
    Jun 2005
    Location
    Mars
    Posts
    1,525
    Thank Post
    106
    Thanked 89 Times in 75 Posts
    Blog Entries
    46
    Rep Power
    40
    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.

  5. #5

    jinnantonnixx's Avatar
    Join Date
    Mar 2011
    Location
    In the Calamatorium.
    Posts
    1,968
    Thank Post
    112
    Thanked 489 Times in 335 Posts
    Blog Entries
    2
    Rep Power
    283
    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 04:11 PM.

  6. #6

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,754
    Thank Post
    3,264
    Thanked 1,051 Times in 972 Posts
    Rep Power
    364
    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

  7. #7

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    15,935
    Thank Post
    886
    Thanked 1,692 Times in 1,471 Posts
    Blog Entries
    12
    Rep Power
    446
    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. Script to run based on computer name
    By FN-GM in forum Scripts
    Replies: 12
    Last Post: 10th July 2013, 11:39 AM
  2. Replies: 18
    Last Post: 4th December 2011, 10:44 PM
  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, 03:10 PM
  5. ISA 2004 - Upstream proxy based on user group?
    By Ravening_Wolf in forum Wireless Networks
    Replies: 0
    Last Post: 11th December 2006, 01: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
  •