0

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
PMental
  • 1,091
  • 6
  • 12
MCaston
  • 1
  • 2
  • Do both `(Get-Command nssm).Source` and `Get-Command nssm` work correctly on the 2012 server? – PMental Nov 23 '20 at 17:11
  • I guess not. Get-Command nssm works. Thanks a lot... What's the difference between the two? – MCaston Nov 23 '20 at 17:46
  • You're probably seeing differences due to different PowerShell versions. Either adopt the script and simply get the path another way, or upgrade PowerShell to 5.1 on the server: https://www.microsoft.com/en-us/download/details.aspx?id=54616 – PMental Nov 23 '20 at 19:27
  • Actually it seems Get-Command when forced to string as it probably is that might work as well, maybe that's what you meant, it works as is with just `Get-Command nssm`? – PMental Nov 23 '20 at 19:37
  • Yes, Worked as Get-Command nssm – MCaston Nov 24 '20 at 11:10

0 Answers0