Jump to content
Sign in to follow this  
nikink

$objWMIService.Get error in v3.3.14

Recommended Posts

nikink

Hi all,

I have a bit of code that works on my old Win10 PC, that fails on my new Win10 PC, and I think the only significant difference is the version of Autoit - old PC has 3.3.12, new has 3.3.14.

I couldn't find anything mentioned in the change logs though, so perhaps I'm wrong.

Anyway, the code to replicate my issue is:

Test('username', 'DOMAIN')
; THIS ERRORS:
;Test('localun', 'DOMAIN')
; THIS ERRORS:
;Test(' ', ' ')
; THIS ERRORS:
;Test('', '')
; THIS ERRORS:
;Test('localun', '')
; THIS ERRORS:
;Test('', 'DOMAIN')

Func Test($un, $dom)

    $compName = 'PCNAME'
    $FullName = '.'
    $Description = '.'
    ; get the WIM object
    $objWMIService = ObjGet("winmgmts:\\" & $compName & "\root\cimv2")

    ; get default user full name and description
    $objAccount = $objWMIService.Get("Win32_UserAccount.Name='" & $un & "',Domain='" & $dom & "'")
    If IsObj($objAccount) Then
        $FullName = $objAccount.FullName
        $Description = $objAccount.Description
    EndIf
    ConsoleWrite($FullName & @CRLF)
    ConsoleWrite($Description & @CRLF)
    Return
EndFunc

 

On my old PC this code will output just . and . for each of those line currently commented out. Which is fine.

On my new PC any of those commented out lines of code cause an error, and the script won't even compile.

$objAccount = $objWMIService.Get("Win32_UserAccount.Name='" & $un & "',Domain='" & $dom & "'")
$objAccount = $objWMIService^ ERROR

I'm very much a newb with the WMI stuff and objects, but it looks like the .Get property is failing when either $un or $dom aren't valid in v3.3.14, whereas in 3.3.12 the .Get would fail to return an object, which is then caught by the If statement.

Am I on track with this? Is there some new/better way to code the example so that 3.3.14 will compile it? :)

Share this post


Link to post
Share on other sites
ripdad

try this...

Opt('MustDeclareVars', 1)
;
Local $rtn = Test(@UserName, @ComputerName)
MsgBox(0, 'Results', $rtn)
;
Func Test($un, $dom)
    Local $objWMI = ObjGet('winmgmts:\\' & $dom & '\root\cimv2')
    Local $objAccount = $objWMI.InstancesOf('Win32_UserAccount Where Name="' & $un & '"')
    If $objAccount.Count = 0 Then Return -1
    Local $string = ''
    ;
    For $objItem In $objAccount
        $string &= 'FullName: ' & $objItem.FullName & @CRLF
        $string &= 'Name: ' & $objItem.Name & @CRLF
        $string &= 'Description: ' & $objItem.Description & @CRLF & @CRLF
    Next
    ;
    Return $string
EndFunc
;

 


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
ripdad

You might have to put #RequireAdmin at the top of that script.


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
nikink

Ok, thanks for that! I'll give it a try - it might be ok as a workaround. :)

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  

  • Similar Content

    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I'm working on a little project of mines, and I was trying to use WMI Object.
      The question which I don't find an answer is: 
      Once I do the query with WMI Object, something like "SELECT * FROM Win32_LogonSession", instead of specify the field of the collection returned, ( i.e. $colItems.Caption ), can I loop though each property and each value of the property, writing so one row of code only?
      Hope my question was clear enough.
      Thanks in advance.

      Best Regards.
    • nacerbaaziz
      By nacerbaaziz
      Hi dear
      I have a question please
      Can we customize error messages in autoit script?
      I mean internal error messages
      for example
      AutoIt Error
      Line 4  (File "D:\my projects\NVDA Manager\New AutoIt v3
      Script.au3"):
      $script[1] = 1
      $script^ ERROR
      Error: Subscript used on non-accessible variable.
      For example, I want to customize this message
      Is this possible?
      am waiting for your answers
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to make a program that can look at a folder directory, find all the CSV files, and then add the data from CSV's to an array. 
      The problem I seem to be running into is on the 2nd iteration (2nd file) when the script will not create an array. Could someone please help? Thanks in advance
       
      #include <Array.au3> #include <File.au3> #include <MsgBoxConstants.au3> #include <Excel.au3> #include <MsgBoxConstants.au3> Global $MasterArray RefineData() Func RefineData() Local $i, $filenum, $file, $csvArray, $sFilePath = @ScriptDir $fileList = _FileListToArrayRec($sFilePath, "*.csv", 1) ;Create and array of all .csv files within folder ;=====Loop through the .csv files within the folder====== For $filenum = 1 To UBound($fileList) - 1 Step 1 $file = $fileList[$filenum] $sFilePath = $sFilePath & "\" & $file ;=====Create array based on csv file===== _FileReadToArray($sFilePath, $csvArray, $FRTA_NOCOUNT, ",") _ArrayDisplay($csvArray,"File: " & $filenum) If $filenum = 1 Then $MasterArray = $csvArray _ArrayDisplay($MasterArray, "Master") Else $MasterArray = _ArrayColInsert($MasterArray, UBound($MasterArray)) ;want column added at end For $i = 0 To UBound($MasterArray)-1 Step 1 $MasterArray[$i][UBound($MasterArray) - 1] = $csvArray[$i][4] Next _ArrayDisplay($MasterArray, "Master") EndIf Next EndFunc ;==>RefineData  
    • ur
      By ur
      In the autoit file, if we add multiple libraries (both internal and custom created one)
       
      When we compile the code and run the exe, some time we are getting runtime errors.
      But the line number of error is not showing the exact line number, might be it is including the line number count of libraries also.
      Can you please suggest how to get the exact error line number.
      So, it will be useful for debugging.
    • Rskm
      By Rskm
      i have seen many pointing out issues related to this, i tried reading through them - still unable to solve mine.. i have a script which reads certain lines based on a search item, this is an iterative process - the ldflst_file gets updated for each iteration and the script finds the line number which has the search item. this works perfectly for few iterations, but stops in some cases abruptly with the error = 
      Subscript used on non-accessible variable $aRead^ ERROR I have verified that the search item is present in the file. what could be the reason for this code to stop in certain cases. thanks for help
      my code below (partial).. full code is in attachment
       
      local $aRead=FileReadToArray($ldflst_file)
      global $ldflst_var1=" JOINT  LOAD       FORCE(X)        FORCE(Y)        FORCE(Z)       MOMENT(X)       MOMENT(Y)       MOMENT(Z)"
       
      for $i= 0 to $ldflst_totlines    ; ldflst_totlines is total number of  lines in the file 'ldflst_file'

          if $aRead[$i]=$ldflst_var1 Then
              $ldflst_LFound=$i
              ExitLoop
              else
              EndIf
      Next
       
       
      nomo_new1.au3
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.