Jump to content
Sign in to follow this  
Chuckconnor

list names of user profiles on pc win 7

Recommended Posts

Chuckconnor

I'm trying to build a routine that will allow a tech to type in the username of a user on a pc and have my routine verify the username is valid ( profile directory exists on PC ).

Here's what I've got so far, using inputbox().  My problem is getting a loop going so I can test the username and if valid start backup using that username, if not valid loop and allow re-entry of valid name.  Or if timeout or user clicks ok, I want to backup everything.  I'm using MS USMT tools scanstate.exe to backup data.

I was also thinking if I could list the profile directory names and allow the tech to pick one or click OK, or hit enter to backup all users.  but not sure how to code that function.

Func GetUserName2Backup()
 local $BackupAllUsers = "/ue:natgypsvc* /ue:natgypz-* /ue:*uniadmin /ue:*admin* /uel:17"    ;Global USMT Variable - used local for testing
 local $BackupSglUser  = "/ue:*.* /ui:natgyp"                                                                                 ;Global USMT Variable - used local for testing
 local $BackupWho   = ""

 Local $Answer

 While DirGetSize($Answer, 2) = -1
        $Answer = InputBox("Backup Single User by ID", "Enter User ID of Single User to Migrate?", "EVERYBODY", "", -1, -1, Default, Default, 30) ; Specifies which Domain User to backup
        ExitLoop
 WEnd

 If DirGetSize($Answer, 2) = -1 Then
       $BackupWho = $BackupAllUsers
       MsgBox($MB_SYSTEMMODAL, "", $BackupWho)
 Else
       $BackupWho = $BackupSglUser & $Answer
       MsgBox($MB_SYSTEMMODAL, "", $BackupWho)
 EndIf

EndFunc

Share this post


Link to post
Share on other sites
BrewManNH

Why not get the list of profiles on the computer and present them in a combobox instead? Then the tech can simply find it in the list and select it, rather than trying to type it out and hoping it's there.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
jguinch

As BrewManNH said, you could list the local profiles, from registry (HKLMSOFTWAREMicrosoftWindows NTCurrentVersionProfileList)

I didn't try it, but you can get the username from its SID (key name) with _Security__LookupAccountSid()

 

Share this post


Link to post
Share on other sites
BrewManNH

I was thinking something along the lines of this.

 

If @OSVersion == "WIN_7" Or @OSVersion == "WIN_VISTA" Or @OSVersion = "WIN_2008" Then
    $NewOS = True
ElseIf @OSVersion == "WIN_XP" Then
    $NewOS = False
Else
    MsgBox($MB_ICONexclam, "Wrong OS", "This Script only works on Windows XP and up, it can't be used on this version of Windows.", $Timeout)
    Exit
EndIf
If Not $NewOS Then
    $UserList = _FileListToArray("C:\Documents and Settings", "*.*", 2)
    For $I = 1 To $UserList[0]
        Select
            Case $UserList[$I] = "All Users"
            Case $UserList[$I] = "Default User"
            Case $UserList[$I] = "NetworkService"
            Case $UserList[$I] = "LocalService"
            Case $UserList[$I] = "Administrator"
            Case Else
                $aUserList[$X] = $UserList[$I]
                $X += 1
                If $X >= UBound($aUserList) Then
                    ReDim $aUserList[UBound($aUserList) + 10]
                EndIf
        EndSelect
    Next
Else
    $UserList = _FileListToArray("C:\Users", "*.*", 2)
    For $I = 1 To $UserList[0]
        Select
            Case $UserList[$I] = "All Users"
            Case $UserList[$I] = "Default"
            Case $UserList[$I] = "Default User"
            Case $UserList[$I] = "Public"
            Case Else
                $aUserList[$X] = $UserList[$I]
                $X += 1
                If $X >= UBound($aUserList) Then
                    ReDim $aUserList[UBound($aUserList) + 10]
                EndIf
        EndSelect
    Next
