Jump to content
TMelanson

Using non system variable with WMI

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

Share this post


Link to post
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.

Share this post


Link to post
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

 

Share this post


Link to post
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 :)

Share this post


Link to post
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!

Share this post


Link to post
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 :)

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

  • Similar Content

    • By Yodavish
      I have this AutoIt script that works on Windows 7. However, when I test it on Windows 10, the objEvent will not execute the function it's supposed to call. So I have a webpage the script will check if it is active, then it will search if a popup button exist (elementId), if so, assign it to the event handler so that when that button is clicked, then handler function will move the focus back to the main GUI again.
      If WinActive("Website Title - Internet Explorer") Then    $ie = _IEAttach("Website Title")    $objElement= _IEGetObjById ($ie, "elementId")    local $oEvent = ObjEvent($objElement, "_MY_EVENT_HANDLER_") EndIf Func _MY_EVENT_HANDLER_onclick($oEvtObj)     msgbox(0,"Alert Window", "Button Clicked!") setMainWindow() ; sets focus back to main GUI EndFunc  
      I've verified, on Windows 10, that it does see the object element. But it will not trigger the event. Could it be related to some timing issues due to Windows 10 Internet Explorer over Windows 7 Internet Explorer? 
      Windows 7 IE version 11.0.9600.19507
      Windows 10 IE version 11.418.18362.0
      AutoIt v 3.3
       
    • By somebadhat
      Restore blank Windows 10 start menu icons.
      ; RESTORE BLANK WINDOWS 10 START MENU ICONS. ; TOGGLE SETTINGS-START-"USE START FULL SCREEN" TWICE ; THIS WILL RESTORE SOME OF THE BLANK ICONS ; FOR THOSE THAT IT DOES NOT REDO "CHANGE ICON" FROM THE PROPERTIES DIALOG BOX FOR EACH MISSING ICON. ; "C:\Program Files (x86)\AutoIt3\AutoIt3_x64.exe" "F:\MYAPPS\WINDOWS\StartMenu1.au3" ; windows 10 and autoit3 ; begin restore blank icons on start menu Run(@ComSpec & " /c start ms-settings:personalization-start", "", @SW_SHOWMINIMIZED) Sleep(500) WinWaitActive("Settings") Sleep(1000) ; WinWaitActive ("[CLASS:ApplicationFrameInputSinkWindow]", "") ; ; BEGIN https://www.autoitscript.com/forum/topic/1353-check-if-a-computer-is-online/ $Computer = "8.8.8.8" AutoItSetOption ("SendKeyDelay" , "125") ;was 55, 500 If RunWait("ping.exe -n 1 " & $Computer, "", @SW_HIDE) == 0 Then WinActivate("Settings") send("{tab}{down 5}{tab 6}") sleep(500) send("{space}{tab 10}") ; if online. Move to "use start full screen", toggle it and (improves reliability) return cursor to "find a setting" search box. Else WinActivate("Settings") send("{tab}{down 5}{tab 6}") sleep(500) send("{space}{tab 6}") ; if offline. Move to "use start full screen", toggle it and (improves reliability) return cursor to "find a setting" search box. EndIf ; END https://www.autoitscript.com/forum/topic/1353-check-if-a-computer-is-online/ ; ; MouseClick("left", 379, 490, 1, 0) ; Toggle "use start full screen". Autoitv3 Window Info MouseClick Coords are 23px north. If y=467 use y=490 Sleep(500) ; Msgbox(0,"Operation halted","Continue?") ; PAUSE FOR DEBUG ; WinClose("[CLASS:ApplicationFrameWindow]", "") ; Send("{LWIN 2}") ; OPEN AND CLOSE THE START MENU Send("{LWIN}") ; OPEN START MENU sleep(500) Send("{LWIN}") ; CLOSE START MENU Sleep(1000) ; Run(@ComSpec & " /c start ms-settings:personalization-start", "", @SW_SHOWMINIMIZED) ; Sleep(500) ; WinWaitActive ("Settings") ; WinWaitActive ("[CLASS:ApplicationFrameInputSinkWindow]", "") ; ; BEGIN https://www.autoitscript.com/forum/topic/1353-check-if-a-computer-is-online/ $Computer = "8.8.8.8" AutoItSetOption ("SendKeyDelay" , "125") ;was 125, 55, 500 If RunWait("ping.exe -n 1 " & $Computer, "", @SW_HIDE) == 0 Then WinActivate("Settings") sleep(500) send("{tab 7}") ; if online. Move to "use start full screen", sleep(500) send("{space}{tab 10}") ; if online. Toggle "use start full screen" and (improves reliability) return cursor to "find a setting" search box. Else WinActivate("Settings") sleep(500) send("{tab 7}") ; if offline. Move to "use start full screen", sleep(500) send("{space}{tab 6}") ; if offline. Toggle "use start full screen" and (improves reliability) return cursor to "find a setting" search box. EndIf ; END https://www.autoitscript.com/forum/topic/1353-check-if-a-computer-is-online/ ; ; MouseClick("left", 379, 490, 1, 0) ; Toggle "use start full screen". Autoitv3 Window Info MouseClick Coords are 23px north. If y=467 use y=490 Sleep(500) ; Msgbox(0,"Operation halted","Continue?") ; PAUSE FOR DEBUG WinClose("[CLASS:ApplicationFrameWindow]", "") ; end restore blank icons on start menu Exit
    • By DavidFromLafayette
      My company is moving from WIN7 to WIN10, I have had a script that runs in the middle of the night, when the computer is locked, to open an Excel spreadsheet, read a few cells, and generate an email.  This has worked fine under WIN7, when I try to run under WIN10 it freezes when it gets to a line to handle an array.  When I run without the computer locked the script runs as expected.  After doing a lot of investigating, it looks like the following line reads a "0" from the Excel file instead of a text string, only when the computer is locked.
      Any ideas?
      $Driver1 = _Excel_RangeRead($oWorkbook, "Template", "H12")
      This is how I am opening the Excel file:
      Local $oExcel = _Excel_Open(False)
      Sleep(10000)
      $oWorkbook = _Excel_BookOpen($oExcel, $sFilePath, True, False)
    • By mlazovjp
      I am going through the AutoIt Wiki and soaking up a lot of great information.  On the “Best Coding Practices” page at https://www.autoitscript.com/wiki/Best_coding_practices#Scopes_of_Variables there seems to be a minor detail missing.  In the “Variable Initialization” section at https://www.autoitscript.com/wiki/Best_coding_practices#Variable_Initialization , there are references to a data type $o, but in the table listing prefixes and their data types, there does not appear to be a prefix “o”.  I assume that this is for Object?
    • By Simpel
      Hi,
      at work we changed now from windows 7 to windows 10. Suddenly this command _GUICtrlListView_GetHotItem() isn't working anymore for me. When I drag&drop a listviewitem it always returns -1.
      Here is the script that worked with WIN7:
      #include <GUIConstantsEx.au3> #include <GuiListView.au3> #include <Misc.au3> Local $aArray_Base[5][2] = [["0 - 0", "0 - 1"], ["1 - 0", "1 - 1"], ["2 - 0", "2 - 1"], ["3 - 0", "3 - 1"], ["4 - 0", "4 - 1"]] GUICreate("listview", 220, 200) Global $g_idListView = GUICtrlCreateListView("", 10, 10, 200, 180) _GUICtrlListView_AddColumn($g_idListView, "Col 1", 100) _GUICtrlListView_AddColumn($g_idListView, "Col 2", 100) _GUICtrlListView_AddArray($g_idListView, $aArray_Base) GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $GUI_EVENT_PRIMARYDOWN _Arrange_List() EndSwitch WEnd Func _Arrange_List() Local $iSelected = _GUICtrlListView_GetSelectionMark($g_idListView) If $iSelected = -1 Then Return While _IsPressed(1) WEnd Local $iDropped = _GUICtrlListView_GetHotItem($g_idListView) ConsoleWrite("GetHotItem: " & $iDropped & @CRLF) If $iDropped > -1 Then _GUICtrlListView_BeginUpdate($g_idListView) If $iSelected < $iDropped Then _GUICtrlListView_InsertItem($g_idListView, "", $iDropped + 1) _GUICtrlListView_SetItemText($g_idListView, $iDropped +1, _GUICtrlListView_GetItemTextString($g_idListView, $iSelected), -1) _GUICtrlListView_DeleteItem($g_idListView, $iSelected) ElseIf $iSelected > $iDropped Then _GUICtrlListView_InsertItem($g_idListView, "", $iDropped) _GUICtrlListView_SetItemText($g_idListView, $iDropped, _GUICtrlListView_GetItemTextString($g_idListView, $iSelected + 1), -1) _GUICtrlListView_DeleteItem($g_idListView, $iSelected + 1) EndIf _GUICtrlListView_SetItemSelected($g_idListView, $iDropped) _GUICtrlListView_SetSelectionMark($g_idListView, $iDropped) _GUICtrlListView_EndUpdate($g_idListView) EndIf EndFunc Any ideas?
      Regards, Simpel
       
      P.S. If I click an item then $iDropped is returning the correct number.
×
×
  • Create New...