我将为您翻译以下内容,涉及IT技术:
我编写了一个简单的批处理文件作为PowerShell脚本,但在运行时出现错误。
它在我的路径中的一个脚本目录中。 这是我得到的错误:
无法加载,因为在此系统上禁用了脚本执行。 请参阅“get-help about-signing”。
我查看了帮助,但一无所获。
我编写了一个简单的批处理文件作为PowerShell脚本,但在运行时出现错误。
它在我的路径中的一个脚本目录中。 这是我得到的错误:
无法加载,因为在此系统上禁用了脚本执行。 请参阅“get-help about-signing”。
我查看了帮助,但一无所获。
可能是 PowerShell 的默认安全级别,据我所知,它只会运行已签名的脚本。
尝试输入以下内容:
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
始终使用上述命令在当前会话中启用执行 PowerShell。
powershell -executionpolicy bypass -File .\MYSCRIPT.ps1
也就是说,我在调用脚本的方式中添加了-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%
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.
.\
。例如:.\scriptname.ps1
set-executionpolicy unrestricted
将允许您创建的任何脚本以已登录用户身份运行。但请确保在注销之前使用set-executionpolicy signed
命令将执行策略设置回已签名状态。type file.ps1 | powershell -command -
或者在 PowerShell 中:
gc file.ps1|powershell -c -