98 lines
3.7 KiB
PowerShell
98 lines
3.7 KiB
PowerShell
param (
|
|
[string]$serviceName,
|
|
[string]$option,
|
|
[string]$appPath,
|
|
[string]$command, # just the command like run startadm or what ever you have in npm.
|
|
[string]$description
|
|
)
|
|
|
|
# Example string to run with the parameters in it.
|
|
# .\services.ps1 -serviceName "LSTV2" -option "install" -appPath "E:\LST\lstV2" -description "Logistics Support Tool V2" -command "run start"
|
|
|
|
|
|
### the fix
|
|
# .\services.ps1 -serviceName "LST-App" -option "delete" -appPath "E:\LST\lstV2" -description "Logistics Support Tool V2" -command "run start"
|
|
# .\services.ps1 -serviceName "LST-frontend" -option "delete" -appPath "E:\LST\lstV2" -description "Logistics Support Tool V2" -command "run start"
|
|
# .\services.ps1 -serviceName "LST-System" -option "delete" -appPath "E:\LST\lstV2" -description "Logistics Support Tool V2" -command "run start"
|
|
# .\services.ps1 -serviceName "LST-Gateway" -option "delete" -appPath "E:\LST\lstV2" -description "Logistics Support Tool V2" -command "run start"
|
|
|
|
# .\services.ps1 -serviceName "LST-App" -option "install" -appPath "E:\LST\lst_backend" -description "Logistics Support Tool V2" -command "run startapp"
|
|
# .\services.ps1 -serviceName "LST-frontend" -option "install" -appPath "E:\LST\lst_backend" -description "Logistics Support Tool V2" -command "run startfront"
|
|
|
|
|
|
|
|
$nssmPath = $AppPath + "\nssm.exe"
|
|
$npmPath = "C:\Program Files\nodejs\npm.cmd" # Path to npm.cmd
|
|
|
|
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
|
|
Write-Host "Error: This script must be run as Administrator."
|
|
exit 1
|
|
}
|
|
|
|
if(-not $serviceName -or -not $option){
|
|
Write-host "The service name or option is missing please enter one of them and try again."
|
|
exit 1
|
|
}
|
|
|
|
if ($option -eq "start"){
|
|
write-host "Starting $($serviceName)."
|
|
Start-Service $serviceName
|
|
}
|
|
|
|
if ($option -eq "stop"){
|
|
write-host "Stoping $($serviceName)."
|
|
Stop-Service $serviceName
|
|
}
|
|
|
|
if ($option -eq "restart"){
|
|
write-host "Stoping $($serviceName) to be restarted"
|
|
Stop-Service $serviceName
|
|
Start-Sleep 3 # so we give it enough time to fully stop
|
|
write-host "Starting $($serviceName)"
|
|
Start-Service $serviceName
|
|
}
|
|
|
|
if ($option -eq "delete"){
|
|
if(-not $appPath){
|
|
Write-host "The path to the app is missing please add it in and try again."
|
|
exit 1
|
|
}
|
|
& $nssmPath stop $serviceName
|
|
write-host "Removing $($serviceName)"
|
|
& $nssmPath remove $serviceName confirm
|
|
|
|
}
|
|
|
|
if($option -eq "install"){
|
|
if(-not $appPath -or -not $description -or -not $command){
|
|
Write-host "Please check all parameters are passed to install the app.."
|
|
exit 1
|
|
}
|
|
|
|
|
|
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
|
|
|
|
if(-not $service){
|
|
write-host $serviceName "is not installed we will install it now"
|
|
|
|
Write-Host "Installing $serviceName..."
|
|
& $nssmPath install $serviceName $npmPath $command
|
|
& $nssmPath set $serviceName AppDirectory $appPath
|
|
& $nssmPath set $serviceName Description $description
|
|
# Set recovery options
|
|
sc.exe failure $serviceName reset= 0 actions= restart/5000/restart/5000/restart/5000
|
|
& $nssmPath start $serviceName
|
|
}else{
|
|
write-host $serviceName "is already installed will push the updated info"
|
|
Write-Host "Updating $serviceName..."
|
|
& $nssmPath stop $serviceName
|
|
& $nssmPath set $serviceName AppDirectory $appPath
|
|
& $nssmPath set $serviceName Description $description
|
|
# Set recovery options
|
|
sc.exe failure $serviceName reset= 0 actions= restart/5000/restart/5000/restart/5000
|
|
Start-Sleep 4
|
|
& $nssmPath start $serviceName
|
|
}
|
|
}
|
|
|