4.0.18

Support for Packaging Scripts as Services

Only available in PowerShell Pro Tools

You can now package PowerShell scripts as services. The services can self install\uninstall and execute under the Service Control Manager. PowerShell services can be helpful for processes that need to continue to run during start up and shut down of a machine such as web servers like Universal Dashboard or Polaris.

OnStart and OnStop function need to be defined in the entry point of the package. These functions will be called when starting or stopping the service.

# Service1 Service Entry Point
<#
This function is called when the service is started. Once this function returns,
your service will be set to a Running state.
#>
function OnStart() {
"Running" | Out-File C:\users\adamr\Desktop\test.txt
}
<#
This function is called when the service is started. Once this function returns,
your service will be set to a Stoppe state and the process will terminate.
#>
function OnStop() {
Remove-Item C:\users\adamr\Desktop\test.txt -Force
}

In Visual Studio, you can select the Output Type in the Project Properties.

Output Type for Packaging

The service name and display name can be set in the Service Properties tab.

Service Properties Tab

Using the PowerShell Pro Tools module, you can use Merge-Script and a package config file to configure service packaging.

package.psd1
@{
Root = 'c:\Users\adamr\Source\Repos\ExampleScript\Service1.ps1'
OutputPath = 'c:\Users\adamr\Source\Repos\ExampleScript\out'
Package = @{
Enabled = $true
Obfuscate = $false
HideConsoleWindow = $false
DotNetVersion = 'v4.6.2'
FileVersion = '1.0.0'
FileDescription = ''
ProductName = ''
ProductVersion = ''
Copyright = ''
RequireElevation = $false
ApplicationIconPath = ''
PackageType = 'Service'
ServiceName = 'PowerShellService'
ServiceDisplayName = 'PowerShellService'
}
Bundle = @{
Enabled = $true
Modules = $true
# IgnoredModules = @()
}
}
Merge-Script -ConfigFile .\package.psd1

The PowerShell Pro Tools VS Code extension has been updated to add these properties automatically in the Package as Executable command.

Managing your service install

From an elevated command prompt, you can install your service by specifying the --install flag.

MyService.exe --install

To uninstall the service, specify the --uninstall flag.

MyService.exe --uninstall