Jump to content

Recommended Posts

Posted

Hello there,

I need help with the new COM (Object) functionality in the AutoIt Beta.

I'm trying to automatically install a Windows Service. I found a perfect piece of code for this, only it is written in VBscript. I would like to have it AutoIt coded.

Even with some examples from this forum (like getting PcInfo etc.) I could not get a working script :whistle: .

Please help!

Here is the VBscript code:

' Connect to WMI. 
set objServices = GetObjecT("winmgmts:root\cimv2") 

' Obtain the definition of the Win32_Service class. 
Set objService = objServices.Get("Win32_Service") 

' Obtain an InParameters object specific to the Win32_Service.Create method. 
Set objInParam = objService.Methods_("Create").inParameters.SpawnInstance_() 

' Add the input parameters. 
objInParam.Properties_.item("Name") = "GPServer" '< - Service Name 
objInParam.Properties_.item("DisplayName") = "GPServer" '< - Display Name, what you see in the Services control panel 
objInParam.Properties_.item("PathName") = "c:\Server\srvany.exe" '< - Path and Command Line of the executable 
objInParam.Properties_.item("ServiceType") = 16 
objInParam.Properties_.item("ErrorControl") = 0 
objInParam.Properties_.item("StartMode") = "Manual" 
objInParam.Properties_.item("DesktopInteract") = True
'objInParam.Properties_.item("StartName") = ".\Administrator" '< - If null, will run as Local System 
'objInParam.Properties_.item("StartPassword") = "YourPassword" '< - Only populate if the SatrtName param is populated 

'More parameters and return statuses are listed in MSDN: "Create Method of the Win32_Service Class" 


' Execute the method and obtain the return status. 
' The OutParameters object in objOutParams is created by the provider. 
Set objOutParams = objService.ExecMethod_("Create", objInParam) 
wscript.echo objOutParams.ReturnValue

Thanks,

Geert (Netherlands)

Posted (edited)

Try this:

; Connect to WMI. 
$objServices = ObjGet("winmgmts:root\cimv2") 

; Obtain the definition of the Win32_Service class. 
$objService = $objServices.Get("Win32_Service") 

; Obtain an InParameters object specific to the Win32_Service.Create method. 
$objInParam = $objService.Methods_("Create").inParameters.SpawnInstance_() 

;Add the input parameters. 
$objInParam.Properties_.item("Name") = "GPServer";< - Service Name 
$objInParam.Properties_.item("DisplayName") = "GPServer";< - Display Name, what you see in the Services control panel 
$objInParam.Properties_.item("PathName") = "c:\Server\srvany.exe";< - Path and Command Line of the executable 
$objInParam.Properties_.item("ServiceType") = 16 
$objInParam.Properties_.item("ErrorControl") = 0 
$objInParam.Properties_.item("StartMode") = "Manual" 
$objInParam.Properties_.item("DesktopInteract") = True
;$objInParam.Properties_.item("StartName") = ".\Administrator";< - If null, will run as Local System 
;$objInParam.Properties_.item("StartPassword") = "YourPassword";< - Only populate if the SatrtName param is populated 

;More parameters and return statuses are listed in MSDN: "Create Method of the Win32_Service Class" 


; Execute the method and obtain the return status. 
; The OutParameters object in objOutParams is created by the provider. 
$objOutParams = $objService.ExecMethod_("Create", $objInParam) 
ConsoleWrite($objOutParams)

I can't test cause I'm in a restricted environment (no installation of software nor setting a own background image)

Edit: Typos

Edited by Raindancer
Say: "Chuchichäschtli"My UDFs:_PrintImage UDF_WinAnimate UDFGruess Raindancer
Posted

Raindancer, line 9 has an improper comment delimeter...other than that, it should work

Writing AutoIt scripts since

_DateAdd("d", -2, _NowCalcDate())
Posted

If you have sc.exe in your system directory, then you could just use this

Func _ServiceCreate( $display_name, $service_name, $exe_fullpath, $start = 'auto')
; creates a service from an executable file.
    $display_name = 'displayname= "' & $display_name & '"'
    $exe_fullpath = 'binpath= "' & $exe_fullpath & '"'
    $start = 'start= ' & $start
    If FileExists( $exe_fullpath) Then
        RunWait( @SystemDir & '\sc create ' & $service_name & ' ' & $exe_fullpath & ' ' & $display_name & ' ' & $start, '', @SW_HIDE)
    EndIf
