Jump to content

$objWMIService.Get error in v3.3.14


nikink
 Share

Recommended Posts

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? :)

Link to comment
Share on other sites

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

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