EndIf
ReDim $aUserList[$X]
$sUserList = _ArrayToString($aUserList, "|")
; and then to add that list to a combo
Global $UserName = GUICtrlCreateCombo("", 80, 190, 177, 25)
GUICtrlSetTip(-1, "Select User to back up/restore!")
GUICtrlSetData(-1, $sUserList)
There's going to be a lot of undeclared variables/functions in this snippet, as it's not a standalone working script, but the general idea should be easy to figure out.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Chuckconnor

BrewManNH,

Thanks for the code snippet.  It looks great,  I think I understand what you are proposing, but I should have started this post with I'm not a newbie but my autoit kung fu is weak.

I defined the missing variables but I get no display when I run the program. :think:

I have also never used arrays so any further pointers to usage, examples and documentation would be great. :ermm:  

PS: I'm developing this on a windows 8.1 workstation.

Edited by Chuckconnor

Share this post


Link to post
Share on other sites
BrewManNH

Here is a working example of that snippet above. It should work with XP and above, as long as you add it (the OSVersion information) to line 7.

#include <File.au3>
#include <Array.au3>
#include <msgboxconstants.au3>
#include <GUIConstantsEx.au3>

Global $NewOS = False, $Timeout = 5, $X = 0, $aUserList[10]
If @OSVersion == "WIN_7" Or @OSVersion == "WIN_VISTA" Or @OSVersion = "WIN_2008" Or @OSVersion == "WIN_81" Or @OSVersion == "WIN_8" Then
    $NewOS = True
ElseIf @OSVersion == "WIN_XP" Then
    $NewOS = False
Else
    MsgBox($MB_ICONEXCLAMATION, "Wrong OS", "This Script only works on Windows XP and up, it can't be used on this version of Windows.", $Timeout)
    Exit
EndIf
If Not $NewOS Then
    $UserList = _FileListToArray("C:\Documents and Settings", "*.*", 2)
    For $I = 1 To $UserList[0]
        Select
            Case $UserList[$I] = "All Users"
            Case $UserList[$I] = "Default User"
            Case $UserList[$I] = "NetworkService"
            Case $UserList[$I] = "GlobalService"
            Case $UserList[$I] = "Administrator"
            Case Else
                $aUserList[$X] = $UserList[$I]
                $X += 1
                If $X >= UBound($aUserList) Then
                    ReDim $aUserList[UBound($aUserList) + 10]
                EndIf
        EndSelect
    Next
Else
    $UserList = _FileListToArray("C:\Users", "*.*", 2)
    For $I = 1 To $UserList[0]
        Select
            Case $UserList[$I] = "All Users"
            Case $UserList[$I] = "Default"
            Case $UserList[$I] = "Default User"
            Case $UserList[$I] = "Public"
            Case Else
                $aUserList[$X] = $UserList[$I]
                $X += 1
                If $X >= UBound($aUserList) Then
                    ReDim $aUserList[UBound($aUserList) + 10]
                EndIf
        EndSelect
    Next
EndIf
ReDim $aUserList[$X]
$sUserList = _ArrayToString($aUserList, "|")
ConsoleWrite($sUserList & @CRLF)
Global $hGUI = GUICreate("Example", 300, 200)

; Create a combobox control.
Global $UserName = GUICtrlCreateCombo("", 10, 10)

; Add additional items to the combobox.
GUICtrlSetTip(-1, "Select User to back up/restore!")
GUICtrlSetData(-1, $sUserList)

; Display the GUI.
GUISetState(@SW_SHOW, $hGUI)

Global $sComboRead = ""

; Loop until the user exits.
While GUIGetMsg() <> $GUI_EVENT_CLOSE

WEnd

; Delete the previous GUI and all controls.
GUIDelete($hGUI)

This does not list the built-in user folders like the Administrator or default users. So you won't have to weed out the one's you don't need.

If a user has ever logged into the computer this is run on, it will list them.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
jguinch

My idea was to list all profiles from the registry and retrieve each associated username, because for me, it's a good way to work only with some used profiles. Also, this method covers the case where a profile directory name do not match with the username.

