Jump to content

Simple Function to get User SID


Recommended Posts

Hey guys, i stumpled across the problem to get the SID of a user in a very simple way, without including additional UDFs and stuff. So i came up with my own approach. 

You can simply call the function by inserting the wanted Username as parameter.

;For Example get your Current User SID
_getSID(@UserName)

 

; #FUNCTION# ====================================================================================================================
; Name ..........: _getSID
; Description ...: Get the SID of the given User
; Syntax ........: _getSID($sUsername)
; Parameters ....: $sUsername       - a string value.
; Return values .: Success - SID    - a string value.
;                  Failure - @error
; Author ........: Blitzkid
; Modified ......: 22.08.2024
; Remarks .......: 
; Related .......: 
; Link ..........: 
; Example .......: No
; ===============================================================================================================================
Func _getSID($sUsername)

    Local $sCommand = "wmic useraccount where name='" & $sUsername & "' get sid"
    Local $hProcess = Run('cmd /c ' & $sCommand, @SystemDir, @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
    Local $sResult = ""
    Local $sLine

    While 1
        $sLine = StdoutRead($hProcess)
        If @error Then ExitLoop

        If StringLen($sLine) > 0 Then
            $sResult &= $sLine
        EndIf
    WEnd

    If StringInStr($sResult, "Keine Instanzen") Then Return @error
    If StringInStr($sResult, "No Instance") Then Return @error
    
    $sResult = StringStripWS(StringTrimLeft(StringStripWS($sResult, 4), 4),3)
    ConsoleWrite("User SID: " & $sResult & @CRLF)

    Return $sResult
EndFunc   ;==>_getSID

 

If you use a OS Language which isnt german or english you need to update the following lines or add another one in your needed language:

If StringInStr($sResult, "Keine Instanzen") Then Return @error
If StringInStr($sResult, "No Instance") Then Return @error

 

Hope this helps :)

Cheers Blitzkid

Edited by Blitzkid
Link to comment
Share on other sites

i wrote this function, to get back a String in a simple and easy way.

With the mentioned functions i need to convert the bytes in strings, which also needs another function. Depending on your environment the $sSystem parameter can be a bit tricky and leads to a 0 result. At least thats my experience in bigger domains.

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

×
×
  • Create New...