Jump to content

Install Windows Service


Geert
 Share

Recommended Posts

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)

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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')
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 2 years later...

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