首页 文章

无法加载.ps1,因为在此系统上禁用了脚本的执行[重复]

提问于
浏览
95

这个问题在这里已有答案:

我运行此代码以从ASP.NET应用程序执行PowerShell代码:

System.Management.Automation.Runspaces.Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
runspace.Open();
System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(@"\\servername\path");

pipeline.Commands.Add("Out-String");

Collection<PSObject> results = pipeline.Invoke();

runspace.Close();

但我收到一个错误:

.ps1无法加载,因为在此系统上禁用了脚本的执行 . 有关详细信息,请参阅“get-help about_signing” .

从命令提示符或Windows(Windows窗体)应用程序运行相同的代码 .

5 回答

  • 1

    你需要运行 Set-ExecutionPolicy

    Set-ExecutionPolicy Unrestricted <-- Will allow unsigned PowerShell scripts to run.
    
    Set-ExecutionPolicy Restricted <-- Will not allow unsigned PowerShell scripts to run.
    
    Set-ExecutionPolicy RemoteSigned <-- Will allow only remotely signed PowerShell scripts to run.
    
  • 69

    由于execution policy,您的脚本无法执行 .

    您需要以管理员身份运行PowerShell,并在客户端PC上将其设置为Unrestricted . 您可以通过调用Invoke来执行此操作:

    Set-ExecutionPolicy Unrestricted
    
  • 119

    在某些情况下,您可以按照其他答案中建议的步骤进行操作,验证是否正确设置了执行策略,并且仍然使脚本失败 . 如果您遇到这种情况,您可能位于具有32位和64位版本PowerShell的64位计算机上,并且在未设置执行策略的版本上发生故障 . The setting does not apply to both versions ,所以你必须明确地设置它两次 .

    在Windows目录中查找System32和SysWOW64 .

    对每个目录重复这些步骤:

    • 导航到WindowsPowerShell \ v1.0并启动powershell.exe

    • 检查ExecutionPolicy的当前设置:

    Get-ExecutionPolicy -List

    • 为所需的级别和范围设置ExecutionPolicy,例如:

    Set-ExecutionPolicy -Scope LocalMachine Unrestricted

    请注意,您可能需要以管理员身份运行PowerShell,具体取决于您尝试为其设置策略的范围 .

    你可以在这里阅读更多内容:Running Windows PowerShell Scripts

  • 15

    问题是执行策略是基于每个用户设置的 . 每次运行它时,您都需要在应用程序中运行以下命令以使其工作:

    Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned
    

    可能有一种方法可以为ASP.NET用户设置此项,但这意味着您不会打开整个系统,只打开您的应用程序 .

    Source

  • 20

    我遇到了类似的问题,并注意到Windows Server 2012上的默认cmd运行的是x64 .

    对于Windows 7,Windows 8,Windows Server 2008 R2或Windows Server 2012,请以管理员身份运行以下命令:

    86

    打开C:\ Windows \ SysWOW64 \ cmd.exe运行命令:powershell Set-ExecutionPolicy RemoteSigned

    64位

    打开C:\ Windows \ system32 \ cmd.exe运行命令powershell Set-ExecutionPolicy RemoteSigned

    您可以使用检查模式

    在CMD中:echo%PROCESSOR_ARCHITECTURE%在Powershell中:[Environment] :: Is64BitProcess

    我希望这对你有帮助 .

相关问题