Sign in to follow this  
Followers 0
fly

Need help listing users on a system...

14 posts in this topic

I am recoding an application I wrote into AutoIt cause I really like the language and support. However, as a n00b, I'm kinda lost.

What would be the best way to get a list of users on a PC and throw it into a COMBO box? Previously, I just listed all the folder names in C:\Documents and Settings in the combo box. is there a way to do either that or list all the users in HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\{SID}\ProfileImagePath?

My apologies for the silly question and thanks for your help!

Share this post


Link to post
Share on other sites



Hi fly,

This is function that I create for one of my script, it will give you a 2 dimensional array with 1) username and 2) Sid account it used registry key to get the information and also you can used for a remote system. The are three way to used:

1) $SID = _UserSID() ; It will give you all usernames and sid numbers from local computer

2) $SID = _UserSID("Danny"); It will give you only Danny SID number local computer

3) $SID = _UserSID("Danny", "HomeComputer") ; It will give you only Danny SID number from HomeComputer

dim $SID

$SID = _UserSID() 
;$SID = _UserSID("UserName")
;$SID = _UserSID('UserName', 'RemoteComputerName')

if IsArray($SID) Then
    for $x = 1 to $SID[0][0]
        msgbox(0, 'User SID', $SID[0][$x] & ' ====> ' & $SID[1][$x])
    Next
Else
    MsgBox(0, "", "Is not an Array.")
EndIf

;===============================================================================
; Function Name:   _UserSID()
;
; Description:   Return a 2 dimensional array first username second SID.
;
; Syntax:         _UserSID ( [$s_UserName, $s_RemoteComputer] )
;
; Parameter(s): $s_UserName = Username to get SID.
;                  $s_RemoteComputer = ComputerName on the network
;                
; Requirement(s):  External:   = None.
;                 Internal:   = None.
;
; Return Value(s): On Success: = Returns 2 dimensional array with UserName, SID and sets @error to 0.
;                 On Failure: = Returns "" and sets @error to 1.
;
; Author(s):       Dan Colón
;
; Note(s):       
;
; Example(s):
;   _UserSID("DColon") it will return DColon SID
;   _UserSID() it will return every user SID
;===============================================================================

