Octopus.Script exported 2017-05-12 by 1FastSTi belongs to ‘Windows’ category.
Create Windows Service using NSSM and powershell script.
Visit https://nssm.cc/usage for more information.
NOTE: This site may be blocked due to .cc domain
Parameters
When steps based on the template are included in a project’s deployment process, the parameters below can be set.
Step to stop service added as first step?
ServiceStopStepAdded = False
Ensure that a step to stop the service added as first step
ServiceName
ServiceNameValue =
[Required] Service Name
DisplayName
ServiceDisplayNameValue
Display Name for the service
StartupType
StartupTypeValue = SERVICE_AUTO_START
StartupType (check https://nssm.cc/usage for the Valid values)
Description
ServiceDescriptionValue
Description
Service Executable
ServiceExecutableValue =
[Required] Path of the executable to run as Windows Service
Service Executable Arguments
serviceExecutableArgsValue
Any arguments to be passed to Service Executable
Service App Directory
ServiceAppDirectoryValue =
[Required] Directory path of the Service Executable
Service User Account
serviceUserAccountValue =
[Required] User Account to run Service as.
Service User Password
serviceUserPasswordValue =
[Required] Password for the User Account to run service as.
Depends On
DependsOnValue
Any services or service groups which must be started before the the service can run. (check https://nssm.cc/usage for more info)
Service Error Log File
serviceErrorLogFileValue
Capture error log messages generated by the application
Service Output Log File
serviceOutputLogFileValue
Capture output log messages generated by the application
NSSM Executable
NSSMExecutableValue = .\nssm.exe
Allows you to override the NSSM.exe installation location.
Script body
Steps based on this template will execute the following PowerShell script.
$serviceStopStepAdded = $OctopusParameters['ServiceStopStepAdded']
$serviceName = $OctopusParameters['ServiceNameValue']
$displayName = $OctopusParameters['ServiceDisplayNameValue']
$startupType = $OctopusParameters['StartupTypeValue']
$description = $OctopusParameters['ServiceDescriptionValue']
$serviceExecutable = $OctopusParameters['ServiceExecutableValue']
$serviceExecutableArgs = $OctopusParameters['serviceExecutableArgsValue']
$serviceAppDirectory = $OctopusParameters['ServiceAppDirectoryValue']
$serviceUserAccount = $OctopusParameters['serviceUserAccountValue']
$serviceUserPassword = $OctopusParameters['serviceUserPasswordValue']
$dependsOn = $OctopusParameters['DependsOnValue']
$serviceErrorLogFile = $OctopusParameters['serviceErrorLogFileValue']
$serviceOutputLogFile = $OctopusParameters['serviceOutputLogFileValue']
$nssmExecutable = $OctopusParameters['NSSMExecutableValue']
if($serviceStopStepAdded -ne 'True'){
Write-Host Please add a step to stop the windows service as the first step!
Write-Host If already added, make sure to check the checkbox - Step to stop service added as first step? - in NSSM Windows Service Setup
return
}
Write-Host Installing service $serviceName -foreground "green"
Write-Host "NSSM path" $serviceAppDirectory
Write-Host $serviceName
Write-Host $serviceExecutable
Write-Host $serviceExecutableArgs
Write-Host $serviceAppDirectory
Write-Host $serviceErrorLogFile
Write-Host $serviceOutputLogFile
Write-Host $serviceUserAccount
Write-Host $serviceUserPassword
push-location
Set-Location $serviceAppDirectory
$service = Get-Service $serviceName -ErrorAction SilentlyContinue
if($service) {
Write-host service $service.Name is $service.Status
Write-Host Removing $serviceName service
if($service.Status -ne 'Stopped'){
&$nssmExecutable stop $serviceName
}
&$nssmExecutable remove $serviceName confirm
}
Write-Host Installing $serviceName as a service
&$nssmExecutable install $serviceName $serviceExecutable $serviceExecutableArgs
if($displayName){
&$nssmExecutable set $serviceName DisplayName $displayName
}
if($startupType){
&$nssmExecutable set $serviceName Start $startupType
}
if($description){
&$nssmExecutable set $serviceName Description $description
}
if($dependsOn){
&$nssmExecutable set $serviceName DependOnService $dependsOn
}
# setting log file
if($serviceErrorLogFile){
&$nssmExecutable set $serviceName AppStderr $serviceErrorLogFile
&$nssmExecutable set $serviceName AppStderrCreationDisposition 2
}
if($serviceOutputLogFile){
&$nssmExecutable set $serviceName AppStdout $serviceOutputLogFile
&$nssmExecutable set $serviceName AppStdoutCreationDisposition 2
}
# setting app directory
if($serviceAppDirectory) {
Write-host setting app directory to $serviceAppDirectory -foreground "green"
&$nssmExecutable set $serviceName AppDirectory $serviceAppDirectory
}
# setting user account
if($serviceUserAccount -And $serviceUserPassword) {
&$nssmExecutable set $serviceName ObjectName $serviceUserAccount $serviceUserPassword
}
#start service right away
&$nssmExecutable start $serviceName
pop-location
Provided under the Apache License version 2.0.
To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.
{
"Id": "fc54e757-fbd9-40b1-98fa-7b2e16c649de",
"Name": "NSSM Windows Service - Create",
"Description": "Create Windows Service using NSSM and powershell script. \n\nVisit https://nssm.cc/usage for more information.\n\nNOTE: This site may be blocked due to .cc domain",
"Version": 2,
"ExportedAt": "2017-05-12T22:50:16.405Z",
"ActionType": "Octopus.Script",
"Author": "1FastSTi",
"Parameters": [
{
"Name": "ServiceStopStepAdded",
"Label": "Step to stop service added as first step?",
"HelpText": "Ensure that a step to stop the service added as first step",
"DefaultValue": "False",
"DisplaySettings": {
"Octopus.ControlType": "Checkbox"
}
},
{
"Name": "ServiceNameValue",
"Label": "ServiceName",
"HelpText": "[Required] Service Name",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "ServiceDisplayNameValue",
"Label": "DisplayName",
"HelpText": "Display Name for the service",
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "StartupTypeValue",
"Label": "StartupType",
"HelpText": "StartupType (check https://nssm.cc/usage for the Valid values)",
"DefaultValue": "SERVICE_AUTO_START",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "ServiceDescriptionValue",
"Label": "Description",
"HelpText": "Description",
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "ServiceExecutableValue",
"Label": "Service Executable",
"HelpText": "[Required] Path of the executable to run as Windows Service",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "serviceExecutableArgsValue",
"Label": "Service Executable Arguments",
"HelpText": "Any arguments to be passed to Service Executable",
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "ServiceAppDirectoryValue",
"Label": "Service App Directory",
"HelpText": "[Required] Directory path of the Service Executable",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "serviceUserAccountValue",
"Label": "Service User Account",
"HelpText": "[Required] User Account to run Service as.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "serviceUserPasswordValue",
"Label": "Service User Password",
"HelpText": "[Required] Password for the User Account to run service as.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "Sensitive"
}
},
{
"Name": "DependsOnValue",
"Label": "Depends On",
"HelpText": "Any services or service groups which must be started before the the service can run.\n(check https://nssm.cc/usage for more info)",
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "MultiLineText"
}
},
{
"Name": "serviceErrorLogFileValue",
"Label": "Service Error Log File",
"HelpText": "Capture error log messages generated by the application",
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "serviceOutputLogFileValue",
"Label": "Service Output Log File",
"HelpText": "Capture output log messages generated by the application",
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "NSSMExecutableValue",
"Label": "NSSM Executable",
"HelpText": "Allows you to override the NSSM.exe installation location.",
"DefaultValue": ".\\nssm.exe",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
}
],
"Properties": {
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.RunOnServer": "false",
"Octopus.Action.Script.ScriptBody": "$serviceStopStepAdded = $OctopusParameters['ServiceStopStepAdded']\n$serviceName = $OctopusParameters['ServiceNameValue']\n$displayName = $OctopusParameters['ServiceDisplayNameValue']\n$startupType = $OctopusParameters['StartupTypeValue']\n$description = $OctopusParameters['ServiceDescriptionValue']\n$serviceExecutable = $OctopusParameters['ServiceExecutableValue']\n$serviceExecutableArgs = $OctopusParameters['serviceExecutableArgsValue']\n$serviceAppDirectory = $OctopusParameters['ServiceAppDirectoryValue']\n$serviceUserAccount = $OctopusParameters['serviceUserAccountValue']\n$serviceUserPassword = $OctopusParameters['serviceUserPasswordValue']\n$dependsOn = $OctopusParameters['DependsOnValue']\n$serviceErrorLogFile = $OctopusParameters['serviceErrorLogFileValue']\n$serviceOutputLogFile = $OctopusParameters['serviceOutputLogFileValue']\n$nssmExecutable = $OctopusParameters['NSSMExecutableValue']\n\nif($serviceStopStepAdded -ne 'True'){\n Write-Host Please add a step to stop the windows service as the first step!\n Write-Host If already added, make sure to check the checkbox - Step to stop service added as first step? - in NSSM Windows Service Setup\n return\n}\n\nWrite-Host Installing service $serviceName -foreground \"green\"\nWrite-Host \"NSSM path\" $serviceAppDirectory\nWrite-Host $serviceName\nWrite-Host $serviceExecutable\nWrite-Host $serviceExecutableArgs\nWrite-Host $serviceAppDirectory\nWrite-Host $serviceErrorLogFile\nWrite-Host $serviceOutputLogFile\nWrite-Host $serviceUserAccount\nWrite-Host $serviceUserPassword\n\npush-location\nSet-Location $serviceAppDirectory\n\n$service = Get-Service $serviceName -ErrorAction SilentlyContinue\n\nif($service) {\n Write-host service $service.Name is $service.Status\n Write-Host Removing $serviceName service \n if($service.Status -ne 'Stopped'){\n &$nssmExecutable stop $serviceName\n }\n &$nssmExecutable remove $serviceName confirm\n}\n\nWrite-Host Installing $serviceName as a service\n&$nssmExecutable install $serviceName $serviceExecutable $serviceExecutableArgs\n\nif($displayName){\n &$nssmExecutable set $serviceName DisplayName $displayName\n} \n\nif($startupType){\n &$nssmExecutable set $serviceName Start $startupType\n}\n\nif($description){\n &$nssmExecutable set $serviceName Description $description\n}\n\nif($dependsOn){\n &$nssmExecutable set $serviceName DependOnService $dependsOn\n}\n\n# setting log file \nif($serviceErrorLogFile){\n &$nssmExecutable set $serviceName AppStderr $serviceErrorLogFile\n &$nssmExecutable set $serviceName AppStderrCreationDisposition 2\n}\n\nif($serviceOutputLogFile){\n &$nssmExecutable set $serviceName AppStdout $serviceOutputLogFile\n &$nssmExecutable set $serviceName AppStdoutCreationDisposition 2\n}\n\n# setting app directory\nif($serviceAppDirectory) {\n Write-host setting app directory to $serviceAppDirectory -foreground \"green\"\n &$nssmExecutable set $serviceName AppDirectory $serviceAppDirectory\n}\n\n# setting user account\nif($serviceUserAccount -And $serviceUserPassword) {\n &$nssmExecutable set $serviceName ObjectName $serviceUserAccount $serviceUserPassword\n}\n\n#start service right away\n&$nssmExecutable start $serviceName\npop-location",
"Octopus.Action.Script.ScriptFileName": "",
"Octopus.Action.Package.FeedId": "",
"Octopus.Action.Package.PackageId": ""
},
"Category": "Windows",
"HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/nssm-windows-service-create.json",
"Website": "/step-templates/fc54e757-fbd9-40b1-98fa-7b2e16c649de",
"Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////Da3qSsLvhtb0wur6O7zuWcfxldv2aMzyK7ftpOD3s+X48Pr+0fD7d9HzHLLr4fX8xD/OcwAAAaNJREFUeNrs3cFygjAUQFECWott1f//2sJoW6kIKEzNs+euXOmcmSSGDa8oJEmSJEmSJGmsj1W1K9cpsGD1Vr2WdToVEPC+2lYvZfpVrEW0qZpF1F+MRdRugzoNlvkiarfBPk0pT8GhWUSX2yASpDlLr2+DEJBmEY1ug6whx7N0n2b30G1QlmmxHsRYp6X76yvF9vg5RYQczq8UVURI35UiFmTgShED0p6lI1eKzCHTrxS5Qk6PZ9PLDtJ9PIsJmXWlyAky6/dAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQMJCyjltF/iO3gpJUpD8s4OAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgID8T8itwwKyhbTdMr4ha8hXUwZqhICcOgyNOIkE+V5wo4MSgr1u/fp7poO+AL8K/gL8yw0UeyRB34m9iQ/pVD8L5JYTO3NI58R+AsiEEzsW5OfE3sUe/zRwYkeGnG2g2CPS7rhjF4GKP0ZwyoldxK37kFqEL/7wU0mSJEmSJOmJ+xRgAHxZTCXGdZkfAAAAAElFTkSuQmCC",
"$Meta": {
"Type": "ActionTemplate"
}
}
Page updated on Friday, May 12, 2017