为什么我的powershell脚本不能运行?
我写了一个简单的batch file作为PowerShell脚本,我得到错误时,他们运行。
它在我的path中的脚本目录中。
Cannot be loaded because the execution of scripts is disabled on this system. please see "get-help about-signing". 我看着帮助,但它不是帮助。
可能是PowerShell的默认安全级别,(IIRC)只能运行签名脚本。
尝试input这个:
 set-executionpolicy remotesigned 
这将告诉PowerShell允许本地(即在本地驱动器上)未签名的脚本运行。
然后尝试再次执行您的脚本。
你需要运行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. 
希望这可以帮助!
 Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process 
 总是使用上面的cmd来启用在当前会话中执行powershell。 
我可以通过像这样调用powershell来绕过这个错误:
 powershell -executionpolicy bypass -File .\MYSCRIPT.ps1 
 也就是说,我添加了-executionpolicy bypass到我调用脚本的方式。 
这在Windows 7 Service Pack 1上工作。我是PowerShell的新手,所以可能会有这样的警告,我不知道。
[编辑2017-06-26]我已经继续在其他系统上使用这种技术,包括Windows 10和Windows 2012r2没有问题。
这是我现在使用的。 这使我不会意外地通过点击运行脚本。 当我在调度程序中运行它时,我添加了一个参数:“调度程序”,并绕过提示。
这也终止了窗口,所以我可以看到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% 
 另外值得一提的是,您可能需要在脚本名称前join.\ 。 例如: 
 .\scriptname.ps1 
 Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process 
即使发生以下错误,上述命令也适用于我:
 Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. 
 命令set-executionpolicy unrestricted将允许您创build的任何脚本以login用户身份运行。 只要确保在注销之前使用set-executionpolicy signed命令将执行策略设置恢复为带符号。