EndFunc

Example usage:

_ServiceCreate('User Profile Hive Cleanup', 'UPHClean', @SystemDir & '\uphclean.exe')
Posted

Well hell, I've been looking into that off-and-on for the last year...and it's just an executable that creates system services :whistle:

Writing AutoIt scripts since

_DateAdd("d", -2, _NowCalcDate())
Posted

ok so now tha we can create a service how do we access and use the service? what i mean is if i start autoit3 as a service how can i pass that service a script to be interpreted.. contemplating this also leads me to think that live interpretation could be added to scite or a similiar IDE to check for mistakes as Type. if would have to be sandboxed with a ErrorControl it also shouldnt fully sandbox until a [CRLF] _isPressed Could activate this

$ErrorObj = @Error

Func _SandBox (Const ByReF $ErrorObj = 0, $Code)

Return Execute($ECode)

EndFunc

if _IsPressed("{Enter}") then

If $ErrorObj = 0 then

_SandBox(0, $Code)

EndIf

EndIf

http://www.myclanhosting.com/defiasVisit Join and contribute to a soon to be leader in Custumized tools development in [C# .Net 1.1 ~ 2.0/C/C++/MFC/AutoIt3/Masm32]
Posted

Thank you all for the quick reactions! :whistle:

SC.EXE is also new to me.

I will use the translated version made by Raindancer -tx-

There are several ways to create a service, but I needed a way to activate the option Interact with the desktop. This can also be done within the registry, but the app wont be visible unless you reboot.

Using WMI with the property:

$objInParam.Properties_.item("DesktopInteract") = True

it will always work as designed.

Thanks again,

Geert

Posted

Create Service

; Connect to WMI.
$objServices = ObjGet("winmgmts:root\cimv2")

; Obtain the definition of the Win32_Service class.
$objService = $objServices.Get ("Win32_Service")

; Obtain an InParameters object specific to the Win32_Service.Create method.
$objInParam = $objService.Methods_ ("Create") .inParameters.SpawnInstance_ ()

;Add the input parameters.
$objInParam.Properties_.item ("Name") = "YourService";< - Service Name
$objInParam.Properties_.item ("DisplayName") = "YourService";< - Display Name, what you see in the Services control panel
$objInParam.Properties_.item ("PathName") = "C:\YourService.exe";< - Path and Command Line of the executable
$objInParam.Properties_.item ("ServiceType") = 16
$objInParam.Properties_.item ("ErrorControl") = 0
$objInParam.Properties_.item ("StartMode") = "Automatic"
$objInParam.Properties_.item ("DesktopInteract") = True
;$objInParam.Properties_.item("StartName") = ".\Administrator";< - If null, will run as Local System
;$objInParam.Properties_.item("StartPassword") = "YourPassword";< - Only populate if the SatrtName param is populated

;More parameters and return statuses are listed in MSDN: "Create Method of the Win32_Service Class"


; Execute the method and obtain the return status.
; The OutParameters object in objOutParams is created by the provider.
$objOutParams = $objService.ExecMethod_ ("Create", $objInParam)
ConsoleWrite($objOutParams)

Delete Service

Dim $objWMIService, $objItem, $objService
Dim $colListOfServices, $strService

; NB strService is case sensitive.
$strService = "YourService";< - Service Name

; Connect to WMI. 
$objWMIService = ObjGet("winmgmts:root\cimv2")

$colListOfServices = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & $strService & "'")

For $objService in $colListOfServices
    $objService.Delete()
Next

Start Service

Dim $objWMIService, $objItem, $objService
Dim $colListOfServices, $strService

; NB strService is case sensitive.
$strService = "YourService";< - Service Name

; Connect to WMI. 
$objWMIService = ObjGet("winmgmts:root\cimv2")

$colListOfServices = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & $strService & "'")

For $objService in $colListOfServices
    $objService.StartService()
Next

Stop Service

Dim $objWMIService, $objItem, $objService
Dim $colListOfServices, $strService

; NB strService is case sensitive.
$strService = "YourService";< - Service Name

