无法加载ps1,因为此系统上禁用了运行脚本 [英] ps1 cannot be loaded because running scripts is disabled on this system

查看:92
本文介绍了无法加载ps1,因为此系统上禁用了运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从c#运行 powershell 脚本。

I try to run powershell script from c#.

首先我设置 ExecutionPolicy Unrestricted ,脚本现在从 PowerShell ISE 运行。

First i set the ExecutionPolicy to Unrestricted and the script is running now from PowerShell ISE.

现在这是c#我的代码:

Now this is c# my code:

class Program
{
    private static PowerShell ps;
    static void Main(string[] args)
    {
        ps = PowerShell.Create();
        string ps1File = Path.Combine(Environment.CurrentDirectory, "script.ps1");
        ExecuteScript(ps1File);
        Console.ReadLine();
    }

    static void ExecuteScript(string script)
    {
        try
        {
            ps.AddScript(script);
            Collection<PSObject> results = ps.Invoke();
            Console.WriteLine("Output:");
            foreach (var psObject in results)
            {
                Console.WriteLine(psObject);
            }
            Console.WriteLine("Non-terminating errors:");
            foreach (ErrorRecord err in ps.Streams.Error)
            {
                Console.WriteLine(err.ToString());
            }
        }
        catch (RuntimeException ex)
        {
            Console.WriteLine("Terminating error:");
            Console.WriteLine(ex.Message);
        }
    }
}

输出为:


ps1无法加载,因为在此
系统上禁用了运行脚本。有关更多信息,请参见
的about_Execution_Policies http://go.microsoft.com/fwlink/ ?LinkID = 135170


推荐答案

这可能是由于当前用户具有未定义的 ExecutionPolicy

This could be due to the current user having an undefined ExecutionPolicy.

您可以尝试以下操作:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

这篇关于无法加载ps1,因为此系统上禁用了运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