我正在尝试在 .netCore 上使用 C# 运行 PowerShell 脚本。
我现在尝试了许多不同的解决方案,但似乎没有一个有效。我只想执行一个 PowerShell 脚本并设置 ExecutionPolicies 和 Scope 以使其工作。但我总是遇到一个例外,即 ExecutionPolicies 不允许我以这种方式运行脚本。
尽管有实际的配置和代码,你会在下面找到,在到达.Invoke();之后,我没有从调试器那里得到任何反馈。被执行。等待响应并让软件在后台执行其操作总是会导致 stackoverflow 异常。
commandParameters 是一个简单的 Dictionary<string, string>
对此有什么想法吗?
干杯。
var iss = InitialSessionState.CreateDefault2();
// Set its script-file execution policy.
iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
iss.ExecutionPolicy = (ExecutionPolicy)Microsoft.PowerShell.ExecutionPolicyScope.CurrentUser;
// Create a PowerShell instance with a runspace based on the
// initial session state.
PowerShell ps = PowerShell.Create(iss);
ps.AddCommand(path + "\\" + fileName);
ps.AddParameters(commandParameters);
var results = ps.InvokeAsync().GetAwaiter().GetResult();