gpt4 book ai didi

c# - 无法加载ps1,因为此系统上禁用了运行脚本

转载 作者:行者123 更新时间:2023-12-03 00:05:36 25 4
gpt4 key购买 nike

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

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

现在这是C#我的代码:

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 cannot be loaded because running scripts is disabled on this system. For more informationm see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.

最佳答案

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

您可以尝试以下方法:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

关于c# - 无法加载ps1,因为此系统上禁用了运行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63662663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com