使用C#执行第三方Powershell SDK命令

时间:2015-10-01 22:50:29

标签: c# powershell citrix

这是一个令人难以置信的令人沮丧的问题,似乎没有其他人能够得到答案,所以我试图将我的问题分解为最简单的问题。当然还有其他第三方Powershell SDK,人们试图通过C#访问和使用它们。有谁知道为什么会出现以下错误?

The term 'add-PSSnapin Citrix*.Admin.V* is not recognized as the name of a cmdlet, function, script file, or operable program.

此PSSnapin在命令提示符下提供的其他命令也是如此。

我可以在powershell命令提示符下手动输入此命令,但它可以正常工作。其他命令也是如此。这笔交易是什么?

public void testPS()
        {
            using (Runspace runspace = RunspaceFactory.CreateRunspace())
            {
                runspace.Open();
                PowerShell ps = PowerShell.Create();
                ps.Runspace = runspace;
                ps.AddCommand("add-PSSnapin Citrix.*.Admin.V*");
                ps.Invoke();
                //Commented sections below don't work either, same error. 
                //ps.AddCommand("Get-BrokerSession");
                //ps.AddParameter("AdminAddress");
                //ps.AddParameter("SERVERNAME");
                //ps.Invoke();
                //Collection<PSObject> psr = ps.Invoke();
                //foreach (PSObject x in psr)
                //{
                //    MessageBox.Show(x.ToString());
                //}
            }
        }

更新:

以下答案中建议的新代码会出现新错误:'System.Management.Automation.ParameterBindingException' occurred in System.Management.Automation.dll

public void testPS()
        {
            using (Runspace runspace = RunspaceFactory.CreateRunspace())
            {
                runspace.Open();
                PowerShell ps = PowerShell.Create();
                ps.Runspace = runspace;
                PSSnapInException psex;
                runspace.RunspaceConfiguration.AddPSSnapIn("Citrix.Broker.Admin.V2", out psex);
                Pipeline pipeline = runspace.CreatePipeline();

                Command getSession = new Command("Get-BrokerSession");
                getSession.Parameters.Add("AdminAddress");
                getSession.Parameters.Add("MYSERVERNAME");
                //also tried the above with this code
                //getSession.Parameters.Add("-AdminAddress MYSERVERNAME");
                // and
                //getSession.Parameters.Add("AdminAddress MYSERVERNAME");
                // and other methods as well
                pipeline.Commands.Add(getSession);

                //This line below is where the exception occurs.
                Collection<PSObject> output = pipeline.Invoke();

                foreach (PSObject x in output)
                {
                    MessageBox.Show(x.ToString());
                }
            }
        }

更新2:

在尝试设置执行策略时,我也遇到了同样的错误。

enter image description here

enter image description here

更新3:

已修复,请参阅下面的答案中的评论。参数行的语法不正确。

3 个答案:

答案 0 :(得分:1)

您需要确保从其参数中拆分命令。在你的情况下,它会是这样的:

ps.AddCommand("add-PSSnapin");

然后你总是可以将Citrix.*.Admin.V*作为上述命令的参数追加。

答案 1 :(得分:1)

使用RunspaceConfiguration.AddPSSnapIn添加PSSnapin,然后添加命令:

        Runspace runSpace = RunspaceFactory.CreateRunspace();
        runSpace.Open();
        PSSnapInException psex;
        runSpace.RunspaceConfiguration.AddPSSnapIn("Citrix.Broker.Admin.V2", out psex);
        Pipeline pipeline = runSpace.CreatePipeline();

        Command getSession = new Command("Get-BrokerSession");
        getSession.Parameters.Add("AdminAddress", "SERVERNAME");
        pipeline.Commands.Add(getSession);

        Collection<PSObject> output = pipeline.Invoke();

答案 2 :(得分:0)

对于那些仍然像我这样坚持的人,即使在最新的Microsoft.Powershell.Sdk 6.2.0 nuget包中,我也遇到了这个问题。我的一个同事通过执行以下操作最终使它开始工作:

    public string RunPowershell(string param1)
    {
        var outputString = "";

        var startInfo = new ProcessStartInfo
        {
            FileName = @"powershell.exe",
            Arguments = "C:\\path\\to\\script.ps1" + " -param1  " + param1,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        using (var process = new Process())
        {
            process.StartInfo = startInfo;
            process.Start();

            var output = process.StandardOutput.ReadToEnd();
            var errors = process.StandardError.ReadToEnd();

            if (!string.IsNullOrEmpty(output))
            {
                outputString = output;
            }

            if (!string.IsNullOrEmpty(errors))
            {
                Console.WriteLine($"Error: {errors}");
            }
        }

        return outputString;
    }

“ C:\ path \ to \ script.ps1”可以包含Add-PSSnapin命令,并且仍然可以正常运行。

我找不到为Microsoft.Powershell.Sdk提交的bug来处理此问题,但是如果有人创建了一个bug并将其链接到此处,谢谢!

希望这会有所帮助