为什么我的PowerShell脚本没有运行?

Dev*_*ris 96 powershell

我写了一个简单的批处理文件作为PowerShell脚本,我在运行时遇到错误.

它位于我路径的脚本目录中.

__PRE__

我查看了帮助,但这并没有帮助.

Mat*_*ton 98

它可能是PowerShell的默认安全级别,(IIRC)只会运行签名脚本.

尝试输入:

set-executionpolicy remotesigned
Run Code Online (Sandbox Code Playgroud)

这将告诉PowerShell允许本地(即在本地驱动器上)运行未签名的脚本.

然后再次尝试执行脚本.

  • **这是一个相当可怕的答案.**例如,它永久地改变了Powershell的默认安全级别,可能是不受欢迎的(和不安全的)方式.另一方面,它甚至没有充分解释签名的远程脚本和未签名的本地脚本 - 但[Chocolatey](https://chocolatey.org)偶尔需要的_not_ unsigned远程脚本 - 将被授予执行权限.大多数用户可能需要[this](/sf/answers/1398248281/)和[this](/sf/answers/1129899991/). (12认同)
  • 您必须以管理员权限运行Powershell,至少在Windows 8下运行! (5认同)
  • 虽然塞西尔对答案中的一些弱点的担忧是公平的,但我想指出一些事情。1)他的两个链接似乎为我打开了同一页面。2)如果您希望脚本简单地运行,设置执行策略可能仍然是可行的方法,并且可能与OP无关:3)似乎最好的策略是启动脚本,在命令行中指定执行策略和范围运行脚本,并根据需要设置会话配置以进行登录。如果您希望 Windows 会话期间具有较高的安全性,但希望降低登录时的安全性,则需要对脚本顺序进行细致入微的处理。 (3认同)

Nad*_*_MK 72

你需要运行Set-ExecutionPolicy:

Set-ExecutionPolicy Restricted <-- Will not allow any powershell scripts to run.  Only individual commands may be run.

Set-ExecutionPolicy AllSigned <-- Will allow signed powershell scripts to run.

Set-ExecutionPolicy RemoteSigned <-- Allows unsigned local script and signed remote powershell scripts to run.

Set-ExecutionPolicy Unrestricted <-- Will allow unsigned powershell scripts to run.  Warns before running downloaded scripts.

Set-ExecutionPolicy Bypass <-- Nothing is blocked and there are no warnings or prompts.
Run Code Online (Sandbox Code Playgroud)

  • @MatthewLock`Restricted`是默认策略.[阅读更多](https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_execution_policies) (5认同)
  • Windows将新安装设置为默认的ExecutionPolicy值是什么? (3认同)

小智 20

使用:

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
Run Code Online (Sandbox Code Playgroud)

始终使用以上命令启用在当前会话中执行PowerShell.


Mic*_*ter 15

通过像这样调用PowerShell,我能够绕过这个错误:

powershell -executionpolicy bypass -File .\MYSCRIPT.ps1
Run Code Online (Sandbox Code Playgroud)

也就是说,我添加了-executionpolicy bypass我调用脚本的方式.

这适用于Windows 7 Service Pack 1.我是PowerShell的新手,因此我可能会注意到这一点,我不知道.

[编辑2017-06-26]我继续在其他系统上使用此技术,包括Windows 10和Windows 2012 R2,没有问题.

这是我现在使用的.这使我不会通过点击它意外地运行脚本.当我在调度程序中运行它时,我添加一个参数:"scheduler"并绕过提示符.

这也会在最后暂停窗口,以便我可以看到PowerShell的输出.

if NOT "%1" == "scheduler" (
   @echo looks like you started the script by clicking on it.
   @echo press space to continue or control C to exit.
   pause
)

C:
cd \Scripts

powershell -executionpolicy bypass -File .\rundps.ps1

set psexitcode=%errorlevel%

if NOT "%1" == "scheduler" (
   @echo Powershell finished.  Press space to exit.
   pause
)

exit /b %psexitcode%
Run Code Online (Sandbox Code Playgroud)


Leo*_*ick 5

另外值得知道您可能需要包含.\在脚本名称前面.例如:

.\scriptname.ps1
Run Code Online (Sandbox Code Playgroud)


小智 5

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
Run Code Online (Sandbox Code Playgroud)

即使出现以下错误,上面的命令也适用于我:

Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.
Run Code Online (Sandbox Code Playgroud)