Jump to content

This WMI way to get all windows's users is good?


Luigi
 Share

Recommended Posts

Hi Forum,

I know... there are many ways to get a list (array) with all windows's users...

This query in WMI is good?

Always work?

What I want? Get an array with all windows's users. The windows's default users is not wanted (like 'All Users', 'Default', 'Default User', etc).

Best regards,

Detefon

SELECT LocalPath FROM Win32_UserProfile WHERE SID LIKE 'S-1-5-21%'
#include <Array.au3>

Local $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")

Local $arrName = StringSplit("LocalPath", ",", 2)

Local $colItems = $objWMIService.ExecQuery("SELECT LocalPath FROM Win32_UserProfile WHERE SID LIKE 'S-1-5-21%'", "WQL", 0x10 + 0x20)
Local $Result = ''
Local $sLocalPath
Local $aUsers[1]
Local $sUsersFolder = _GetUsersFolder()
If IsObj($colItems) Then
    For $objItem In $colItems
        For $jj = 0 To UBound($arrName) - 1
            $sLocalPath = StringReplace(Execute("$objItem." & $arrName[$jj]), $sUsersFolder & "\", "")
            _ArrayAdd($aUsers, $sLocalPath)
        Next
    Next
    _ArrayDelete($aUsers, 0)
    _ArrayDisplay($aUsers, "Users")
EndIf

Func _GetUsersFolder()
    Local $arr = StringSplit(@DesktopCommonDir, "\", 2)
    Return $arr[0] & "\" & $arr[1]
EndFunc ;==>_GetUsersFolder
Edited by Detefon

Visit my repository

Link to comment
Share on other sites

Link to comment
Share on other sites

Work, for me, the doubt is: it always work?

I'm not sure here:

SELECT LocalPath FROM Win32_UserProfile WHERE SID LIKE 'S-1-5-21%'

If all users's guid starting with 's-1-5-21%' always return the users, that is que question! 8D

Edited by Detefon

Visit my repository

Link to comment
Share on other sites

Work, for me, the doubt is: it always work?

I'm not sure here:

SELECT LocalPath FROM Win32_UserProfile WHERE SID LIKE 'S-1-5-21%'

If all users's guid starting with 's-1-5-21%' always return the users, that is que question! 8D

Yes, I'm pretty sure.

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Yes should work. or use this registry key

HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionProfileList

saludos

Link to comment
Share on other sites

Thank you MikahS. ^^

No problem :)

Also, way to check if WMI is enabled (I'm pretty sure)

$oWMIService = ObjGet("winmgmts:\\.\root\cimv2")
If IsObj($oWMIService) Then
Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

#Include <Array.au3>
$a = _LocalAccounts_GetUserList()
_ArrayDisplay($a)


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

    Local $oComputer = ObjGet("WinNT://" & $sComputerName)
    If NOT IsObj($oComputer) Then Return SetError(2, 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 ; ===> _LocalAccounts_GetUserList

Link to comment
Share on other sites

Datefon,

A non-WMI way...

#include <File.au3>
;#include <array.au3>

local $aUsers = _FileListToArrayRec('C:\users','*',$FLTAR_FOLDERS)

for $1 = 1 to $aUsers[0]
    if not stringregexp($aUsers[$1],'All Users(\|)?|Default( User)?(\|)?|Public(/|)?') then ConsoleWrite($aUsers[$1] & @CRLF)
next

 This is good on Win7.  The directory structure is different on XP and may be different on Vista and Win8.

kylomas 

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

one more way (without wmi)

#include <array.au3>
Local $DOS_out

; Returns members of Users group (remove first 6 unwanted lines)
$iPID = Run(@ComSpec & ' /c NET LOCALGROUP Users | MORE /E +6', "", @SW_HIDE, 2)

Do ; wait that dos has finished
    $DOS_out &= StdoutRead($iPID)
Until @error

; Parse members of Users group from DOS output
$admins = StringSplit(StringStripWS($DOS_out, 7), @CR, 2)
_ArrayPop($admins); remove last unwanted line
_ArrayDisplay($admins) ; show Users group members

edit:

change the group name in this line to select members of other groups:

$iPID = Run(@ComSpec & ' /c NET LOCALGROUP Users | MORE /E +6', "", @SW_HIDE, 2)
Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

I hope that Google translator help me to say what I want. 
 
I am very happy to see that so many people reply this topic, and especially with different ways of doing the same thing! 
 
Particularly I read each code carefully and let the imagination fly, thinking of other alternatives, other scripts, this is really cool. 
 
I think unfair to choose one of the answers, to give the topic is closed, I can not do that. Choosing one is not an alternative for me, all are great.
 
All options solved my problem. 
 
Thank you: @Danyfirex, @MikahS, @j0kky, @mikell, @jguinch, @kylomas, @Chimp
:thumbsup:

Visit my repository

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...