windows 默认情况下,Powershell 脚本不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3980332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Powershell scripts don't work by default
提问by C Johnson
So I wanted to get started with writing a powershell script. I wanted to start small and simply write a script that substiutes a local file folder as a drive. Pretty simple. I've done it before using the old DOS command tools.
所以我想开始编写一个 powershell 脚本。我想从小处开始,简单地编写一个脚本,将本地文件夹替换为驱动器。很简单。我在使用旧的 DOS 命令工具之前已经完成了。
So the script I wanted to write is:
所以我想写的脚本是:
subst d: G:\CER
Ok, so I try to execute the script in the Powershell ISE by pressing the 'Play' button (i.e. F5)
好的,所以我尝试通过按“播放”按钮(即 F5)在 Powershell ISE 中执行脚本
So what do I get?
那我能得到什么?
File G:\CER\Make_Stage.ps1 cannot be loaded because the execution of scripts is disabledon this system. Please see "get-help about_signing" for more details. At line:0 char:0
无法加载文件 G:\CER\Make_Stage.ps1,因为在此系统上禁用了脚本的执行。有关更多详细信息,请参阅“get-help about_signing”。在行:0 字符:0
How stupid is this, that out of the box execution of powershell scripts are disabled! i.e. By default! Simply incredible that by default this new tool simply does notwork. Powershell has to be the stupidest scripting shell out there.
这是多么愚蠢,powershell 脚本的开箱即用执行被禁用!即默认!简直难以置信,默认情况下这个新工具根本无法正常工作。Powershell 必须是最愚蠢的脚本 shell。
So apparently I have to register something somewhere (hopefully not with MS). If anyone has an answer as to how to fix this, I'd appreciate it.
所以显然我必须在某处注册一些东西(希望不是用 MS)。如果有人对如何解决此问题有答案,我将不胜感激。
回答by Jay Bazuzi
To enable scripting, open a powershell prompt as Administrator and run:
要启用脚本,请以管理员身份打开 powershell 提示符并运行:
Set-ExecutionPolicy RemoteSigned
Here's a blog post from Lee that talks about PowerShell's security principles (http://www.leeholmes.com/blog/2005/08/16/demonstration-of-monads-security-features/).
这是 Lee 的一篇关于 PowerShell 安全原则的博客文章 ( http://www.leeholmes.com/blog/2005/08/16/demonstration-of-monads-security-features/)。
Keep in mind that most computer will never run a PowerShell script.
请记住,大多数计算机永远不会运行 PowerShell 脚本。
回答by driis
To fix this, execute:
要解决此问题,请执行:
set-executionpolicy remotesigned
In an elevated PowerShell prompt.
在提升的 PowerShell 提示符中。
回答by C Johnson
Found it:
找到了:
From the help docs (deeep in the help docs)
来自帮助文档(深入帮助文档)
LONG DESCRIPTION The Restricted execution policy does not permit any scripts to run.
详细说明受限执行策略不允许运行任何脚本。
A long article ensues on how to set an execution policy. I'm not impressed so far with PS.
接下来是一篇关于如何设置执行策略的长文章。到目前为止,我对 PS 印象不深。