; Connect to WMI. 
$objWMIService = ObjGet("winmgmts:root\cimv2")

$colListOfServices = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & $strService & "'")

For $objService in $colListOfServices
    $objService.StopService()
Next

Have fun!

Geert

Posted

Had the Idea to wrap this code into some UDFs

Func _CreateService($name, $displayname, $pathname, $startmode = "Automatic", $desktopinteract = True, $startname ="", $servicetype = 16, $errorcontrol = 0)
; Connect to WMI.
    $objServices = ObjGet("winmgmts:root\cimv2")
    
; Obtain the definition of the Win32_Service class.
    $objService = $objServices.Get ("Win32_Service")
    
; Obtain an InParameters object specific to the Win32_Service.Create method.
    $objInParam = $objService.Methods_ ("Create") .inParameters.SpawnInstance_ ()
    
;Add the input parameters.
    $objInParam.Properties_.item ("Name") = $name;< - Service Name
    $objInParam.Properties_.item ("DisplayName") = $displayname;< - Display Name, what you see in the Services control panel
    $objInParam.Properties_.item ("PathName") = $pathname;< - Path and Command Line of the executable
    $objInParam.Properties_.item ("ServiceType") = $servicetype
    $objInParam.Properties_.item ("ErrorControl") = $errorcontrol
    $objInParam.Properties_.item ("StartMode") = $startmode
    $objInParam.Properties_.item ("DesktopInteract") = $desktopinteract
    If not $startname = "" Then 
        $objInParam.Properties_.item("StartName") = $startname;< - If null, will run as Local System
    EndIf
    If not $password = "" And not $startname ="" Then 
        $objInParam.Properties_.item("StartPassword") = $password;< - Only populate if the SatrtName param is populated
    EndIf

;More parameters and return statuses are listed in MSDN: "Create Method of the Win32_Service Class"


; Execute the method and obtain the return status.
; The OutParameters object in objOutParams is created by the provider.
    $objOutParams = $objService.ExecMethod_ ("Create", $objInParam)
    ConsoleWrite($objOutParams)
EndFunc

Func _DeleteService($name)
    Dim $objWMIService, $objItem, $objService
    Dim $colListOfServices, $strService

; NB strService is case sensitive.
    $strService = $name;< - Service Name

; Connect to WMI. 
    $objWMIService = ObjGet("winmgmts:root\cimv2")

    $colListOfServices = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & $strService & "'")

    For $objService in $colListOfServices
        $objService.Delete()
    Next
EndFunc

Func _StartService($name)
    Dim $objWMIService, $objItem, $objService
    Dim $colListOfServices, $strService

; NB strService is case sensitive.
    $strService = $name;< - Service Name

; Connect to WMI. 
    $objWMIService = ObjGet("winmgmts:root\cimv2")

    $colListOfServices = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & $strService & "'")

    For $objService in $colListOfServices
        $objService.StartService()
    Next
EndFunc

Func _StopService($name)
    Dim $objWMIService, $objItem, $objService
    Dim $colListOfServices, $strService

; NB strService is case sensitive.
    $strService = $name;< - Service Name

; Connect to WMI. 
    $objWMIService = ObjGet("winmgmts:root\cimv2")

    $colListOfServices = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & $strService & "'")

    For $objService in $colListOfServices
        $objService.StopService()
    Next
EndFunc

Though there isn't any bit of errorchecking.

Say: "Chuchichäschtli"My UDFs:_PrintImage UDF_WinAnimate UDFGruess Raindancer
  • 2 years later...
  • 15 years later...
Posted (edited)

I know it's old, but for anyone using Create Service, note ConsoleWrite($objOutParams) is meaningless (always empty), since $objService.ExecMethod_ ("Create", $objInParam) returns an object, not a string!

It should be:

ConsoleWrite($objOutParams.ReturnValue)

Whereas a ReturnValue of 0 means "The request was accepted", and everything else suggests which error happened (see in Microsoft's documentation).

Edited by LWC
Posted

He started his post with "I know it's old".
I think in this case it is a good idea to post his finding in an rather old thread for completeness.
So everyone searching for "Services" finds this thread (amongst others) and gets help in solving this problem.
Or should he have created a new thread for this few lines?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

 

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
  • Recently Browsing   0 members

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