Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to launch a simple windows service through Windows PowerShell ISE. My code is as below:


$serviceName = "BabServiceInPowerShell"

Class BabServiceInPowerShell : System.ServiceProcess.ServiceBase {
    [System.ComponentModel.Description("OnStart")]
    [void] OnStart([string[]]$args) {
        Write-Host "Service started"
    }

    [System.ComponentModel.Description("OnStop")]
    [void] OnStop() {
        Write-Host "Service stopped"
    }
}

$service = [BabServiceInPowerShell]::new()

Register-ObjectEvent -InputObject $service -EventName "OnStart" -Action {
    $global:serviceStatus = "Starting"
}

Register-ObjectEvent -InputObject $service -EventName "OnStop" -Action {
    $global:serviceStatus = "Stopping"
}

Set-Service -Name $serviceName -Status Running



When I click Run Script button it shows this error

Register-ObjectEvent : Cannot register for the specified event. An event with the name 'OnStart' does not exist.
Parameter name: eventName
At F:\Per\PowerShell\WindowsServiceBab.ps1:19 char:1
+ Register-ObjectEvent -InputObject $service -EventName OnStart -Action ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (BabServiceInPowerShell:BabServiceInPowerShell) [Register-ObjectEvent], ArgumentExcep
tion
+ FullyQualifiedErrorId : INVALID_REGISTRATION,Microsoft.PowerShell.Commands.RegisterObjectEventCommand


So it won't allow me to register OnStart and OnStop events for my windows service. Please help!

What I have tried:

I've gone through some articles from internet. Unfortunately I couldn't find a single article on this topic. I'm working on PowerShell ISE to develop my Windows Service purely in Windows PowerShell.
Posted
Updated 9-Apr-23 1:26am

1 solution

OnStart and OnStop are not events the .NET object System.ServiceProcess.ServiceBase does expose. These are virtual Methods you can ovveride in case you derive from ServiceBase.

For more infromation please read this: Register-ObjectEvent (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn[^].

Quote:
EventName
Specifies the event to which you are subscribing.

The value of this parameter must be the name of the event that the .NET object exposes.
 
Share this answer
 
Comments
baburman 10-Apr-23 8:58am    
Thanks for the reply. The question is then how may I register OnStart() and OnStop() events for my service?
0x01AA 10-Apr-23 9:54am    
For this you have the methods [void] OnStart([string[]]$args) {
Write-Host "Service started"
}
which will called
baburman 10-Apr-23 12:56pm    
Ok, then I remove Register-ObjectEvent cmdlets and here's my final code.


Add-Type -AssemblyName System.ServiceProcess

$serviceName = "BabServiceInPowerShell"

Class BabServiceInPowerShell : System.ServiceProcess.ServiceBase {
[System.ComponentModel.Description("OnStart")]
[void] OnStart([string[]]$args) {
#Write-Host "Service started"
}

[System.ComponentModel.Description("OnStop")]
[void] OnStop() {
#Write-Host "Service stopped"
}
}

$service = [BabServiceInPowerShell]::new()


Set-Service -Name $serviceName -StartupType Automatic
Start-Service -Name $serviceName



Still it gives error which I can't figure:

Start-Service : Service 'My Service (BabServiceInPowerShell)' cannot be started due to the following error: Cannot start service
BabServiceInPowerShell on computer '.'.
At F:\Per\PowerShell\WindowsServiceBab.ps1:31 char:1
+ Start-Service -Name $serviceName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommand
Exception
+ FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900