简体   繁体   English

如何在PowerShell中安静地设置执行策略,而无需询问?

[英]How to set execution policy in PowerShell quietly, without asking?

I'm running the following command. 我正在运行以下命令。

Set-ExecutionPolicy -ExecutionPolicy Bypass

It asks me if I'm sure and if I yes it, it sets the policy just as supposed to. 它询问我是否确定,如果是,它将按预期设置策略。 I wonder, however, how I'm supposed to execute the command so that the computer doesn't ask me to confirm. 但是,我不知道如何执行该命令,以便计算机不要求我进行确认。

I've googled the issue and there's a flag -Confirm but using it produces an additional confirmation request. 我已经用Google搜索这个问题,并且有一个-Confirm标志,但是使用它会产生一个额外的确认请求。 What would be the opposite of it, ie something like this (quacky typo intended)? 它的反义词是什么(即类似这样的错误输入)?

Set-ExecutionPolicy -ExecutionPolicy Bypass -JustDoWhatWeTellYouForDucksSake

As @Briantist pointed out in this case you want to use the -Force switch to suppress the prompt. 正如@Briantist指出的,在这种情况下,您想使用-Force开关禁止显示提示。 However for future reference in most cases the correct syntax to remove the confirmation prompt from most cmdlets is -Confirm:$False 但是,为将来参考,在大多数情况下,从大多数cmdlet中删除确认提示的正确语法是-Confirm:$False

Here is one that is slightly better, as this one will work even if you do not have local administrator privileges. 这是一个稍好一点的方法,因为即使您没有本地管理员权限,该方法也可以使用。 However, it only applies to the current Powershell session. 但是,它仅适用于当前的Powershell会话。

Set-ExecutionPolicy Bypass -scope Process -Force

Or you can set the policy to unrestricted, but then whitelist whatever fileserver you're running a script from. 或者,您可以将策略设置为Unrestricted,然后将正在运行脚本的任何文件服务器列入白名单。 There's probably a gpo way to do this too. 也可能有一种gpo方式来执行此操作。 The New-ItemProperty -Type parameter isn't easily found in the docs. 在文档中不容易找到New-ItemProperty -Type参数。

new-item -path   'hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\stackoverflow.com\server' -force > $null
new-itemproperty 'hkcu:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\stackoverflow.com\server' * -type DWORD -force > $null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM