Powershell在非互动模式 [英] Powershell in NonInteractive mode

查看:1223
本文介绍了Powershell在非互动模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用八达通进行部署。我有一个Powershell脚本控制部署问题:

 #停止运行进程
$ processes = @(Notepad,
Firefox)
foreach($ process in $ processes)
{
$ prc = Get-Process -Name $ process -ErrorAction SilentlyContinue
if(-not($ prc -eq $ null))
{
写入主机停止$ prc.ProcessName
Stop-Process -InputObject $ prc -ErrorAction SilentlyContinue
}
}

我试图停止的程序不是你看到的程序上面的脚本,但它们代表我想要做的。现在我遇到的问题是,它在一个服务器上运行良好,但不能在另一个服务器上运行。它不起作用,我收到错误信息:



停止进程:Windows PowerShell处于非互斥模式。阅读和提示功能不可用。



可在Powershell 3.0上运行的脚本,Powershell 3.0上不起作用。因为旧的服务器运行Windows Server 2003,我无法升级到Powershell 3.0。如何使其在PS 2.0上运行?

解决方案

运行 -Force



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



由于CB在注释中建议: -confirm:$ false 也应该工作。其理由如下: -Confirm 是一个开关参数。如果使用尾部冒号和值指定参数,则开关参数只能接受参数。


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?

解决方案

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.

这篇关于Powershell在非互动模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