Sign in to follow this  
Followers 0
stev379

Get local administrator name

3 posts in this topic

#1 ·  Posted (edited)

I got a function from Torgeir to get the local administrator account name (in case it's already been changed). I'm trying to convert to AutoIT. Obviously, my current attempt below doesn't work.

Any suggestions on a proper conversion?

Original VB code

Function GetAdministratorName(sComputerName)
    Dim sUserSID, oWshNetwork, oUserAccount

    Set oUserAccounts = GetObject( _
    "winmgmts:{impersonationLevel=impersonate}!//" _
    & sComputerName & "/root/cimv2").ExecQuery( _
    "Select Name, SID from Win32_UserAccount WHERE Domain = '" _
    & sComputerName & "'")

    On Error Resume Next
    For Each oUserAccount In oUserAccounts
        If Left(oUserAccount.SID, 9) = "S-1-5-21-" And _
            Right(oUserAccount.SID, 4) = "-500" Then
            GetAdministratorName = oUserAccount.Name
            Exit For
        End if
    Next
End Function

Attempted AutoIT code

Func GetAdministratorName(@ComputerName)
    Dim $UserSID, $oWshNetwork, $oUserAccount

    $oUserAccounts = objGet( _
    "winmgmts:{impersonationLevel=impersonate}!//" _
    & @ComputerName & "/root/cimv2").ExecQuery( _
    "Select Name, SID from Win32_UserAccount WHERE Domain = '" _
    & @ComputerName & "'")

;On Error Resume Next
    For $oUserAccount In $oUserAccounts
        If StringLeft($oUserAccount.SID, 9) = "S-1-5-21-" And _
            StringRight($oUserAccount.SID, 4) = "-500" Then
            GetAdministratorName = $oUserAccount.Name
        Exit For
        Endif
    Next
EndFunc
Edited by stev379

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This should do it:

$Admin = GetAdministratorName(@ComputerName)
ConsoleWrite('@@ Debug(1) : $Admin = ' & $Admin & @lf & '>Error code: ' & @error & @lf) ;### Debug Console

Func GetAdministratorName($ComputerName)
    Dim $UserSID, $oWshNetwork, $oUserAccount
    $objWMIService = objGet( "winmgmts:{impersonationLevel=impersonate}!//"  & $ComputerName & "/root/cimv2")
    $oUserAccounts = $objWMIService.ExecQuery("Select Name, SID from Win32_UserAccount WHERE Domain = '" & $ComputerName & "'")
    For $oUserAccount In $oUserAccounts
        If StringLeft($oUserAccount.SID, 9) = "S-1-5-21-" And _
            StringRight($oUserAccount.SID, 4) = "-500" Then
            Return $oUserAccount.Name
        Endif
    Next
EndFunc
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

This should do it:

$Admin = GetAdministratorName(@ComputerName)
ConsoleWrite('@@ Debug(1) : $Admin = ' & $Admin & @lf & '>Error code: ' & @error & @lf) ;### Debug Console

Func GetAdministratorName($ComputerName)
    Dim $UserSID, $oWshNetwork, $oUserAccount
    $objWMIService = objGet( "winmgmts:{impersonationLevel=impersonate}!//"  & $ComputerName & "/root/cimv2")
    $oUserAccounts = $objWMIService.ExecQuery("Select Name, SID from Win32_UserAccount WHERE Domain = '" & $ComputerName & "'")
    For $oUserAccount In $oUserAccounts
        If StringLeft($oUserAccount.SID, 9) = "S-1-5-21-" And _
            StringRight($oUserAccount.SID, 4) = "-500" Then
            Return $oUserAccount.Name
        Endif
    Next
EndFunc
Thanks! You're awsome!! Edited by stev379

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