Jump to content

COM stuff - .change method


Recommended Posts

This works - this just changes the caption of the service

$strComputer = "localhost"
$objWMIService = ObjGet ("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_Service WHERE Caption = 'Error Reporting Service' ")
If IsObj ($colItems) Then
    For $objItem In $colItems
        $objItem.change("A new caption")
    Next
EndIf

This does not work - this tries to change the startup mode of a service.

$strComputer = "localhost"
$objWMIService = ObjGet ("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_Service WHERE Caption = 'Error Reporting Service' ")
If IsObj ($colItems) Then
    For $objItem In $colItems
        $objItem.change(, , , , "Manual")  
    Next
EndIf

It looks like it should work

This this the code snipped from MS illustrating the .change method

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
    ("Select * from Win32_Service where StartMode = 'Manual'")
For Each objService in colServiceList
    errReturnCode = objService.Change( , , , , "Disabled")   
Next

Anyone any thoughts ? :(

Link to comment
Share on other sites

This does not work - this tries to change the startup mode of a service.

$strComputer = "localhost"
$objWMIService = ObjGet ("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_Service WHERE Caption = 'Error Reporting Service' ")
If IsObj ($colItems) Then
    For $objItem In $colItems
        $objItem.change(, , , , "Manual")  
    Next
EndIf

Anyone any thoughts ? :(

<{POST_SNAPBACK}>

I don't think you can do this in AutoIT

$objItem.change(, , , , "Manual")

Have a look here for proper usage of the Change method.

Link to comment
Share on other sites

Thanks for your reply.

Have a look here for proper usage of the Change method.

The snippet of code quoted in the first post was referenced from that page !!

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

    & "{impersonationLevel=impersonate}!\\" & _

    strComputer & "\root\cimv2")

Set colServiceList = objWMIService.ExecQuery _

    ("Select * from Win32_Service where StartMode = 'Manual'")

For Each objService in colServiceList

    errReturnCode = objService.Change( , , , , "Disabled") 

Next

I have just got home and run the example code given on first post - at home the code runs - but does not change to startup mode of the service. At work the script errors out pointing to the first (, <----- .

Any thoughts ? :(

Link to comment
Share on other sites

Thanks for your reply.

The snippet of code quoted in the first post was referenced from that page !!

I have just got home and run the example code given on first post - at home the code runs - but does not change to startup mode of the service. At work the script errors out pointing to the first (,  <-----   .

Any thoughts ? :(

<{POST_SNAPBACK}>

If you just want to change the start mode then you can use the ChangeStartMode method.

Otherwise, I think you will have to fill in all the params prior to StartMode using the service's built-in properties.

Here's how you can get to those properties:

$strComputer = @ComputerName
$objWMIService = ObjGet ("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_Service WHERE Caption = 'Error Reporting Service' ")
If IsObj ($colItems) Then
    For $objItem In $colItems
        ConsoleWrite($objItem.DisplayName & @LF)
        ConsoleWrite($objItem.PathName & @LF)
        ConsoleWrite($objItem.ServiceType & @LF)
        ConsoleWrite($objItem.ErrorControl & @LF)
        ConsoleWrite($objItem.StartMode & @LF)
        ConsoleWrite($objItem.DesktopInteract & @LF)
        ConsoleWrite($objItem.StartName & @LF)
    Next
EndIf
Edited by SumTingWong
Link to comment
Share on other sites

This does not work - this tries to change the startup mode of a service.

...
        $objItem.change(, , , , "Manual")  
...

<{POST_SNAPBACK}>

Hello steve8tch,

Like SumTingWong said: AutoIt does not allow empty parameters to function calls. That's because AutoIt does not have something like a NULL value (the nearest AutoIt can do is an empty string or an integer 0).

But since these type of COM-calls occur very often, I added some code that will support empty parameters, but ONLY in COM-functions.

I have tested your script with this modified AutoIt3.exe and it works !

I will submit the patch to jpm, who will hopefully merge it with the next beta version.

Thanks for finding this bug..(nah, not really a bug, more a 'missing feature' :-)

Regards,

-Sven

Link to comment
Share on other sites

Both of you - thankyou very much.

(I think this is proving an exercise in understanding MSDN as new functionality in Aut3!!!)

As you suggested...

$strComputer = "localhost"
$objWMIService = ObjGet ("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_Service WHERE Caption = 'Error Reporting Service' ")
If IsObj ($colItems) Then
    For $objItem In $colItems
        $objItem.changeStartMode("Manual")
    Next
EndIf

works.

I would like to have got the change method to work - just so that I could learn more from it - but after a few failed attempts... I thought that I just leave it for now and post my thanks to you!! :(

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...