【发布时间】:2020-12-01 06:48:59
【问题描述】:
我已经完成了以下代码来执行一个 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(最终程序也是如此)。
【问题讨论】:
-
发送命令设置执行策略怎么样。检查this。
-
@user1672994 哦?有用!似乎很奇怪的安全性,用户无法执行脚本但可以执行命令以允许它?无论如何,您可以将其添加为答案吗?
-
@JAN - 谢谢,添加为答案。
标签: c# .net powershell