简体   繁体   English

为特定脚本全局设置 ExecutionPolicy

[英]Set-ExecutionPolicy gloablly for a specific script

Is it possible to Set-ExecutionPolicy for a specific script, globally?是否可以在全球范围内为特定脚本Set-ExecutionPolicy I know it is possible to open PowerShell and set a policy for a specific script like so:我知道可以打开PowerShell并为特定脚本设置策略,如下所示:

powershell.exe -ExecutionPolicy Bypass -File C:\Script\From\Internet.ps1

But I am trying to set the policy for that script globally so no matter how I open PowerShell , it'll have that policy set for that file.但我正在尝试为该脚本全局设置策略,因此无论我如何打开PowerShell ,它都会为该文件设置该策略。 That way if I open PowerShell from the Start menu, or if VS Code opens a PowerShell terminal, that script will always work/run.这样,如果我从“开始”菜单打开PowerShell ,或者如果 VS Code 打开PowerShell终端,该脚本将始终运行/运行。

The Set-ExecutionPolicy doesn't have a paramater for -File so I'm not sure how to set it globally for a specific file. Set-ExecutionPolicy没有-File的参数,所以我不确定如何为特定文件全局设置它。

No.不。

As documented, there is no concept of setting the EP at the script level.如文档所述,没有在脚本级别设置 EP 的概念。

You have to do it as you have already shown...你必须像你已经展示的那样去做......

powershell.exe -ExecutionPolicy Bypass -File C:\Script\From\Internet.ps1

... which btw is not at the script level (since there is no such thing), you are the default process level. ...顺便说一下,它不在脚本级别(因为没有这样的东西),你是默认的进程级别。

You can add it, the EP, to your profile(s)...您可以将它(EP)添加到您的个人资料中...

  • Microsoft.PowerShell_profile.ps1 Microsoft.PowerShell_profile.ps1
  • Microsoft.PowerShellISE_profile.ps1 Microsoft.PowerShellISE_profile.ps1
  • Microsoft.VSCode_profile.ps1 Microsoft.VSCode_profile.ps1

... but that applies to any PowerShell instance you start. ...但这适用于您启动的任何 PowerShell 实例。

Again, as documented, there are only 5 options (file/script is not one of them) : 同样,如文档所述,只有 5 个选项(文件/脚本不是其中之一)

  1. MachinePolicy机器策略
  2. UserPolicy用户策略
  3. Process过程
  4. CurrentUser当前用户
  5. LocalMachine本地机器

Your only option to get close to what you are after is to create a custom shortcut on your desktop using the command you have already shown and pin that to wherever you need it.接近您所追求的目标的唯一选择是使用您已经显示的命令在您的桌面上创建一个自定义快捷方式,并将其固定到您需要的任何位置。

The only real reason to change the EP is if the Machine EP is more restrictive (Restricted or AllSigned) enforce via GPO and you need to work outside that.更改 EP 的唯一真正原因是机器 EP 是否通过 GPO 执行更严格(受限或 AllSigned),而您需要在其之外工作。

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

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