|
|
@ -6,6 +6,7 @@
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
|
|
|
|
#Requires -Module Ansible.ModuleUtils.Legacy
|
|
|
|
#Requires -Module Ansible.ModuleUtils.Legacy
|
|
|
|
|
|
|
|
#Requires -Module Ansible.ModuleUtils.CommandUtil
|
|
|
|
|
|
|
|
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
|
|
|
|
|
|
|
@ -35,8 +36,6 @@ if (($appParameters -ne $null) -and ($appParametersFree -ne $null))
|
|
|
|
Fail-Json $result "Use either app_parameters or app_parameteres_free_form, but not both"
|
|
|
|
Fail-Json $result "Use either app_parameters or app_parameteres_free_form, but not both"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#abstract the calling of nssm because some PowerShell environments
|
|
|
|
|
|
|
|
#mishandle its stdout(which is Unicode) as UTF8
|
|
|
|
|
|
|
|
Function Nssm-Invoke
|
|
|
|
Function Nssm-Invoke
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[CmdletBinding()]
|
|
|
|
[CmdletBinding()]
|
|
|
@ -44,21 +43,10 @@ Function Nssm-Invoke
|
|
|
|
[Parameter(Mandatory=$true)]
|
|
|
|
[Parameter(Mandatory=$true)]
|
|
|
|
[string]$cmd
|
|
|
|
[string]$cmd
|
|
|
|
)
|
|
|
|
)
|
|
|
|
Try {
|
|
|
|
|
|
|
|
$encodingWas = [System.Console]::OutputEncoding
|
|
|
|
|
|
|
|
[System.Console]::OutputEncoding = [System.Text.Encoding]::Unicode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$nssmOutput = invoke-expression "nssm $cmd"
|
|
|
|
$nssm_result = Run-Command -command "nssm $cmd"
|
|
|
|
return $nssmOutput
|
|
|
|
|
|
|
|
}
|
|
|
|
return $nssm_result
|
|
|
|
Catch {
|
|
|
|
|
|
|
|
$ErrorMessage = $_.Exception.Message
|
|
|
|
|
|
|
|
Fail-Json $result "an exception occurred when invoking NSSM: $ErrorMessage"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Finally {
|
|
|
|
|
|
|
|
# Set the console encoding back to what it was
|
|
|
|
|
|
|
|
[System.Console]::OutputEncoding = $encodingWas
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Function Service-Exists
|
|
|
|
Function Service-Exists
|
|
|
@ -84,15 +72,15 @@ Function Nssm-Remove
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if ((Get-Service -Name $name).Status -ne "Stopped") {
|
|
|
|
if ((Get-Service -Name $name).Status -ne "Stopped") {
|
|
|
|
$cmd = "stop ""$name"""
|
|
|
|
$cmd = "stop ""$name"""
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$cmd = "remove ""$name"" confirm"
|
|
|
|
$cmd = "remove ""$name"" confirm"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error removing service ""$name"""
|
|
|
|
Throw "Error removing service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -122,12 +110,12 @@ Function Nssm-Install
|
|
|
|
|
|
|
|
|
|
|
|
if (!(Service-Exists -name $name))
|
|
|
|
if (!(Service-Exists -name $name))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$results = Nssm-Invoke "install ""$name"" ""$application"""
|
|
|
|
$nssm_result = Nssm-Invoke "install ""$name"" ""$application"""
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error installing service ""$name"""
|
|
|
|
Throw "Error installing service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -135,25 +123,25 @@ Function Nssm-Install
|
|
|
|
$result.changed = $true
|
|
|
|
$result.changed = $true
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
$results = Nssm-Invoke "get ""$name"" Application"
|
|
|
|
$nssm_result = Nssm-Invoke "get ""$name"" Application"
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error installing service ""$name"""
|
|
|
|
Throw "Error installing service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($results -cnotlike $application)
|
|
|
|
if ($nssm_result.stdout -cnotlike $application)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$cmd = "set ""$name"" Application ""$application"""
|
|
|
|
$cmd = "set ""$name"" Application ""$application"""
|
|
|
|
|
|
|
|
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error installing service ""$name"""
|
|
|
|
Throw "Error installing service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$result.application = "$application"
|
|
|
|
$result.application = "$application"
|
|
|
@ -166,14 +154,14 @@ Function Nssm-Install
|
|
|
|
if ($result.changed)
|
|
|
|
if ($result.changed)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$applicationPath = (Get-Item $application).DirectoryName
|
|
|
|
$applicationPath = (Get-Item $application).DirectoryName
|
|
|
|
$cmd = "nssm set ""$name"" AppDirectory ""$applicationPath"""
|
|
|
|
$cmd = "set ""$name"" AppDirectory ""$applicationPath"""
|
|
|
|
|
|
|
|
|
|
|
|
$results = invoke-expression $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error installing service ""$name"""
|
|
|
|
Throw "Error installing service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -207,12 +195,12 @@ Function Nssm-Update-AppParameters
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
$cmd = "get ""$name"" AppParameters"
|
|
|
|
$cmd = "get ""$name"" AppParameters"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error updating AppParameters for service ""$name"""
|
|
|
|
Throw "Error updating AppParameters for service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -250,7 +238,7 @@ Function Nssm-Update-AppParameters
|
|
|
|
$result.nssm_app_parameters = $appParameters
|
|
|
|
$result.nssm_app_parameters = $appParameters
|
|
|
|
$result.nssm_single_line_app_parameters = $singleLineParams
|
|
|
|
$result.nssm_single_line_app_parameters = $singleLineParams
|
|
|
|
|
|
|
|
|
|
|
|
if ($results -ne $singleLineParams)
|
|
|
|
if ($nssm_result.stdout -ne $singleLineParams)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if ($appParameters -or $appParametersFree)
|
|
|
|
if ($appParameters -or $appParametersFree)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -258,12 +246,12 @@ Function Nssm-Update-AppParameters
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
$cmd = "set ""$name"" AppParameters '""""'"
|
|
|
|
$cmd = "set ""$name"" AppParameters '""""'"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error updating AppParameters for service ""$name"""
|
|
|
|
Throw "Error updating AppParameters for service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -283,16 +271,16 @@ Function Nssm-Set-Output-Files
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
$cmd = "get ""$name"" AppStdout"
|
|
|
|
$cmd = "get ""$name"" AppStdout"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error retrieving existing stdout file for service ""$name"""
|
|
|
|
Throw "Error retrieving existing stdout file for service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($results -cnotlike $stdout)
|
|
|
|
if ($nssm_result.stdout -cnotlike $stdout)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!$stdout)
|
|
|
|
if (!$stdout)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -301,12 +289,12 @@ Function Nssm-Set-Output-Files
|
|
|
|
$cmd = "set ""$name"" AppStdout $stdout"
|
|
|
|
$cmd = "set ""$name"" AppStdout $stdout"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error setting stdout file for service ""$name"""
|
|
|
|
Throw "Error setting stdout file for service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -315,36 +303,36 @@ Function Nssm-Set-Output-Files
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$cmd = "get ""$name"" AppStderr"
|
|
|
|
$cmd = "get ""$name"" AppStderr"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error retrieving existing stderr file for service ""$name"""
|
|
|
|
Throw "Error retrieving existing stderr file for service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($results -cnotlike $stderr)
|
|
|
|
if ($nssm_result.stdout -cnotlike $stderr)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!$stderr)
|
|
|
|
if (!$stderr)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$cmd = "reset ""$name"" AppStderr"
|
|
|
|
$cmd = "reset ""$name"" AppStderr"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error clearing stderr file setting for service ""$name"""
|
|
|
|
Throw "Error clearing stderr file setting for service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
$cmd = "set ""$name"" AppStderr $stderr"
|
|
|
|
$cmd = "set ""$name"" AppStderr $stderr"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error setting stderr file for service ""$name"""
|
|
|
|
Throw "Error setting stderr file for service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -359,27 +347,27 @@ Function Nssm-Set-Output-Files
|
|
|
|
|
|
|
|
|
|
|
|
#set files to overwrite
|
|
|
|
#set files to overwrite
|
|
|
|
$cmd = "set ""$name"" AppStdoutCreationDisposition 2"
|
|
|
|
$cmd = "set ""$name"" AppStdoutCreationDisposition 2"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
$cmd = "set ""$name"" AppStderrCreationDisposition 2"
|
|
|
|
$cmd = "set ""$name"" AppStderrCreationDisposition 2"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
#enable file rotation
|
|
|
|
#enable file rotation
|
|
|
|
$cmd = "set ""$name"" AppRotateFiles 1"
|
|
|
|
$cmd = "set ""$name"" AppRotateFiles 1"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
#don't rotate until the service restarts
|
|
|
|
#don't rotate until the service restarts
|
|
|
|
$cmd = "set ""$name"" AppRotateOnline 0"
|
|
|
|
$cmd = "set ""$name"" AppRotateOnline 0"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
#both of the below conditions must be met before rotation will happen
|
|
|
|
#both of the below conditions must be met before rotation will happen
|
|
|
|
#minimum age before rotating
|
|
|
|
#minimum age before rotating
|
|
|
|
$cmd = "set ""$name"" AppRotateSeconds 86400"
|
|
|
|
$cmd = "set ""$name"" AppRotateSeconds 86400"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
#minimum size before rotating
|
|
|
|
#minimum size before rotating
|
|
|
|
$cmd = "set ""$name"" AppRotateBytes 104858"
|
|
|
|
$cmd = "set ""$name"" AppRotateBytes 104858"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Function Nssm-Update-Credentials
|
|
|
|
Function Nssm-Update-Credentials
|
|
|
@ -395,12 +383,12 @@ Function Nssm-Update-Credentials
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
$cmd = "get ""$name"" ObjectName"
|
|
|
|
$cmd = "get ""$name"" ObjectName"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error updating credentials for service ""$name"""
|
|
|
|
Throw "Error updating credentials for service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -414,14 +402,14 @@ Function Nssm-Update-Credentials
|
|
|
|
$fullUser = ".\" + $user
|
|
|
|
$fullUser = ".\" + $user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
If ($results -ne $fullUser) {
|
|
|
|
If ($nssm_result.stdout -ne $fullUser) {
|
|
|
|
$cmd = "set ""$name"" ObjectName $fullUser '$password'"
|
|
|
|
$cmd = "set ""$name"" ObjectName $fullUser '$password'"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error updating credentials for service ""$name"""
|
|
|
|
Throw "Error updating credentials for service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -443,23 +431,23 @@ Function Nssm-Update-Dependencies
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
$cmd = "get ""$name"" DependOnService"
|
|
|
|
$cmd = "get ""$name"" DependOnService"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error updating dependencies for service ""$name"""
|
|
|
|
Throw "Error updating dependencies for service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
If (($dependencies) -and ($results.Tolower() -ne $dependencies.Tolower())) {
|
|
|
|
If (($dependencies) -and ($nssm_result.stdout.Tolower() -ne $dependencies.Tolower())) {
|
|
|
|
$cmd = "set ""$name"" DependOnService $dependencies"
|
|
|
|
$cmd = "set ""$name"" DependOnService $dependencies"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error updating dependencies for service ""$name"""
|
|
|
|
Throw "Error updating dependencies for service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -479,25 +467,25 @@ Function Nssm-Update-StartMode
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
$cmd = "get ""$name"" Start"
|
|
|
|
$cmd = "get ""$name"" Start"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error updating start mode for service ""$name"""
|
|
|
|
Throw "Error updating start mode for service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$modes=@{"auto" = "SERVICE_AUTO_START"; "delayed" = "SERVICE_DELAYED_AUTO_START"; "manual" = "SERVICE_DEMAND_START"; "disabled" = "SERVICE_DISABLED"}
|
|
|
|
$modes=@{"auto" = "SERVICE_AUTO_START"; "delayed" = "SERVICE_DELAYED_AUTO_START"; "manual" = "SERVICE_DEMAND_START"; "disabled" = "SERVICE_DISABLED"}
|
|
|
|
$mappedMode = $modes.$mode
|
|
|
|
$mappedMode = $modes.$mode
|
|
|
|
if ($results -cnotlike $mappedMode) {
|
|
|
|
if ($nssm_result.stdout -cnotlike $mappedMode) {
|
|
|
|
$cmd = "set ""$name"" Start $mappedMode"
|
|
|
|
$cmd = "set ""$name"" Start $mappedMode"
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error updating start mode for service ""$name"""
|
|
|
|
Throw "Error updating start mode for service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -515,9 +503,9 @@ Function Nssm-Get-Status
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
$cmd = "status ""$name"""
|
|
|
|
$cmd = "status ""$name"""
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
return ,$results
|
|
|
|
return $nssm_result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Function Nssm-Start
|
|
|
|
Function Nssm-Start
|
|
|
@ -530,14 +518,14 @@ Function Nssm-Start
|
|
|
|
|
|
|
|
|
|
|
|
$currentStatus = Nssm-Get-Status -name $name
|
|
|
|
$currentStatus = Nssm-Get-Status -name $name
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($currentStatus.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $currentStatus.stderr
|
|
|
|
Throw "Error starting service ""$name"""
|
|
|
|
Throw "Error starting service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch ($currentStatus)
|
|
|
|
switch ($currentStatus.stdout)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
"SERVICE_RUNNING" { <# Nothing to do #> }
|
|
|
|
"SERVICE_RUNNING" { <# Nothing to do #> }
|
|
|
|
"SERVICE_STOPPED" { Nssm-Start-Service-Command -name $name }
|
|
|
|
"SERVICE_STOPPED" { Nssm-Start-Service-Command -name $name }
|
|
|
@ -560,12 +548,12 @@ Function Nssm-Start-Service-Command
|
|
|
|
|
|
|
|
|
|
|
|
$cmd = "start ""$name"""
|
|
|
|
$cmd = "start ""$name"""
|
|
|
|
|
|
|
|
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error starting service ""$name"""
|
|
|
|
Throw "Error starting service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -583,12 +571,12 @@ Function Nssm-Stop-Service-Command
|
|
|
|
|
|
|
|
|
|
|
|
$cmd = "stop ""$name"""
|
|
|
|
$cmd = "stop ""$name"""
|
|
|
|
|
|
|
|
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error stopping service ""$name"""
|
|
|
|
Throw "Error stopping service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -606,23 +594,23 @@ Function Nssm-Stop
|
|
|
|
|
|
|
|
|
|
|
|
$currentStatus = Nssm-Get-Status -name $name
|
|
|
|
$currentStatus = Nssm-Get-Status -name $name
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($currentStatus.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $currentStatus.stderr
|
|
|
|
Throw "Error stopping service ""$name"""
|
|
|
|
Throw "Error stopping service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($currentStatus -ne "SERVICE_STOPPED")
|
|
|
|
if ($currentStatus.stdout -ne "SERVICE_STOPPED")
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$cmd = "stop ""$name"""
|
|
|
|
$cmd = "stop ""$name"""
|
|
|
|
|
|
|
|
|
|
|
|
$results = Nssm-Invoke $cmd
|
|
|
|
$nssm_result = Nssm-Invoke $cmd
|
|
|
|
|
|
|
|
|
|
|
|
if ($LastExitCode -ne 0)
|
|
|
|
if ($nssm_result.rc -ne 0)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_cmd = $cmd
|
|
|
|
$result.nssm_error_log = "$results"
|
|
|
|
$result.nssm_error_log = $nssm_result.stderr
|
|
|
|
Throw "Error stopping service ""$name"""
|
|
|
|
Throw "Error stopping service ""$name"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|