【问题标题】:Why does PowerShell ISE asks to confirm when an executable outputs to stderr?为什么 PowerShell ISE 要求确认可执行文件何时输出到 stderr?
【发布时间】:2018-04-27 17:27:15
【问题描述】:

我正在 ISE 中调试一个 powershell 脚本,并在那里调用 sysinternals pskill 实用程序。

由于某种原因,PowerShell 会打开以下对话框:

为什么?如何将其配置为不显示对话框,而只是按照我的意图运行脚本?

编辑 1

问题很容易重现:

  1. 运行 ise -noprofile
  2. 在 ise 命令提示符内运行 pskill -h

结果如下:

虽然没有对话框,但有错误输出,我想这是我在其他场景中出现对话框的原因。那么,问题是错误输出的原因是什么?

似乎 pskill 将其帮助屏幕的一部分发送到 stderr,确实,观察:

C:\Dayforce\utils> .\pskill.exe -h

PsKill v1.15 - Terminates processes on local or remote systems
Copyright (C) 1999-2012  Mark Russinovich
Sysinternals - www.sysinternals.com

Usage: pskill [-t] [\\computer [-u username [-p password]]] <process ID | name>
     -t    Kill the process and its descendants.
     -u    Specifies optional user name for login to
           remote computer.
     -p    Specifies optional password for user name. If you omit this
           you will be prompted to enter a hidden password.

C:\Dayforce\utils> .\pskill.exe -h 2> $null
Usage: pskill [-t] [\\computer [-u username [-p password]]] <process ID | name>
     -t    Kill the process and its descendants.
     -u    Specifies optional user name for login to
           remote computer.
     -p    Specifies optional password for user name. If you omit this
           you will be prompted to enter a hidden password.

C:\Dayforce\utils>

所以,我似乎只遇到了 pskill 的问题,因为它输出到 stderr 并且 ISE 认为这是打开对话框的好理由。我用 -nobanner 命令行选项修复它。

但是一般如何解决呢?如果命令输出 stderr,我不希望 ISE 打开任何对话框,这本身可能不是一个好习惯,但并不意味着有错误。

【问题讨论】:

  • 添加-Confirm:$false 以禁止命令。 Set-PSDebug -Offwill turn off debugging features.
  • 什么调试功能?我确实想要调试脚本的能力。在哪里添加-Confirm:$false

标签: powershell stderr powershell-ise


【解决方案1】:

为了在您使用Write-Debug 时取消确认框,请在脚本/函数的顶部插入this

If ($PSBoundParameters['Debug']) {
    $DebugPreference = 'Continue'
}

【讨论】:

  • 但我没有使用 Write-Debug。让我发布一个展示所描述行为的最小脚本。
  • 但我猜你的建议会消除对话框,会检查它。
  • 我编辑了问题以专注于真正的问题 - 在任何标准错误上打开对话框。
猜你喜欢
  • 1970-01-01
  • 2018-12-22
  • 2012-09-14
  • 2019-09-04
  • 1970-01-01
  • 2021-01-03
  • 2012-07-09
  • 1970-01-01
相关资源
最近更新 更多