使用AutoIt将组用户置于计算机中

时间:2014-08-08 18:20:42

标签: wmi autoit user-accounts wmi-query

我正在尝试将所有群组用户都放入计算机中。如果我手动执行此操作,我的方法是转到计算机管理以获取本地用户和组的列表,然后从那里,我可以获得用户和组的列表。

enter image description here

这是我的代码,我使用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

但是,没有返回任何输出。我的查询是否正确?

1 个答案:

答案 0 :(得分:1)

试试这个:"Computer Info UDF"

另外,我找到了这个旧代码片段。 (未经测试!)

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