Func _UserSID($s_UserName = "All", $s_RemoteComputer = '')
    If $s_UserName = '' Then $s_UserName = 'All'
    If $s_RemoteComputer <> '' Then
        If StringMid($s_RemoteComputer, 1, 1) <> '\' Or StringMid($s_RemoteComputer, 2, 1) <> '\' Or StringRight($s_RemoteComputer, 1) <> '\' Then
            $s_RemoteComputer = '\\' & StringReplace($s_RemoteComputer, '\', '') & '\'
        EndIf
    EndIf
    
    Local $line, $var, $ProfilePath, $i = 1
    Local Const $regkey = $s_RemoteComputer & "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\"
    Local Const $regkeyval1 = "ProfilesDirectory"
    Local Const $regkeyval2 = "ProfileImagePath"
        
    $ProfilePath = RegRead($regkey, $regkeyval1)    
    While 1
        $line = RegEnumkey($regkey, $i)
        $var = RegRead ( $regkey & $line, $regkeyval2)
        If @error = 1 Or @error = -1 Then ExitLoop
        If $s_UserName == "All" Then 
            If Not IsDeclared("aArray") Then Dim $aArray[1][1]
            ReDim $aArray[UBound($aArray) + 1][UBound($aArray) + 1]
            $aArray[0][UBound($aArray) - 1] = StringMid($var, StringInStr($var, '\', 0, -1) + 1)
            $aArray[1][UBound($aArray) - 1] = $line 
            $aArray[0][0] = UBound($aArray) - 1
        ElseIf StringLower($var) == StringLower($ProfilePath & "\" & $s_UserName) Then
            If Not IsDeclared("aArray") Then Dim $aArray[1][1]
            ReDim $aArray[UBound($aArray) + 1][UBound($aArray) + 1]
            $aArray[0][UBound($aArray) - 1] = StringMid($var, StringInStr($var, '\', 0, -1) + 1)
            $aArray[1][UBound($aArray) - 1] = $line 
            $aArray[0][0] = UBound($aArray) - 1
        EndIf
        $i = $i + 1
    WEnd
    If Not IsDeclared("aArray") Then
        SetError(1)
        Return("")
    Else
        SetError(0)
        Return($aArray)
    EndIf
EndFunc

Then all you have to do is used a for loop

for $x = 1 to $SID[0][0]

_GUICtrlComboAddString($h_combobox, $SID[0][$x])

next

and that will add username to the combo list

I hope this will help you to get started....


AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

That should be perfect! Thanks for the help!

Share this post


Link to post
Share on other sites

_GUICtrlComboAddString($h_combobox, $SID[0][$x])

Hmmm, where can I find info on _GUICtrlComboAddString?

Share this post


Link to post
Share on other sites

Im using the new beta 3.1.1.84 check this link AutoIt Beta

Crap. Doesn't look like I can use that. As soon as I put in:

#include <GuiCombo.au3>

The app blows up with this error:

C:\Program Files\AutoIt3\Include\Misc.au3(64,40) : ERROR: DllStructCreate(): undefined function.

Local $p = DllStructCreate ($struct)

I'm gonna try to uninstall/reinstall everything...

Share this post


Link to post
Share on other sites

When you run your script are you using ALT+F5 this will do a Beta Run


AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

Reinstall didn't work. Is there a bug submission on the site somewhere?

Share this post


Link to post
Share on other sites

When you run your script are you using ALT+F5 this will do a Beta Run

Oh! :">

Sucks being a n00b. Thanks!

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Don't worry welcome to AutoIt...

anyway I'm still a n00b....

Edited by Danny35d

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

Don't worry welcome to AutoIt...

anyway I'm still a n00b....

If I can pester you again, I tried changing your script, as I only need the userid from the array.

Here is what I changed:

If $s_UserName == "All" Then
            If Not IsDeclared("aArray") Then Dim $aArray[1]
            ReDim $aArray[UBound($aArray) + 1]
            $aArray[UBound($aArray) - 1] = StringMid($var, StringInStr($var, '\', 0, -1) + 1)
           ;$aArray[1][UBound($aArray) - 1] = $line 
            $aArray[0] = UBound($aArray) - 1

It seems to work, except the first thing in the array is the number 8. Not sure why. Also, for whatever reason, arrays still confuse me so its probably something obvious.

Share this post


Link to post
Share on other sites

Wow, I'm an idiot. Nevermind.

Share this post


Link to post
Share on other sites

Hi fly,

This is function that I create for one of my script, it will give you a 2 dimensional array with 1) username and 2) Sid account it used registry key to get the information and also you can used for a remote system. The are three way to used:

1) $SID = _UserSID() ; It will give you all usernames and sid numbers from local computer

2) $SID = _UserSID("Danny"); It will give you only Danny SID number local computer

3) $SID = _UserSID("Danny", "HomeComputer") ; It will give you only Danny SID number from HomeComputer

dim $SID

$SID = _UserSID() 
;$SID = _UserSID("UserName")
;$SID = _UserSID('UserName', 'RemoteComputerName')

if IsArray($SID) Then
    for $x = 1 to $SID[0][0]
        msgbox(0, 'User SID', $SID[0][$x] & ' ====> ' & $SID[1][$x])
    Next
Else
    MsgBox(0, "", "Is not an Array.")
EndIf

;===============================================================================
; Function Name:   _UserSID()
;
; Description:   Return a 2 dimensional array first username second SID.
;
; Syntax:         _UserSID ( [$s_UserName, $s_RemoteComputer] )
;
; Parameter(s): $s_UserName = Username to get SID.
;                  $s_RemoteComputer = ComputerName on the network
;                
; Requirement(s):  External:   = None.
;                 Internal:   = None.
;
; Return Value(s): On Success: = Returns 2 dimensional array with UserName, SID and sets @error to 0.
;                 On Failure: = Returns "" and sets @error to 1.
;
; Author(s):       Dan Colón
;
; Note(s):       
;
; Example(s):
;   _UserSID("DColon") it will return DColon SID
;   _UserSID() it will return every user SID
;===============================================================================

Func _UserSID($s_UserName = "All", $s_RemoteComputer = '')
    If $s_UserName = '' Then $s_UserName = 'All'
    If $s_RemoteComputer <> '' Then
        If StringMid($s_RemoteComputer, 1, 1) <> '\' Or StringMid($s_RemoteComputer, 2, 1) <> '\' Or StringRight($s_RemoteComputer, 1) <> '\' Then
            $s_RemoteComputer = '\\' & StringReplace($s_RemoteComputer, '\', '') & '\'
        EndIf
    EndIf
    
    Local $line, $var, $ProfilePath, $i = 1
    Local Const $regkey = $s_RemoteComputer & "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\"
    Local Const $regkeyval1 = "ProfilesDirectory"
    Local Const $regkeyval2 = "ProfileImagePath"
        
    $ProfilePath = RegRead($regkey, $regkeyval1)    
    While 1
        $line = RegEnumkey($regkey, $i)
        $var = RegRead ( $regkey & $line, $regkeyval2)
        If @error = 1 Or @error = -1 Then ExitLoop
        If $s_UserName == "All" Then 
            If Not IsDeclared("aArray") Then Dim $aArray[1][1]
            ReDim $aArray[UBound($aArray) + 1][UBound($aArray) + 1]
            $aArray[0][UBound($aArray) - 1] = StringMid($var, StringInStr($var, '\', 0, -1) + 1)
            $aArray[1][UBound($aArray) - 1] = $line 
            $aArray[0][0] = UBound($aArray) - 1
        ElseIf StringLower($var) == StringLower($ProfilePath & "\" & $s_UserName) Then
            If Not IsDeclared("aArray") Then Dim $aArray[1][1]
            ReDim $aArray[UBound($aArray) + 1][UBound($aArray) + 1]
            $aArray[0][UBound($aArray) - 1] = StringMid($var, StringInStr($var, '\', 0, -1) + 1)
            $aArray[1][UBound($aArray) - 1] = $line 
            $aArray[0][0] = UBound($aArray) - 1
        EndIf
        $i = $i + 1
    WEnd
    If Not IsDeclared("aArray") Then
        SetError(1)
        Return("")
    Else
        SetError(0)
        Return($aArray)
    EndIf
EndFunc

Then all you have to do is used a for loop

for $x = 1 to $SID[0][0]

_GUICtrlComboAddString($h_combobox, $SID[0][$x])

next

and that will add username to the combo list

I hope this will help you to get started....

I followed your directions, but I'm not able to populate the combolist, can you possibly show me with a sample script how to use it?

Thank you.

Share this post


Link to post
Share on other sites

It's an old thread, I know, but the script is still very useful. One problem with it is that it includes all accounts. To show only user accounts, check to see if 'UBound(StringSplit($SID[1][$x], "-")) - 1' = 8 for each entry.

Keith Olson

K-Soft Consulting

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