Sign in to follow this  
Followers 0
ReitzelTechnology

Run Script if Computer is a member of group in AD

7 posts in this topic

I have a script to install Macromedia Studio on our machines and I have a group in Active Directory called "Studio8_Stations". I would like to be able for the script to check to see if it is a member of that group before it runs the installer script.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Try this:

#include "AD.au3"

Global $aMembers[1], $computer = "Computer10", $group = "Studio8_Stations", $found = False, $i

; Open Connection to the Active Directory
_AD_Open()
If @error Then Exit MsgBox(16, "Active Directory Example Skript", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)


; Get a sorted list of members for the first group the currently logged on user is a member of
$aMembers = _AD_GetGroupMembers($group)

If @error > 0 Then
    MsgBox(64, "Active Directory Functions - Example 1", "The group '" & $group & "' has no members")
Else
    For $i = 1 To $aMembers[0]
        If StringInStr($aMembers[$i], "CN=" & $computer & ",") Then
            MsgBox(64, "Information", $computer & " is a member of " & $group)
            $found = True
            ExitLoop
        EndIf
    Next
    If Not $found Then MsgBox(16, "Information", $computer & " is NOT a member of " & $group)
EndIf


; Close Connection to the Active Directory
_AD_Close()

You need made by water!

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Or even easier and faster:

#include <AD.au3>
Global $iResult, $sGroup = "Studio8_Stations"
; Open Connection to the Active Directory
_AD_Open()
If @error Then Exit MsgBox(16, "Active Directory", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)
; Check if the computer is an immediate member of the group
If _AD_IsMemberOf($sGroup, @ComputerName & "$") = 1 Then
    ; Computer is member of the group - do whatever you need to do
EndIf
; Close Connection to the Active Directory
_AD_Close()
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hi.

"Run Script if Computer is a member of group in AD" was the question?

So it's (maybe)

If _AD_IsMemberOf($sGroup, @computername) = 1

This is a question, as I'm not sure, *IF* the AD computer account name will always match the local @computername...

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

If you pass the computername as SamAccountName you have to append a dollar sign e.g. @ComputerName & "$"


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Water - Are you sure this is working? I can never get your script in Post 21 to find the computer in the group even though I know it is. Thanks for any help you can provide.

Share this post


Link to post
Share on other sites

Yes, I am.

I just checked it again and it works.

Can you post the code you use?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0