有没有办法抑制PowerShell脚本中的任何交互式提示?


13

在编写PowerShell脚本时,我注意到当某些cmdlet遇到问题时,它们会显示一个交互式提示,以非空目录上的Remove-Item为例。在尝试自动执行任务时,这是致命的,我宁可采取的行动只是失败并抛出异常或返回错误的返回码,以使整个脚本不会被锁定以等待响应。

有什么方法可以强制PowerShell自动失败,而不是寻求用户对操作的输入?


我认为您应该重命名“如何使我Remove-Item快速失败?”这个问题。
杰伊·巴祖兹

1
这不是专门关于Remove-Item的,它只是一个说明性示例。在适当的条件下,还有许多其他cmdlet将阻止等待用户输入。
2013年

Answers:


5

Eris提出的解决方案有效地启动了另一个PowerShell实例。使用更简单的语法执行此操作的另一种方法是将shell封装到powershell.exe的另一个实例中。

powershell.exe -NonInteractive -Command "Remove-Item 'D:\Temp\t'"

2
我真的很喜欢这个解决方案,但是它带来了噩梦,那就是在TRY / CATCH之前用TSQL编写可靠的存储过程,这实际上需要围绕每个带有错误检测样板的查询。
Chuu

4

Get-Help about_Preference_Variables

$ ConfirmPreference
------------------
    确定Windows PowerShell是否自动提示您输入
    在运行cmdlet或函数之前进行确认。

...

无:Windows PowerShell不会自动提示。
                         要请求确认特定命令,请使用
                         cmdlet或函数的Confirm参数。

所以:

> $ConfirmPreference = 'None'

我发现要查看必须解决的问题。$ ConfirmPreference ='低'
Guy Thomas

这似乎不起作用。将$ ConfirmPreference设置为“ None”或“ Low”后,目录上的Remove-Item仍会提示您
Chuu

适用于Remove-ADUser(Server 2012R)
velkoon '19

2

好的,这确实很丑陋,但是芥末把它弄脏了。

问题:

  • 我没有想出如何在第一个错误后继续
  • 在这种情况下,它将删除纯文件,然后在第一个文件夹停止
  • 如果添加UseTransaction参数,我还没有弄清楚如何使此cmdlet工作
  • 这仅适用于简单情况(在当前环境下不会做很多事情的命令)。我没有测试过任何复杂的东西

    $MyPS = [Powershell]::Create()
    $MyPS.Commands.AddCommand("Remove-Item")
    $MyPS.Commands.AddParameter("Path", "D:\Temp\t")
    $MyPS.Invoke()

输出:

Commands                                                                                                                 
--------                                                                                                                 
{Remove-Item}                                                                                                            
{Remove-Item}                                                                                                            
Exception calling "Invoke" with "0" argument(s): "A command that prompts the user failed because the host program or the 
command type does not support user interaction. The host was attempting to request confirmation with the following 
message: The item at D:\Temp\t has children and the Recurse parameter was not specified. If you continue, all children 
will be removed with the item. Are you sure you want to continue?"
At line:19 char:1
+ $MyPS.Invoke()
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CmdletInvocationException

我没有将其标记为解决方案的原因是我还没有时间进行测试。除了作者以外,是否有人部署或测试过此代码?
Chuu 2013年

0

当我创建目录时,上述所有解决方案均对我失败,这意味着系统提示我确定脚本创建的每个目录-很多。对我有用的是敲打| 空出以便将结果通过管道传递到空

New-Item -ItemType Directory -Path $directory -Force:$true | Out-Null

请注意,$ Directory是具有要创建目录的完整路径的字符串。希望这可以节省一些时间:P


-1

我建议两种技巧

a)追加 -force

b)追加 -errorAction silently continue

这就是我研究哪些cmdlet支持特定参数的方式

Get-Command | where { $_.parameters.keys -contains "erroraction"}

我走这条路的原因之一是,我通常想做与-force相反的事情,即-alwaysbackoff之类的事情。我发现大多数支持-force的命令似乎都没有退缩的选择。
Chuu

1
顺便说一句,一个方便的缩写-errorAction SilentlyContinue-ea 0
杰伊·巴祖兹
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.