问题描述
我正在尝试使用 powershell 脚本在网络驱动器上创建一个新目录,但它一直提示我
I'm trying to create a new directory on a network drive using a powershell script but it keeps prompting me
确认:您确定要执行此操作吗...."
有没有办法覆盖它,这样它就不会问我,因为我是从网络界面运行这个脚本的.
Is there a way to override this so it doesn't ask me since I'm running this script from a web interface.
这是我的电话:
New-Item $rollbackDirectory -type Directory -Force
它做同样的事情,有或没有 -Force
参数
It does the same thing, with or without the -Force
parameter
我也试过这种格式,但没有运气
I've also tried this format with no luck
New-Item -name $rollbackName -itemtype directory -path $rollbackdrive -Debug -Force
推荐答案
-confirm
仅当您希望 cmdlet 提示您确认时才需要指定.cmdlet 本身是否会提示确认取决于 cmdlet 的开发人员,他们可以根据其效果为 cmdlet 设置高、中、低.根据 $ConfirmPreference
的值,您将自动获得 cmdlet 的确认.$ConfirmPreference
的默认值为高,New-Item
的级别设置为中等.因此,如果 New-Item
提示确认,则 $ConfirmPreference
值必须已更改为中或低.
-confirm
need only be specified when you want the cmdlet to prompt you for confirmation. Whether the cmdlet by itself would prompt for confirmation or not depends on the developer of the cmdlet who can set a high, medium, low for the cmdlet based on its effect. Based on the value of $ConfirmPreference
you will get the confirmation automatically for a cmdlet. The default value for $ConfirmPreference
is high and the level set for New-Item
is medium. So if the New-Item
is prompting for confirmation, the $ConfirmPreference
value must have been changed to medium or low.
使用 $ConfirmPreference="high"
甚至 $ConfirmPreference="none"
更改它以使 New-Item
不提示,或您的 -confirm:$false
解决方案也可以通过覆盖 $ConfirmPreference
来工作.
Change it using $ConfirmPreference="high"
or even $ConfirmPreference="none"
to make New-Item
not prompt, or your solution of -confirm:$false
works as well by overriding the $ConfirmPreference
.
在这里完美解释:http://blogs.msdn.com/b/powershell/archive/2006/12/15/confirmpreference.aspx
希望这能解决问题.
这篇关于Powershell 新项目:如何自动接受确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!