Here is my example. I just tried it on my own computer, not with domain users...

#include <GUIConstantsEx.au3>
#include <Security.au3>

Local $msg, $sValue, $aSplit
Local $aProfiles = _ListProfiles()
Local $aLVItems[$aProfiles[0][0]]



Global $hGUI = GUICreate("Example", 320, 260)

GUICtrlCreateLabel("Select a user profile to back up/restore :", 10, 10)
Local $ok = GUICtrlCreateButton("GO", 210, 230, 100)

Global $listview = GUICtrlCreateListView("Username|ProfilePath", 10, 40, 300, 180)
For $i = 1 To $aProfiles[0][0]
    $aLVItems[$i - 1] = GUICtrlCreateListViewItem($aProfiles[$i][2] & "|" & $aProfiles[$i][1], $listview)
Next

GUISetState()


While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit

    If $msg = $ok Then
        $sValue = GUICtrlRead( GUICtrlRead($listview) )
        If $sValue <> "0" Then
            $aSplit = StringSplit($sValue, "|")
            MsgBox(0, "", "Let's go for " & $aSplit[2] & " !")
        EndIf
    EndIf

WEnd







; #FUNCTION# ====================================================================================================================
; Name ..........: _ListProfiles
; Description ...: List all local user profiles
; Syntax ........: _ListProfiles()
; Parameters ....: None
; Return values .: An array with the following format:
;                    $aProfile[0][0] - Number of profiles
;                    $aProfile[1][1] - User SID
;                    $aProfile[1][2] - User profile path
;                    $aProfile[1][3] - Username
; Author ........: JGUINCH
; ===============================================================================================================================
Func _ListProfiles()
    Local $aResult[1][3]

    Local Const $sKey = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
    Local $sSid, $sProfileImagePath, $aAccount, $n = 1

    While 1
        $sSid = RegEnumKey($sKey, $n)
        If @error Then ExitLoop

        If StringRegExp($sSid, "S-1-5-21-(\d+-){3}\d+") Then
            $sProfileImagePath = StringReplace( _ExpandEnvStringsEx ( RegRead($sKey & "\" & $sSid, "ProfileImagePath") ), "::", ":")
            $aAccount = _Security__LookupAccountSid ( $sSID )

            If FileExists($sProfileImagePath) AND IsArray($aAccount) Then
                Redim $aResult [UBound($aResult) + 1][3]
                $aResult [UBound($aResult) - 1][0] = $sSid
                $aResult [UBound($aResult) - 1][1] = $sProfileImagePath
                $aResult [UBound($aResult) - 1][2] = $aAccount[1] & "\" & $aAccount[0]
            EndIf
        EndIf

        $n += 1
    WEnd

    $aResult[0][0] = UBound($aResult) - 1

    Return $aResult

EndFunc




; #FUNCTION# ====================================================================================================================
; Name ..........: _ExpandEnvStringsEx
; Description ...: Expands environment variable strings and replaces them with their defined values
; Syntax ........: _ExpandEnvStrings($sString)
; Parameters ....: $sString             - String to convert for environment variables.
; Return values .: Success : Converted string
; Remarks .......: @extended will contain the number of replacements
; Author ........: JGUINCH
; ===============================================================================================================================
Func _ExpandEnvStringsEx($sString)
    Local $aVars = StringRegExp($sString, "%([^%]+)%", 3)
    Local $iCount = 0

    If IsArray($aVars) Then
        For $i = 0 To UBound($aVars) - 1
            $sVal = EnvGet($aVars[$i])
            If $sVal <> "" Then
                $sString = StringReplace($sString, "%" & $aVars[$i] & "%", $sVal )
                $iCount += 1
            EndIf
        Next
        SetExtended( $iCount )
    Else
        SetExtended(0)
    EndIf

    Return $sString
EndFunc
Edited by jguinch

Share this post


Link to post
Share on other sites
Chuckconnor

man what great code jguinch and BrewManNH

Thanks So much.

I hope I'll catch up to you guys one day, so I can offer others, great help like this.

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  

×