[英]Unable to execute a powershell script from c#
我已经完成了以下代码来执行 powershell 脚本:
public static void LaunchCommand(string command, IList<String> arguments)
{
Console.WriteLine($"Launching powershell command: {command} {String.Join(" ", arguments)}");
using (PowerShell ps = PowerShell.Create())
{
// specify the script code to run.
ps.AddScript(command);
ps.Streams.Debug.DataAdded += OnDebugAdded;
ps.Streams.Information.DataAdded += OnInfoAdded;
ps.Streams.Warning.DataAdded += OnWarningAdded;
ps.Streams.Error.DataAdded += OnErrorAdded;
//Transformation into a non-generic version
ArrayList argumentsList = new ArrayList();
foreach (string argument in arguments)
{
argumentsList.Add(argument);
}
// specify the parameters to pass into the script.
ps.AddParameters(argumentsList);
Collection<PSObject> pipelineObjects = ps.Invoke();
// print the resulting pipeline objects to the console.
foreach (var item in pipelineObjects)
{
Console.WriteLine(item.BaseObject.ToString());
}
Console.WriteLine(ps.HadErrors+" -"+ps.HistoryString);
}
Console.WriteLine("Commaned finished");
}
(.DataAdded 只是写到console.writeline 的新消息)
但我打了这个:
Launching powershell command: F:\Dev\AAA\scripts/start.ps1 F:\Dev\AAA\scenarios/BBB/config.json
System.Management.Automation.PSSecurityException: File F:\Dev\AAA\scripts\start.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.
---> System.UnauthorizedAccessException: File F:\Dev\AAA\scripts\start.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.
--- End of inner exception stack trace ---
at System.Management.Automation.AuthorizationManager.ShouldRunInternal(CommandInfo commandInfo, CommandOrigin origin, PSHost host)
at System.Management.Automation.CommandDiscovery.ShouldRun(ExecutionContext context, PSHost host, CommandInfo commandInfo, CommandOrigin commandOrigin)
at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(CommandInfo commandInfo, CommandOrigin commandOrigin, Nullable`1 useLocalScope, SessionStateInternal sessionState)
at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)
at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)
at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionContext context)
at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
我搜索了很多,我以管理员身份执行了这个命令,在 x86 和 x64 命令提示符中:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
如果我自己执行这个(在命令行中复制粘贴)它可以工作,它只会在从 Visual Studio 启动时失败。
知道发生了什么吗?
我在 windows 10 pro(最新更新)+ VS2019(还有最新更新)。 我正在做的控制台应用程序在.Net Core 3.1 上。 我确实以我的简单普通用户身份运行 VS(最终程序也是如此)。
您可以在 power-shell object 中将执行策略设置为命令。
powershell.AddCommand("Set-ExecutionPolicy").AddArgument("Unrestricted")
.AddParameter("Scope","CurrentUser");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.