Im trying to install a topshelf service using powershell but am really struggling to get powershell to run the installer.
Function EnsureTopshelfService([string]$serviceName, [string]$servicePath){
$service = get-service $serviceName -ErrorAction SilentlyContinue
if ($service –ne $null){
"$serviceName is already installed on this server";
}
else{
Write-Host "Installing $serviceName...";
#the problem is here
& "`"$servicepath`" install --sudo"
}
}
When i run this command i get the following
Installing test... & : The term '"c:\A Test\Test.exe" install --sudo' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Users\luke.mcgregor\Documents\Test.ps1:11 char:11 + & "
"$servicepath
" install --sudo" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: ("c:\A Test\Test.exe" install --sudo:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
Running "c:\A Test\Test.exe" install --sudo
at a command prompt works fine so its an issue with how im calling off to an existing program. Does anyone know where I'm going wrong here? I'm pretty new to powershell so I'm guessing its a pretty simple thing.
EDIT: The following is a working example of the above
Function EnsureTopshelfService([string]$serviceName, [string]$servicePath){
$service = get-service $serviceName -ErrorAction SilentlyContinue
if ($service –ne $null){
"$serviceName is already installed on this server";
}
else{
Write-Host "Installing $serviceName...";
& "$servicepath" install --sudo
}
}
You need to tokenize by delimiting with a space. So -
& "`"$servicepath`" install --sudo"
Should be 3 tokens -
& $servicepath install --sudo
The ampersand is the PowerShell call operator. Anything after it works the same as if you had typed it in a CMD.exe window. If a single token has a space in it, you'll need to quote it.
Check out more info about the call operator here.
If you don't like the (weird/crazy/take-your-pick) syntax, add a colon between the argument name and value.
So assuming you have the EXE built with topshelf at C:\Services\ItJustWorks\ItJustWorks.exe
then you can run the following from powershell:
PS C:\Services\ItJustWorks> .\ItJustWorks.exe install -servicename:"ItJustWorks"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With