非交互模式下的Powershell

tob*_*bre 18 deployment powershell octopus-deploy

我使用Octopus进行部署.我有一个Powershell脚本的问题来控制部署:

# stops running processes
$processes = @("Notepad",
               "Firefox")
foreach ($process in $processes)
{
    $prc = Get-Process -Name $process -ErrorAction SilentlyContinue
    if (-not($prc -eq $null))
    {
        Write-Host "Stopping " $prc.ProcessName
        Stop-Process -InputObject $prc -ErrorAction SilentlyContinue
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图阻止的程序不是你在上面的脚本中看到的程序,但它们代表了我想要做的事情.现在我遇到的问题是,它在一台服务器上运行良好,但在另一台服务器上运行不正常.如果它不起作用,我收到错误消息:

停止处理:Windows PowerShell处于NonInteractive模式.读取和提示功能不可用.

适用的脚本在Powershell 3.0上运行,Powershell 3.0不适用于Powershell 2.0.我无法在任何地方升级到Powershell 3.0,因为旧服务器与Windows Server 2003一起运行.如何在PS 2.0上运行?

Dav*_*vic 19

运行-Force:

Stop-Process -InputObject $prc -ErrorAction SilentlyContinue -Force

正如CB在评论中建议的那样:-confirm:$false也应该有效.其基本原理如下:-Confirm是一个开关参数.如果使用尾部冒号和值指定参数,则切换参数只能接受参数.