Jump to content

Get local administrator name


Recommended Posts

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

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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
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...