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