Avatar of Jay_Jay70
Jay_Jay70
Flag for Australia asked on

Continued Question Re: Script... Chris! I'm back!

Hey Mate,

in regards to that script you helped me with last week, i need to exempt just one group from it! Is there a way to do it? Group is say    @james

Here is the code you gave me

Option Explicit

Const ADS_SCOPE_SUBTREE = 2

Dim objConnection, objCommand, objRecordSet, objRootDSE, objGroup
Dim strGroupName

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

Set objRootDSE = GetObject("LDAP://RootDSE")
objCommand.CommandText = "SELECT name, aDSPath " &_
     "FROM 'LDAP://" & objRootDSE.Get("defaultNamingContext") & "' WHERE objectClass='group'"
Set objRootDSE = Nothing

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 600
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False

Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
     On Error Resume Next
     strGroupName = objRecordSet.Fields("name")
     If Left(strGroupName, 1) = "@" Then
          Set objGroup = GetObject(objRecordSet.Fields("aDSPath"))
          'WScript.Echo strGroupName & ": Resetting msExchRequireAuthtoSendTo to True"
          Err.Clear
           objGroup.Put "msExchRequireAuthtoSendTo", True
           objGroup.SetInfo
          If Err.Number <> 0 Then
               WScript.Echo strGroupName & ": Failed; " & Err.Description
          End If
          Set objGroup = Nothing
     End If

     On Error Goto 0
     objRecordSet.MoveNext
Wend

objConnection.Close

Set objRecordSet = Nothing
Set objCommand = Nothing
Set objConnection = Nothing

Thanks again!

J
Windows Server 2003

Avatar of undefined
Last Comment
Chris Dent

8/22/2022 - Mon
Chris Dent


Hey J,

Yep, dead easy :)

Add the Group Name to the EXCLUDED_GROUP constant; it's not case sensitive or anything; @james is added as the example.


Option Explicit

Const EXCLUDED_GROUP = "@james"

Const ADS_SCOPE_SUBTREE = 2

Dim objConnection, objCommand, objRecordSet, objRootDSE, objGroup
Dim strGroupName

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

Set objRootDSE = GetObject("LDAP://RootDSE")
objCommand.CommandText = "SELECT name, aDSPath " &_
      "FROM 'LDAP://" & objRootDSE.Get("defaultNamingContext") & "' WHERE objectClass='group'"
Set objRootDSE = Nothing

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 600
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False

Set objRecordSet = objCommand.Execute
      
While Not objRecordSet.EOF
      On Error Resume Next
      strGroupName = objRecordSet.Fields("name")
      If Left(strGroupName, 1) = "@" And LCase(strGroupName) <> LCase(EXCLUDED_GROUP) Then
            Set objGroup = GetObject(objRecordSet.Fields("aDSPath"))
            'WScript.Echo strGroupName & ": Resetting msExchRequireAuthtoSendTo to True"
            Err.Clear
            objGroup.Put "msExchRequireAuthtoSendTo", True
            objGroup.SetInfo
            If Err.Number <> 0 Then
                  WScript.Echo strGroupName & ": Failed; " & Err.Description
            End If
            Set objGroup = Nothing
      End If

      On Error Goto 0
      objRecordSet.MoveNext
Wend

objConnection.Close

Set objRecordSet = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Jay_Jay70

ASKER
Thanks mate, for multiple groups, cane i add like this   "@james, @chris"

is it that syntax?
Chris Dent


Nope; that one doesn't at all. This one does though. All you need to do is add the groups you want to avoid into this:

arrExcludedGroups = Array("@james", "@chris")

Each should be enclosed in quotes, and the list is comma seperated. You can just have one in there if you like; but don't remove the "Array(" bit or it will get upset ;)

Chris



Option Explicit

Dim arrExcludedGroups
arrExcludedGroups = Array("@james", "@chris")

Const ADS_SCOPE_SUBTREE = 2

Dim objConnection, objCommand, objRecordSet, objRootDSE, objGroup
Dim strGroupName, strGroup
Dim booResetAuth

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

Set objRootDSE = GetObject("LDAP://RootDSE")
objCommand.CommandText = "SELECT name, aDSPath " &_
      "FROM 'LDAP://" & objRootDSE.Get("defaultNamingContext") & "' WHERE objectClass='group'"
Set objRootDSE = Nothing

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 600
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False

Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
      On Error Resume Next
      strGroupName = objRecordSet.Fields("name")
      If Left(strGroupName, 1) = "@" Then

            booResetAuth = True
            For Each strGroup in arrExcludedGroups
                  If LCase(strGroupName) = LCase(strGroup) Then
                        booResetAuth = False
                  End If
            Next

            Set objGroup = GetObject(objRecordSet.Fields("aDSPath"))
            'WScript.Echo strGroupName & ": Resetting msExchRequireAuthtoSendTo to True"
            Err.Clear
            objGroup.Put "msExchRequireAuthtoSendTo", True
            objGroup.SetInfo
            If Err.Number <> 0 Then
                  WScript.Echo strGroupName & ": Failed; " & Err.Description
            End If
            Set objGroup = Nothing
      End If

      On Error Goto 0
      objRecordSet.MoveNext
Wend

objConnection.Close

Set objRecordSet = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Chris Dent

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Jay_Jay70

ASKER
Champion! Thanks very much for your help - works perfect, and another happy client :)
Chris Dent


Pleasure :)

Chris