I have a powershell script to create service that works on my local Windows 10 PC. Below works correctly.
$nssm = (Get-Command nssm).Source
$serviceName = 'ImageResize'
$powershell = (Get-Command powershell).Source
$scriptPath = 'C:\ImageTest\Scripts\current\ResizeImage.ps1'
$arguments = '-ExecutionPolicy Bypass -NoProfile -File "{0}"' -f $scriptPath
& $nssm install $serviceName $powershell $arguments
& $nssm status $serviceName
Start-Service $serviceName
Get-Service $serviceName
When trying same script on windows 2012 server I get errors. Transcript shows following:
The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script block, or a CommandInfo object.
At line:10 char:3
+ & $nssm install $serviceName $powershell $arguments
+ ~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : BadExpression
I also found the following in the Event Viewer:
Application
Failed to start service imagetest. Program C:\ImageResizeScript\ResizeImage.ps1 couldn't be launched. CreateProcess()` failed: imagetest is not a valid Win32 application.
System
The system cannot find the specified path