Powershell in NonInteractive mode

2019-04-19 02:06发布

I use Octopus for our deployments. I have a problem with one of the Powershell scripts to control the deployment:

# 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
    }
}

The programs I try to stop are not the ones you see in the script above, but they represent what I am trying to do. Now the problem I have with it, is that it works well on one server, but not on another. Where it does not work, I get the error message:

Stop-Process : Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available.

The script that works runs on Powershell 3.0, the one that does not work on Powershell 2.0. I cannot upgrade to Powershell 3.0 everywhere yet because the old servers run with Windows Server 2003. How can I make it work on PS 2.0?

1条回答
啃猪蹄的小仙女
2楼-- · 2019-04-19 02:53

Run with -Force:

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

As C.B. suggested in the comment: -confirm:$false should also work. Rationale for this is as follows: -Confirm is a switch parameter. Switch parameters can only take arguments if you specify the parameter with a trailing colon and a value.

查看更多
登录 后发表回答