【问题标题】:Run Powershell from C#从 C# 运行 Powershell
【发布时间】:2014-03-18 06:44:57
【问题描述】:

我需要从 C# 启动一个 powershell 脚本并在 pipeline.Invoke() 上获取 PSSSecurityException

AuthorizationManager 检查失败。

我的代码:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();
    Command scriptCommand = new Command(scriptfile);
    pipeline.Commands.Add(scriptCommand);
    pipeline.Invoke();
}

问题

  1. 我怀疑我需要设置 PSCredential。但是我不能提示它,所以我必须在代码中处理它。这可以以安全的方式完成吗? (事实并非如此

【问题讨论】:

    标签: c# powershell runspace


    【解决方案1】:

    查看这篇超级用户帖子:https://superuser.com/questions/106360/how-to-enable-execution-of-powershell-scripts

    您可能只需要允许未签名的脚本运行。在 PS 控制台中,键入以下内容:

    set-executionpolicy remotesigned
    

    另一个资源与此相呼应:http://tgnp.me/2011/09/powershell-authorizationmanager-check-failed-resolution/

    试一试,让我知道会发生什么。

    要捕获脚本的输出,试试这个:

    Collection output = pipeline.Invoke();
    foreach (PSObject psObject in output)
    {
        <here you can ToString the psObject for the output and write it line by line to your log>
    }
    

    【讨论】:

    • 感谢您的输入,我可以使用关于打印输出的部分,关于“AuthorizationManager 检查失败”的部分。与我的环境的另一个问题有关。我现在已经弄清楚了,并将为遇到相同问题的其他人发布解决方案。
    【解决方案2】:

    问题是脚本被放置在一个共享上。要在此共享上运行脚本,我需要将该共享添加到受信任的站点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-30
      • 2021-09-11
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 2020-11-24
      • 2014-07-23
      相关资源
      最近更新 更多