Jump to content

Error: The requested action with this object has failed.


Go to solution Solved by yralexandre,

Recommended Posts

I am having difficulty with a script that I converted from VBscript. The purpose of the script is to stop a running service, change the service startname & password, then restart the service. The VBscript version works well so I am certain that something was lost in the translation. 

When I execute the AutoIt Script below an error dialogue box returns the following error.

Line 28 (Pathtoscript file.exe)

Error: The requested action with this object has failed. 

Dim $network

$network = ObjCreate("WScript.Network")
$strComputer = $network.ComputerName

$objWMIService = ObjGet("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colServiceList = $objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Service WHERE Name = 'Service'")
For $objService In $colServiceList
    $objService.StopService()
    If (@error <> 0 And ($objService.State = "Running")) Then
        Exit(4209)
    EndIf
Next

CCALBDPWR()

$objWMIService = ObjGet("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colServiceList = $objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Service WHERE Name = 'Service'")
For $objService In $colServiceList
    $objService.StartService()
    If (@error <> 0 And ($objService.State = "Stopped")) Then
        Exit(4209)
    EndIf
Next

Func CCALBDPWR()
    Local $objWMI, $objShare, $objInParam, $objOutParams

    $objWMIService = ObjGet("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
    $objShare = $objWMIService.Get("Win32_Service.Name='Service'")

    $objInParam = $objShare.Methods_("Change").inParameters.SpawnInstance_()

    $objInParam.Properties_.Item("StartName") =  "Domain\UserAcct"
    $objInParam.Properties_.Item("StartPassword") =  "Password"

    $objOutParams = $objWMIService.ExecMethod_("Win32_Service.Name='Service'", "Change", $objInParam);;<<== Error: The requested action with this object has failed

    If $objOutParams.ReturnValue <> @error Then
        Exit($objOutParams.ReturnValue)
    Else
        Exit(0)
    EndIf

    Return Hex($objOutParams.ReturnValue)
EndFunc

I did some digging around in the forum before posting and there were quite a few posts that seem to suggest to leverage the "OBJEVENT" function for com object error handling. With that I modified the script to include a function call to "OBJEVENT". When I execute the AutoIt Script below the "MSGBOX" returns what I believe to be the following captured com errors.

1. ErrorCode Return value is: (0x80020006) Unknown name.

2. ErrorCode Return value is: (0x000000A9) Variable must be of type 'Object'.

Local $Err = ObjEvent('AutoIt.Error', 'ObjErrorHandler')

Dim $network

$network = ObjCreate("WScript.Network")
$strComputer = $network.ComputerName

$objWMIService = ObjGet("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colServiceList = $objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Service WHERE Name = 'Service'")
For $objService In $colServiceList
    $objService.StopService()
    If (@error <> 0 And ($objService.State = "Running")) Then
        Exit(4209)
    EndIf
Next

CCALBDPWR()

$objWMIService = ObjGet("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colServiceList = $objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Service WHERE Name = 'Service'")
For $objService In $colServiceList
    $objService.StartService()
    If (@error <> 0 And ($objService.State = "Stopped")) Then
        Exit(4209)
    EndIf
Next

Func CCALBDPWR()
    Local $objWMI, $objShare, $objInParam, $objOutParams

    $objWMIService = ObjGet("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
    $objShare = $objWMIService.Get("Win32_Service.Name='Service'")

    $objInParam = $objShare.Methods_("Change").inParameters.SpawnInstance_()

    $objInParam.Properties_.Item("StartName") =  "Domain\UserAcct"
    $objInParam.Properties_.Item("StartPassword") =  "Password"

    $objOutParams = $objWMIService.ExecMethod_("Win32_Service.Name='Service'", "Change", $objInParam)

    If $objOutParams.ReturnValue <> @error Then
        Exit($objOutParams.ReturnValue)
    Else
        Exit(0)
    EndIf

    Return Hex($objOutParams.ReturnValue)
EndFunc

Func ObjErrorHandler()
    Local $AOE1 = $Err.ScriptLine
    Local $AOE2 = Hex($Err.Number, 8)
    Local $AOE3 = $Err.Description
    Local $AOE4 = $Err.WinDescription
    $Err.Clear
    Local $eMsg = ''
    If $AOE1 Then $eMsg &= ' '
    If $AOE2 Then $eMsg &= '(0x' & $AOE2 & ') '
    If $AOE3 Then $eMsg &= $AOE3 & ' '
    If $AOE4 Then $eMsg &= $AOE4
    MsgBox(8240, "Error", "ErrorCode Return value is: " & ($eMsg), 10)
EndFunc

My suspicion is that the following line is the culprit although I am not sure what needs to be done to correct it. Some advice would be greatly appreciated.

$objOutParams = $objWMIService.ExecMethod_("Win32_Service.Name='Service'", "Change", $objInParam)

yralexandre

Best Regards,

Y. R. Alexandre Sr.

Link to comment
Share on other sites

If you could post the original VBScript it would help to see if you translated it correctly or not.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Why create your own script? Search the Example Scripts forum and you will find a UDF to work with services.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

If you could post the original VBScript it would help to see if you translated it correctly or not.

 

BrewManNH here is the VBscript.

Dim network

Set network = CreateObject("WScript.Network")
strComputer = network.ComputerName

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Service WHERE Name = 'Service'")
For Each objService In colServiceList
    objService.StopService()
    If (Err.Number <> 0 And (objService.State = "Running")) Then
        WScript.Quit(4209)
    End If
    Err.Clear
    Set objWMIService = Nothing
Next  

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set objShare = objWMIService.Get("Win32_Service.Name='Service'")

Set objInParam = objShare.Methods_("Change"). _
    inParameters.SpawnInstance_()

objInParam.Properties_.Item("StartName") =  "Domain\UserAcct"
objInParam.Properties_.Item("StartPassword") =  "Password"

Set objOutParams = objWMIService.ExecMethod("Win32_Service.Name='Service'", "Change", objInParam)

If objOutParams.ReturnValue <> 0 Then
    WScript.Quit(objOutParams.ReturnValue)
End If

Set objWMIService = Nothing

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Service WHERE Name = 'Service'")
For Each objService In colServiceList
    objService.StartService()
    If (Err.Number <> 0 And (objService.State = "Stopped")) Then
        WScript.Quit(4209)
    End If
    Err.Clear
    Set objWMIService = Nothing
Next

WScript.Quit(objOutParams.ReturnValue)

Best Regards,

Y. R. Alexandre Sr.

Link to comment
Share on other sites

  • Solution

 

The original VB code does not have an "_" after ExecMethod....

VB

Set objOutParams = objWMIService.ExecMethod("Win32_Service.Name='Service'", "Change", objInParam)

AU3

$objOutParams = $objWMIService.ExecMethod_("Win32_Service.Name='Service'", "Change", $objInParam)

 

I feel incredibly grateful yet incredibly silly. Removing the "_" made the difference. Thank you for your assistance.

Best Regards,

Y. R. Alexandre Sr.

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