Sign in to follow this  
Followers 0
Graywalker

Flummoxed by Objects

7 posts in this topic

I'm trying to figure out how to use objects in AutoIT. More specifically, how to run a program on a remote computer using my old tried and true methods from VBScript....

Set objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strPCName & "\root\cimv2")
        Set objProcess = objWMIService.Get("Win32_Process")
        Set objProgram = objProcess.Methods_("Create").InParameters.SpawnInstance_
        objProgram.CommandLine = Replace(strUninstall,"/I","/X") & " /qn"
        Wscript.echo Now & " Uninstalling " & strDisplayName & " using " & objProgram.CommandLine
        LogFile.WriteLine("<br>Uninstalling using <b>" & objProgram.CommandLine & "</b> <br>")
        Set strShell = objWMIService.ExecMethod("Win32_Process","Create",objProgram)

or such...

$oWMIService = ObjCreate("winmgmts:{impersonatonLevel=impersonate}!\\" & $sComputer & "\root\cimv2")
    $oProcess = $oWMIService.Get("Win32_Process")
    $oProgram = $oProcess.Methods_("Create").InParameters.SpawnInstance_
    $oProgram.CommandLine = "cmd dothisthing.exe"
    $strShell = $oWMIService.ExecMethod("Win32_Process","Create", $oProgram)

Isn't working.... getting a "Variable must be 'object' type" error on the $oProcess line. People have been coming up with all kinds of weird problems and scripting requests all week, brain is mush and its Friday.... What am I missing?

Share this post


Link to post
Share on other sites



have you searched the forum for "ExecMethod" ?

Lar.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites

I'm trying to figure out how to use objects in AutoIT. More specifically, how to run a program on a remote computer using my old tried and true methods from VBScript....

Set objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strPCName & "\root\cimv2")
        Set objProcess = objWMIService.Get("Win32_Process")
        Set objProgram = objProcess.Methods_("Create").InParameters.SpawnInstance_
        objProgram.CommandLine = Replace(strUninstall,"/I","/X") & " /qn"
        Wscript.echo Now & " Uninstalling " & strDisplayName & " using " & objProgram.CommandLine
        LogFile.WriteLine("<br>Uninstalling using <b>" & objProgram.CommandLine & "</b> <br>")
        Set strShell = objWMIService.ExecMethod("Win32_Process","Create",objProgram)

or such...

$oWMIService = ObjCreate("winmgmts:{impersonatonLevel=impersonate}!\\" & $sComputer & "\root\cimv2")
    $oProcess = $oWMIService.Get("Win32_Process")
    $oProgram = $oProcess.Methods_("Create").InParameters.SpawnInstance_
    $oProgram.CommandLine = "cmd dothisthing.exe"
    $strShell = $oWMIService.ExecMethod("Win32_Process","Create", $oProgram)

Isn't working.... getting a "Variable must be 'object' type" error on the $oProcess line. People have been coming up with all kinds of weird problems and scripting requests all week, brain is mush and its Friday.... What am I missing?

Your VBS uses GetObject, but your AutoIt uses ObjCreate? Did you try ObjGet()?

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

$oWMIService = ObjCreate("winmgmts:.....

needs to be

$oWMIService = ObjGet("winmgmts:.....

i believe

edit: he he same time as PsaltyDS. :)

Edited by spudw2k

Share this post


Link to post
Share on other sites

Your VBS uses GetObject, but your AutoIt uses ObjCreate? Did you try ObjGet()?

:)

Yeah, ObjGet() was the first thing I tried, tried the Create when I kept getting :

Variable must be of type "Object".:
$oProcess = $oWMIService.Get("Win32_Process")
$oProcess = $oWMIService^ ERROR

in the debug console (using Tools - Go in SciTE). Also getting the "variable must be of type "object" when running the script.

Share this post


Link to post
Share on other sites

have you searched the forum for "ExecMethod" ?

Lar.

So, then this advice above turned up nothing for you? ... curious.

f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites

So, then this advice above turned up nothing for you? ... curious.

He can't call ExecMethod until he gets a valid reference in $oWMIService first, which is already failing. But I can see that would be cleaner thereafter.

Try it with some error handling. This works fine for me:

$sComputer = @ComputerName
$oWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $sComputer & "\root\cimv2")
If IsObj($oWMIService) Then
    ConsoleWrite("Debug: Object retrieved: " & ObjName($oWMIService) & @LF)
    $oProcess = $oWMIService.Get('Win32_Process')
    If IsObj($oProcess) Then
        ConsoleWrite("Debug: Got WMI object and Win32_Process object: " & ObjName($oProcess) & @LF)
    Else
        ConsoleWrite("Debug: Got WMI object, but failed to get Win32_Process object." & @LF)
    EndIf
Else
    ConsoleWrite("Debug: Failed to get WMI object." & @LF)
EndIf

Output:

>Running:(3.2.12.1):C:\Program Files\AutoIt3\autoit3.exe "C:\Temp\Test1.au3"    
Debug: Object retrieved: ISWbemServicesEx
Debug: Got WMI object and Win32_Process object: ISWbemObjectEx
+>17:00:43 AutoIT3.exe ended.rc:0

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  
Followers 0