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
OnStart
OnStop
System.ServiceProcess.ServiceBase
ServiceBase
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.
[void] OnStart([string[]]$args) { Write-Host "Service started" }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)