Karla Mae Z. Jaro
Karla Mae Z. Jaro

Reputation: 71

Getting the group users in computer using AutoIt

I am trying to get all the group users in a computer. If I do this manually, my way is to go to Computer Management to get the list of Local Users and Group, and from there, I can get the list of Users and Group.

enter image description here

This is my code and I use AutoIt:

Func User()
Local $objWMIService, $colSettings, $objComputer, $strComputer = "."

;# Initiate the object
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")

;# Check if it's an object
If IsObj($objWMIService) Then

    ;# Search for PC Infomration
    $colSettings = $objWMIService.ExecQuery("Select * from Win32_GroupUser")

    If IsObj($colSettings) Then
        For $objComputer In $colSettings
            If $objComputer.AccountType <> '' Then
                Return MsgBox(0, "RETURN", "AccountType: " & $objComputer.AccountType & @CRLF & "Full Name: " & $objComputer.FullName & @CRLF & "Caption: " & $objComputer.Caption & @CRLF & "Name: " & $objComputer.Name)
            EndIf
        Next
    Else
        MsgBox(0, "RETURN", $colSettings & " IS NOT AN OBJ")
    EndIf
Else
    MsgBox(0, "RETURN", $objWMIService & " IS NOT AN OBJ")
EndIf

EndFunc   ;==>User

However, no output is being returned. Is my query correct at all?

Upvotes: 0

Views: 2414

Answers (1)

Xenobiologist
Xenobiologist

Reputation: 2151

Try this : "Computer Info UDF"

Also, I found this old snippet. (Not tested!)

Dim $InGroup

$oMyError = ObjEvent("AutoIt.Error", "ComError")


If UserInGroup(@LogonDomain, @UserName, "Administrator") Then
    MsgBox(0, "Validate", @LogonDomain & "/" & @UserName & " : User in your groupname " & $InGroup)
Else
    MsgBox(0, "Validate", @LogonDomain & "/" & @UserName & " : User NOT in your groupname")
EndIf

Exit
; Check if User is in a group
Func UserInGroup($Domain, $UserName, $InGroup)
    ;local $sRet
    Local $objUser = ObjGet("WinNT://" & $Domain & "/" & $UserName)
    For $oGroup In $objUser.Groups
        If $oGroup.Name = $InGroup Then Return 1
    Next
    Return 0
EndFunc   ;==>UserInGroup

;COM Error function
Func ComError()
    If IsObj($oMyError) Then
        $HexNumber = Hex($oMyError.number, 8)
        SetError($HexNumber)
    Else
        SetError(1)
    EndIf
    Return 0
EndFunc   ;==>ComError
#cs
    ; Generated by AutoIt Scriptomatic

    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""
    $strComputer = "localhost"

    $Output=""
    $Output = $Output & "Computer: " & $strComputer  & @CRLF
    $Output = $Output & "==========================================" & @CRLF
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_GroupUser", "WQL", _
    $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) then
    For $objItem In $colItems
    $Output = $Output & "GroupComponent: " & $objItem.GroupComponent & @CRLF
    $Output = $Output & "PartComponent: " & $objItem.PartComponent & @CRLF
    if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
    $Output=""
    Next
    Else
    Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_GroupUser" )
    Endif
#ce

#include <Constants.au3> ; required for StdoutRead

; populate $groupstring with the output of net user /domain
; remove the /domain if you are just interested in local machine groups
$foo = Run(@ComSpec & " /c net user " & @UserName & " /domain", @SystemDir, @SW_HIDE, $STDOUT_CHILD)
$groupstring = ""
While 1
    $groupstring &= StdoutRead($foo)
    If @error = -1 Then ExitLoop
WEnd

Func ingroup($which)
    If $which = "*" Then Return 1
    $which = StringLeft($which, 21) ; net user /domain returns only the first 21 chars of each group
    $which = "*" & $which
    If StringInStr($groupstring, $which) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc   ;==>ingroup

;example usage
If ingroup("Domain Admins") Then
    $admin = True
Else
    $admin = False
EndIf

Upvotes: 1

Related Questions