Jump to content

Recommended Posts

Posted (edited)

Hi

i try to read existing local Groups of remote computer and add to Combobox so i can select a Group to

get members and add to $ListMembers. Does anyone have a solution how i can read existing local Groups of remote computer and add to Combobox?

 

thanks.

 

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.1
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>


$Form1 = GUICreate("Add / Remove Members", 615, 432, 196, 128)
GUISetBkColor(0xFFFFFF)
$Group1 = GUICtrlCreateGroup("", 8, 8, 289, 185)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
$LabelCpname = GUICtrlCreateLabel("CP Name:", 16, 32, 81, 23)
GUICtrlSetFont(-1, 12, 800, 0, "Arial")
$InputCpname = GUICtrlCreateInput("", 16, 56, 129, 30)
GUICtrlSetFont(-1, 14, 800, 0, "Arial")
$LabelLocalGroups = GUICtrlCreateLabel("Local Groups", 16, 96, 107, 20)
GUICtrlSetFont(-1, 12, 800, 0, "Arial")
$ComboGroups = GUICtrlCreateCombo("", 16, 152, 273, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetFont(-1, 14, 800, 0, "Arial")
GUICtrlSetBkColor(-1, 0xFFFF00)
$ButtonReadGroups = GUICtrlCreateButton("read local Groups", 160, 56, 130, 25)
$ButtonReadmembers = GUICtrlCreateButton("read Members", 16, 120, 130, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("User", 8, 200, 289, 89)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
$LabelUserID = GUICtrlCreateLabel("User ID:", 16, 224, 66, 23)
GUICtrlSetFont(-1, 12, 800, 0, "Arial")
$InputComputer = GUICtrlCreateInput("", 88, 216, 89, 30)
GUICtrlSetFont(-1, 14, 800, 0, "Arial")
$ButtonAdd = GUICtrlCreateButton("Add User", 184, 216, 100, 25)
$ButtonRemove = GUICtrlCreateButton("Remove user", 184, 248, 100, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$ListMembers = GUICtrlCreateList("", 304, 16, 305, 409)
GUISetState(@SW_SHOW)


While 1
    $msg = GUIGetMsg()
    Select
        ;Case $GUI_EVENT_CLOSE
             Case $msg = $GUI_EVENT_CLOSE
            Exitloop

         Case $msg = $ButtonReadGroups
            MsgBox(0, "Note", "Button 'read Groups' not work !")


         Case $msg = $ButtonReadmembers
            ;MsgBox(0, "Note", "Button 'Read Members' not work !")

          GUICtrlSetData($ListMembers, "")

       Local $readCombo = GuiCtrlRead($ComboGroups)

          GUICtrlSetData($ListMembers, $readCombo)


         Case $msg = $ButtonAdd
            MsgBox(0, "Note", "Button 'Add User' not working !")


         Case $msg = $ButtonRemove
            MsgBox(0, "Note", "Button 'Remove User' not working !")


    EndSelect
 WEnd

 func terminate()                                                                     ;exit
    Exit 0
EndFunc

 

Edited by kneze
Posted (edited)

These two functions return an array. So it's easy to populate the combo :

#Include <Array.au3>

$aUsers = _LocalAccounts_GetUserList("remotecomputername")
_ArrayDisplay($aUsers)
$aGroups = _LocalAccounts_GetGroupList("remotecomputername")
_ArrayDisplay($aGroups)


Func _LocalAccounts_GetUserList($sComputername = @ComputerName)
    Local $aFilter= ["user"], $aResult[1], $oUser

    Local $oComputer = ObjGet("WinNT://" & $sComputerName)
    If NOT IsObj($oComputer) Then Return SetError(1, 0, 0)

    $oComputer.Filter = $aFilter
    For $oUser In $oComputer
        Redim $aResult[ UBound($aResult) + 1]
        $aResult[ UBound($aResult) - 1] = $oUser.Name
    Next
    $aResult[0] = UBound($aresult) - 1

    Return $aResult
EndFunc

Func _LocalAccounts_GetGroupList($sComputerName = @ComputerName)
    Local $aFilter = ["group"], $aResult[1], $oGroup

    Local $oComputer = ObjGet("WinNT://" & $sComputerName)
    If NOT IsObj($oComputer) Then Return SetError(1, 0, 0)

    $oComputer.Filter = $aFilter
    For $oGroup In $oComputer
        Redim $aResult[ UBound($aResult) + 1]
        $aResult[ UBound($aResult) - 1] = $oGroup.Name
    Next
    $aResult[0] = UBound($aresult) - 1

    Return $aResult
EndFunc

 

Edited by jguinch
Posted

closing double quotes is missing

$aUsers = _LocalAccounts_GetUserList("remotecomputername")

 

@kneze

Please read this:  How to post code on the forum

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 11/9/2015 at 5:13 PM, kneze said:

Sorry for wrong way posting my code.

You can still edit your post, to fix it.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Hi now read local Groups on remote computer and put it to combobox works fine. Now i would like to red members of selected group, but array remains empty.

Please show me whats wrong.

 

 

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.1
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here


#Include <Array.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>


$Form1_1 = GUICreate("Add / Remove Members", 669, 433, 196, 128)
GUISetBkColor(0xFFFFFF)
$Group1 = GUICtrlCreateGroup("", 8, 8, 324, 185)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
$LabelCpname = GUICtrlCreateLabel("CP Name:", 16, 32, 81, 23)
GUICtrlSetFont(-1, 12, 800, 0, "Arial")
$InputCpname = GUICtrlCreateInput("", 16, 56, 129, 30)
GUICtrlSetFont(-1, 14, 800, 0, "Arial")
$LabelLocalGroups = GUICtrlCreateLabel("Local Groups", 16, 101, 107, 20)
GUICtrlSetFont(-1, 12, 800, 0, "Arial")
$ComboGroups = GUICtrlCreateCombo("", 16, 157, 298, 25)
GUICtrlSetData(-1, "")
GUICtrlSetBkColor(-1, 0xFFFF00)
$ButtonReadGroups = GUICtrlCreateButton("read local Groups", 160, 56, 130, 25)
$ButtonReadmembers = GUICtrlCreateButton("read Members", 16, 125, 130, 25)
$IconReadLocalGroups = GUICtrlCreateIcon("C:\temp\work_ICONS\Yell_On_B.ico", -1, 300, 60, 16, 16)
$IconReadMembers = GUICtrlCreateIcon("C:\temp\work_ICONS\Yell_On_B.ico", -1, 155, 130, 16, 16)
$Status = GUICtrlCreateLabel("Status", 150, 115, 32, 15)
GUICtrlSetFont(-1, 7, 800, 0, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
$Statu = GUICtrlCreateLabel("Status", 295, 45, 32, 15)
GUICtrlSetFont(-1, 7, 800, 0, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("User", 8, 210, 324, 109)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
$LabelUserID = GUICtrlCreateLabel("User ID:", 16, 259, 66, 23)
GUICtrlSetFont(-1, 12, 800, 0, "Arial")
$InputComputer = GUICtrlCreateInput("", 88, 251, 89, 30)
GUICtrlSetFont(-1, 14, 800, 0, "Arial")
$ButtonAdd = GUICtrlCreateButton("Add User", 184, 236, 100, 25)
$ButtonRemove = GUICtrlCreateButton("Remove user", 184, 273, 100, 25)
$IconAddUser = GUICtrlCreateIcon("C:\temp\work_ICONS\Yell_On_B.ico", -1, 295, 240, 16, 16)
$IconRemoveUser = GUICtrlCreateIcon("C:\temp\work_ICONS\Yell_On_B.ico", -1, 295, 280, 16, 16)
$Label1 = GUICtrlCreateLabel("Status", 290, 225, 32, 15)
GUICtrlSetFont(-1, 7, 800, 0, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
$Label2 = GUICtrlCreateLabel("Status", 290, 265, 32, 15)
GUICtrlSetFont(-1, 7, 800, 0, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$ListMembers = GUICtrlCreateList("", 339, 31, 325, 383)
GUICtrlSetData(-1, "")
$LabelMembersof = GUICtrlCreateLabel("Members of Group", 345, 10, 121, 20)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
$Pic1 = GUICtrlCreatePic("C:\temp\user-management-.jpg", 110, 330, 94, 87)
GUISetState(@SW_SHOW)



While 1
    $msg = GUIGetMsg()
    Select
        ;Case $GUI_EVENT_CLOSE
             Case $msg = $GUI_EVENT_CLOSE
            Exitloop


         Case $msg = $ButtonReadGroups
            ;MsgBox(0, "Note", "Button 'read Groups' not work !")

      $CPname = GuiCtrlRead($inputCpname)

            If $CPname = "" Then
                MsgBox(16, "Error", "Missing Computer Nr. ")
                   Else

         $aGroups = _LocalAccounts_GetGroupList($CPname)
       ;_ArrayDisplay($aGroups)

         Func _LocalAccounts_GetGroupList($sComputerName = $CPname)
         Local $aFilter = ["group"], $aResult[1], $oGroup

         Local $oComputer = ObjGet("WinNT://" & $CPname)

   If NOT IsObj($oComputer) Then Return SetError(1, 0, 0)

    $oComputer.Filter = $aFilter
    For $oGroup In $oComputer
        Redim $aResult[ UBound($aResult) + 1]
        $aResult[ UBound($aResult) - 1] = $oGroup.Name
    Next
    $aResult[0] = UBound($aresult) - 1
    _ArrayDelete($aResult, 0)

  GUICtrlSetData($ComboGroups, _ArrayToString($aResult))

    Return $aResult
EndFunc

endif


         Case $msg = $ButtonReadmembers
            ;MsgBox(0, "Note", "Button 'Read Members' not work !")

       Local $readCombo = GuiCtrlRead($ComboGroups)

            If $readCombo = "" Then
                MsgBox(16, "Error", "no Group Selected ! ")
             Else

$CPname = GuiCtrlRead($inputCpname)

$aUsers = _LocalAccounts_GetUserList($CPname)
_ArrayDisplay($aUsers)

Func _LocalAccounts_GetUserList($sComputername = $cpname)
    Local $aFilter= ["user"], $aResult[1], $oUser

    Local $oComputer = ObjGet("WinNT://" & $CPname & "\" & $readCombo)

    $oComputer.Filter = $aFilter
    For $oUser In $oComputer
        Redim $aResult[ UBound($aResult) + 1]
        $aResult[ UBound($aResult) - 1] = $oUser.Name
    Next
    $aResult[0] = UBound($aresult) - 1
    _ArrayDelete($aResult, 0)
  GUICtrlSetData($ListMembers, _ArrayToString($aResult))
    Return $aResult
EndFunc
endif
         Case $msg = $ButtonAdd
            MsgBox(0, "Note", "Button 'Add User' not working !")


         Case $msg = $ButtonRemove
            MsgBox(0, "Note", "Button 'Remove User' not working !")

;RunWait(@ComSpec & ' /c Net Localgroup Administrators ' & @UserName & ' /Delete')


    EndSelect
 WEnd

 func terminate()                                                                     ;exit
    Exit 0
EndFunc

 

Posted (edited)

Here are some corrections (functions cannot be declared in a block of code)

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.1
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here


#Include <Array.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>


$Form1_1 = GUICreate("Add / Remove Members", 669, 433, 196, 128)
GUISetBkColor(0xFFFFFF)
$Group1 = GUICtrlCreateGroup("", 8, 8, 324, 185)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
$LabelCpname = GUICtrlCreateLabel("CP Name:", 16, 32, 81, 23)
GUICtrlSetFont(-1, 12, 800, 0, "Arial")
$InputCpname = GUICtrlCreateInput("", 16, 56, 129, 30)
GUICtrlSetFont(-1, 14, 800, 0, "Arial")
$LabelLocalGroups = GUICtrlCreateLabel("Local Groups", 16, 101, 107, 20)
GUICtrlSetFont(-1, 12, 800, 0, "Arial")
$ComboGroups = GUICtrlCreateCombo("", 16, 157, 298, 25)
GUICtrlSetData(-1, "")
GUICtrlSetBkColor(-1, 0xFFFF00)
$ButtonReadGroups = GUICtrlCreateButton("read local Groups", 160, 56, 130, 25)
$ButtonReadmembers = GUICtrlCreateButton("read Members", 16, 125, 130, 25)
$IconReadLocalGroups = GUICtrlCreateIcon("C:\temp\work_ICONS\Yell_On_B.ico", -1, 300, 60, 16, 16)
$IconReadMembers = GUICtrlCreateIcon("C:\temp\work_ICONS\Yell_On_B.ico", -1, 155, 130, 16, 16)
$Status = GUICtrlCreateLabel("Status", 150, 115, 32, 15)
GUICtrlSetFont(-1, 7, 800, 0, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
$Statu = GUICtrlCreateLabel("Status", 295, 45, 32, 15)
GUICtrlSetFont(-1, 7, 800, 0, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("User", 8, 210, 324, 109)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
$LabelUserID = GUICtrlCreateLabel("User ID:", 16, 259, 66, 23)
GUICtrlSetFont(-1, 12, 800, 0, "Arial")
$InputComputer = GUICtrlCreateInput("", 88, 251, 89, 30)
GUICtrlSetFont(-1, 14, 800, 0, "Arial")
$ButtonAdd = GUICtrlCreateButton("Add User", 184, 236, 100, 25)
$ButtonRemove = GUICtrlCreateButton("Remove user", 184, 273, 100, 25)
$IconAddUser = GUICtrlCreateIcon("C:\temp\work_ICONS\Yell_On_B.ico", -1, 295, 240, 16, 16)
$IconRemoveUser = GUICtrlCreateIcon("C:\temp\work_ICONS\Yell_On_B.ico", -1, 295, 280, 16, 16)
$Label1 = GUICtrlCreateLabel("Status", 290, 225, 32, 15)
GUICtrlSetFont(-1, 7, 800, 0, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
$Label2 = GUICtrlCreateLabel("Status", 290, 265, 32, 15)
GUICtrlSetFont(-1, 7, 800, 0, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$ListMembers = GUICtrlCreateList("", 339, 31, 325, 383)
GUICtrlSetData(-1, "")
$LabelMembersof = GUICtrlCreateLabel("Members of Group", 345, 10, 121, 20)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
GUICtrlSetColor(-1, 0x0000FF)
$Pic1 = GUICtrlCreatePic("C:\temp\user-management-.jpg", 110, 330, 94, 87)
GUISetState(@SW_SHOW)



While 1
    $msg = GUIGetMsg()
    Select
        ;Case $GUI_EVENT_CLOSE
        Case $msg = $GUI_EVENT_CLOSE
            Exitloop
        Case $msg = $ButtonReadGroups
            ;MsgBox(0, "Note", "Button 'read Groups' not work !")
            $CPname = GuiCtrlRead($inputCpname)
            If $CPname = "" Then
                MsgBox(16, "Error", "Missing Computer Nr. ")
            Else
                $aGroups = _LocalAccounts_GetGroupList($CPname)
            Endif

         Case $msg = $ButtonReadmembers
            ;MsgBox(0, "Note", "Button 'Read Members' not work !")

            Local $readCombo = GuiCtrlRead($ComboGroups)

            If $readCombo = "" Then
                MsgBox(16, "Error", "no Group Selected ! ")
            Else
                $CPname = GuiCtrlRead($inputCpname)

                $aUsers = _LocalAccounts_GetUserList($CPname)
                _ArrayDisplay($aUsers)
            EndIf
            
        Case $msg = $ButtonAdd
            MsgBox(0, "Note", "Button 'Add User' not working !")

        Case $msg = $ButtonRemove
            MsgBox(0, "Note", "Button 'Remove User' not working !")
            ;RunWait(@ComSpec & ' /c Net Localgroup Administrators ' & @UserName & ' /Delete')

    EndSelect
 WEnd
 
Func _LocalAccounts_GetUserList($sComputername = @ComputerName)
    Local $aFilter= ["user"], $aResult[1], $oUser
    Local $oComputer = ObjGet("WinNT://" & $sComputername)
    If NOT IsObj($oComputer) Then Return SetError(1, 0, 0)

    $oComputer.Filter = $aFilter
    For $oUser In $oComputer
        Redim $aResult[ UBound($aResult) + 1]
        $aResult[ UBound($aResult) - 1] = $oUser.Name
    Next
    $aResult[0] = UBound($aresult) - 1
    _ArrayDelete($aResult, 0)
    GUICtrlSetData($ListMembers, _ArrayToString($aResult))
    Return $aResult
EndFunc


 
Func _LocalAccounts_GetGroupList($sComputerName = @ComputerName)
    Local $aFilter = ["group"], $aResult[1], $oGroup
    Local $oComputer = ObjGet("WinNT://" & $sComputerName)
    If NOT IsObj($oComputer) Then Return SetError(1, 0, 0)

    $oComputer.Filter = $aFilter
    For $oGroup In $oComputer
        Redim $aResult[ UBound($aResult) + 1]
        $aResult[ UBound($aResult) - 1] = $oGroup.Name
    Next
    $aResult[0] = UBound($aresult) - 1
    _ArrayDelete($aResult, 0)

    GUICtrlSetData($ComboGroups, _ArrayToString($aResult))

    Return $aResult
EndFunc
 
 
 

 func terminate()                                                                     ;exit
    Exit 0
EndFunc

Edit : look at the Local account UDF : https://www.autoitscript.com/forum/topic/74118-local-account-udf/

 

Edited by jguinch

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
×
×
  • Create New...