Answers:
Eris提出的解决方案有效地启动了另一个PowerShell实例。使用更简单的语法执行此操作的另一种方法是将shell封装到powershell.exe的另一个实例中。
powershell.exe -NonInteractive -Command "Remove-Item 'D:\Temp\t'"
见Get-Help about_Preference_Variables
:
$ ConfirmPreference ------------------ 确定Windows PowerShell是否自动提示您输入 在运行cmdlet或函数之前进行确认。 ... 无:Windows PowerShell不会自动提示。 要请求确认特定命令,请使用 cmdlet或函数的Confirm参数。
所以:
> $ConfirmPreference = 'None'
Remove-ADUser
(Server 2012R)
好的,这确实很丑陋,但是芥末把它弄脏了。
问题:
这仅适用于简单情况(在当前环境下不会做很多事情的命令)。我没有测试过任何复杂的东西
$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
Remove-Item
快速失败?”这个问题。