Jump to content

Using non system variable with WMI


 Share

Recommended Posts

Hi folks,

I'm hoping someone can help me out here.

Background:
I have the need to run a program with admin credentials (#RequireAdmin), and then get the SID of the locally logged in account. Not the admin account.

If you look at the attached script, Line 16 uses the @UserName variable and returns the SID for the admin account I used to launch the app.
I want to use line 17 which passes the result of the _GetUsername function which is a username using  ($UserName) to the WMIService.

I'm getting " The requested action with this object has failed.:" error message.

Any suggestions would be appreciated! :D

 

test1.au3

Link to comment
Share on other sites

I'm no expert with "winmgmts", but i can say the reason why the object fails, is the method "Get" with your properties is throwing an exception. And AutoIt does not work well with object exceptions, without catching object events.

I would think the code below MIGHT be a solution.

MsgBox(0, "", _Security__SidToStringSid(_Security__GetAccountSid($UserName)))

otherwise something like this:

$objAccount = $objWMIService.ExecQuery("SELECT SID FROM Win32_UserAccount WHERE Name='"&@UserName&"'"); i use @UserName, as the return from "_GetUsername()" contains string terminating char(s) and makes the query fail.
For $row in $objAccount
    ConsoleWrite($row.Name&@CRLF)
    ConsoleWrite($row.SID&@CRLF)
Next

 

Anyway, i hope some of this will be helpful.

Link to comment
Share on other sites

Here is a way to get the list of logged on users (from the registry)  :

#RequireAdmin


#include <Security.au3>
#include <Array.au3>

Local $aLoggedOnUsers = _GetLoggedOnUsers()
_ArrayDisplay($aLoggedOnUsers)

Func _GetLoggedOnUsers()
    Local $sSubkey, $i = 0, $aUserInfo, $aResult[1][2], $iCount
    While 1
        $i += 1
        $sSubkey = RegEnumKey("HKEY_USERS", $i)
        If @error Then ExitLoop

        If StringRegExp($sSubkey, "^S-1-5-21-(\d+-){3}\d+$") Then
            $iCount += 1
            $aUserInfo = _Security__LookupAccountSid ($sSubkey )

            Redim $aResult[$iCount + 1][2]
            $aResult[$iCount][0] = $aUserInfo[1] & "\" & $aUserInfo[0]
            $aResult[$iCount][1] = $sSubkey
        EndIf
    WEnd
    $aResult[0][0] = $iCount
    Return $aResult
EndFunc

 

Link to comment
Share on other sites

16 hours ago, genius257 said:

I'm no expert with "winmgmts", but i can say the reason why the object fails, is the method "Get" with your properties is throwing an exception. And AutoIt does not work well with object exceptions, without catching object events.

I would think the code below MIGHT be a solution.

MsgBox(0, "", _Security__SidToStringSid(_Security__GetAccountSid($UserName)))

otherwise something like this:

$objAccount = $objWMIService.ExecQuery("SELECT SID FROM Win32_UserAccount WHERE Name='"&@UserName&"'"); i use @UserName, as the return from "_GetUsername()" contains string terminating char(s) and makes the query fail.
For $row in $objAccount
    ConsoleWrite($row.Name&@CRLF)
    ConsoleWrite($row.SID&@CRLF)
Next

 

Anyway, i hope some of this will be helpful.

Thank you so much genius257! This works like a charm but it take a considerably longer time to generate the SID. Not sure why but it works :)

Link to comment
Share on other sites

15 hours ago, jguinch said:

Here is a way to get the list of logged on users (from the registry)  :

#RequireAdmin


#include <Security.au3>
#include <Array.au3>

Local $aLoggedOnUsers = _GetLoggedOnUsers()
_ArrayDisplay($aLoggedOnUsers)

Func _GetLoggedOnUsers()
    Local $sSubkey, $i = 0, $aUserInfo, $aResult[1][2], $iCount
    While 1
        $i += 1
        $sSubkey = RegEnumKey("HKEY_USERS", $i)
        If @error Then ExitLoop

        If StringRegExp($sSubkey, "^S-1-5-21-(\d+-){3}\d+$") Then
            $iCount += 1
            $aUserInfo = _Security__LookupAccountSid ($sSubkey )

            Redim $aResult[$iCount + 1][2]
            $aResult[$iCount][0] = $aUserInfo[1] & "\" & $aUserInfo[0]
            $aResult[$iCount][1] = $sSubkey
        EndIf
    WEnd
    $aResult[0][0] = $iCount
    Return $aResult
EndFunc

 

Oh this is a much quicker way to obtain the SID. Thanks so much jguinch!

Link to comment
Share on other sites

1 minute ago, TMelanson said:

Thank you so much genius257! This works like a charm but it take a considerably longer time to generate the SID. Not sure why but it works

Happy to help :)

Yeah, i imagine a better, faster way exists, but i don't know enough about it to be able to help, I'm afraid :)

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