使用PowerShell安装topshelf服务

Not*_*ple 5 powershell

我试图使用powershell安装topshelf服务,但我真的很难让PowerShell运行安装程序.

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"
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行此命令时,我得到以下内容

安装测试...&:术语'"c:\ A Test\Test.exe"install --sudo'无法识别为cmdlet,函数,脚本文件或可运行程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.在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

运行"c:\A Test\Test.exe" install --sudo在命令提示符下正常工作,以便与IM如何调用关闭到现有程序的问题.有谁知道我在哪里出错?我对powershell很新,所以我猜它很简单.

编辑: 以下是上述的一个工作示例

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
    }
}
Run Code Online (Sandbox Code Playgroud)

And*_*ndi 6

您需要通过用空格分隔来标记化.所以 -

& "`"$servicepath`" install --sudo"
Run Code Online (Sandbox Code Playgroud)

应该是3个代币 -

& $servicepath install --sudo
Run Code Online (Sandbox Code Playgroud)

&符是PowerShell调用操作符.它之后的任何操作都与您在CMD.exe窗口中键入的内容相同.如果单个令牌中有空格,则需要引用它.

在此处查看有关呼叫运营商的更多信息.