Jump to content

Get localgroup members


skysel
 Share

Recommended Posts

Hello again AutoIt people :P

I'm having troubles on how to create a script, that would check a list (txt file) of computers for users of localgroup "Administrators".

VBS script for checking members of localogrup administrators:

Set localGroup = GetObject("WinNT://./Administrators")

For Each member In localGroup.Members
    Wscript.Echo member.name
Next

Is it possible to do this using ADFunctions?

Link to comment
Share on other sites

@skysel

Maybe this can get you going :

#include <array.au3>

; Initialize error handler 
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")

$GroupPath = InputBox("List Group Members", "Enter DOMAIN/GROUP to list: ","DOMAIN/GROUP")

$avUsers = _GroupGetMembers($GroupPath)
If Not @error Then
    _ArrayDisplay($avUsers, "Users in group")
Else
    MsgBox(16, "Error", "Error returned:  @error = " & @error)
EndIf


; ------------------------------------------------------------
; Function _GroupGetMembers($sPath)
;   Call with:    _GroupGetMembers($sPath)
;     Where:    $sPath = DOMAIN/GROUP, i.e. "MyDomain/Domain Admins"
;       If Domain is not included, uses local machine, i.e. "Administrators" will be xlated to "./Administrators"
;   On success returns an array of members in the specified group with [0] = count.
;     For an empty group, returns [0] = 0.
;   On failure sets @error.
; ------------------------------------------------------------
Func _GroupGetMembers($sPath)
    Local $oUser, $sRET = "", $avRET
    
    ; Check path to group
    $sPath = StringReplace($sPath, "\", "/") ; Don't use backslash with WMI path
    If Not StringInStr($sPath, "/") Then $sPath = "./" & $sPath ; Use local if no domain given
    
    ; Get group object
    Local $oGroup = ObjGet("WinNT://" & $sPath)
    If IsObj($oGroup) Then
        For $oUser In $oGroup.Members
           $sRET &= $oUser.Name  & @LF
           ; $sRET &= $oUser.Name & "," & $oUser.mail & "," & $oUser.telephoneNumber & "," & $oUser.facsimileTelephoneNumber & @LF
        Next
        
        ; Split into an array for return
        $avRET = StringSplit(StringStripWS($sRET, 2), @LF)
        ; Change result for empty group [0] = 0
        If $avRET[1] = "" Then Local $avRET[1] = [0]
        Return $avRET
    Else
        ; Error getting group object
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>_GroupGetMembers

Func MyErrFunc()
  $HexNumber=hex($oMyError.number,8)
  Msgbox(0,"COM Error Test","We intercepted a COM Error !"       & @CRLF  & @CRLF & _
             "err.description is: "    & @TAB & $oMyError.description    & @CRLF & _
             "err.windescription:"     & @TAB & $oMyError.windescription & @CRLF & _
             "err.number is: "         & @TAB & $HexNumber              & @CRLF & _
             "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
             "err.scriptline is: "     & @TAB & $oMyError.scriptline     & @CRLF & _
             "err.source is: "         & @TAB & $oMyError.source         & @CRLF & _
             "err.helpfile is: "       & @TAB & $oMyError.helpfile       & @CRLF & _
             "err.helpcontext is: "    & @TAB & $oMyError.helpcontext _
            )
  SetError(1)  ; to check for after this function returns
Endfunc

Regards

ptrex

Link to comment
Share on other sites

Thanks for the help guys, however I will still require some help... I've been working on following issues past few days, and I've got no success:

- If a computer name is not found on the network, script should continue executing and not abort

- When script returns names of Administrators group members, those names should be appended to each computer name..

I modified a script a bit, so it's checking computer names from a file (200+ computers)..

test2.txt is the output file with group members, without computer names along.. list-text.txt is list of computers (e.g. COMPUTERNAME\Administrators).

Below is the script:

#include <array.au3>
#include <File.au3>
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")
$sfile = "C:\test2.txt"

$GroupPath = FileReadLine("C:\list-test.txt")

$avUsers = _GroupGetMembers($GroupPath)
If Not @error Then
    _FileWriteFromArray($sfile, $avUsers,1)
Else
    MsgBox(16, "Error", "Error returned:  @error = " & @error)  
EndIf


Func _GroupGetMembers($sPath)
    Local $oUser, $sRET = "", $avRET
   
    ; Check path to group
    $sPath = StringReplace($sPath, "\", "/") ; Don't use backslash with WMI path
    If Not StringInStr($sPath, "/") Then $sPath = "./" & $sPath ; Use local if no domain given
   
    ; Get group object
    Local $oGroup = ObjGet("WinNT://" & $sPath)
    If IsObj($oGroup) Then
        For $oUser In $oGroup.Members
           $sRET &= $oUser.Name & @LF
        Next
       
        $avRET = StringSplit(StringStripWS($sRET, 2), @LF)
        If $avRET[1] = "" Then Local $avRET[1] = [0]
        Return $avRET
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc

Func MyErrFunc()
 $HexNumber=hex($oMyError.number,8)
 Msgbox(0,"COM Error Test","We intercepted a COM Error !"       & @CRLF  & @CRLF & _
            "err.description is: "    & @TAB & $oMyError.description    & @CRLF & _
            "err.windescription:"     & @TAB & $oMyError.windescription & @CRLF & _
            "err.number is: "         & @TAB & $HexNumber              & @CRLF & _
            "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
            "err.scriptline is: "     & @TAB & $oMyError.scriptline     & @CRLF & _
            "err.source is: "         & @TAB & $oMyError.source         & @CRLF & _
             "err.helpfile is: "       & @TAB & $oMyError.helpfile       & @CRLF & _
             "err.helpcontext is: "    & @TAB & $oMyError.helpcontext _
            )
  SetError(1)  ; to check for after this function returns
Endfunc
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...