Powershell in NonInteractive mode

本小妞迷上赌 提交于 2019-12-06 17:07:41

问题


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:


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.



来源:https://stackoverflow.com/questions/16580723/powershell-in-noninteractive-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!