在C#代码运行PSCmdLets(Citrix XenDesktop的) [英] Run PSCmdLets in C# code (Citrix XenDesktop)

查看:346
本文介绍了在C#代码运行PSCmdLets(Citrix XenDesktop的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的PowerShell并运行PowerShell的CMD-允许在C#。具体来说,我想要使用思杰的XenDesktop SDK编写一个Web应用程序来管理我们的XenDesktop环境。



就像一个快速测试,我就到Citrix参考BrokerSnapIn.dll,它看起来像它给了我很好的C#类。然而,当我打.Invoke与此错误消息:
。从PSCmdlet衍生的cmdlet不能直接援引



我已经搜查,并尝试了一堆东​​西,但不知道该怎么称呼PSCmdlets。我有点想离开,我必须使用字符串和一个运行空间/管道等,要做到这一点。



在先进的感谢,
NB

 使用系统; 
使用System.Management.Automation;使用System.Management.Automation.Runspaces
;
使用Citrix.Broker.Admin.SDK;

命名空间CitrixPowerShellSpike
{
类节目
{
静态无效的主要(字串[] args)
{
变种Ç =新GetBrokerCatalogCommand {AdminAddress =xendesktop.domain.com};
变种结果= c.Invoke();
Console.WriteLine(全部完成);
到Console.ReadLine();
}
}
}


解决方案

您需要举办PowerShell引擎,以执行PSCmdlet例如: (从 MSDN文档

  //调用PowerShell.Create()方法创建一个
//空的管道。
PowerShell的PS = PowerShell.Create();

//调用PowerShell.AddCommand(String)方法来添加
//将Get-Process cmdlet来的管道。做
//之前或之后cmdlet名称
//因为这将导致命令失败不能包含空格。
ps.AddCommand(获取进程);

Console.WriteLine(进程ID);
Console.WriteLine(----------------------------);

//调用PowerShell.Invoke()方法来运行
管道//命令。
的foreach(PSObject结果ps.Invoke())
{
Console.WriteLine(
{0,-24} {1},
的结果。 。各位[ProcessName]值,
result.Members [ID]值)。
}
}


I'm new to PowerShell and running PowerShell cmd-lets in C#. Specifically, I'm trying to use Citrix's XenDesktop SDK to write a web app to manage our XenDesktop environment.

Just as a quick test, I made a reference to the Citrix BrokerSnapIn.dll, which looks like it gives me good C# classes. However, when I hit the .Invoke with this error message: "Cmdlets derived from PSCmdlet cannot be invoked directly."

I've searched and tried a bunch of stuff, but don't know how to call PSCmdlets. I'm kinda left thinking that I have to use strings and a runspace/pipeline, etc, to do this.

Thanks In Advanced, NB

using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using Citrix.Broker.Admin.SDK;

namespace CitrixPowerShellSpike
{
    class Program
    {
        static void Main(string[] args)
        {
            var c = new GetBrokerCatalogCommand {AdminAddress = "xendesktop.domain.com"};
            var results = c.Invoke();
            Console.WriteLine("all done");
            Console.ReadLine();
        }
    }
}

解决方案

You need to host the PowerShell engine in order to execute a PSCmdlet e.g. (from the MSDN docs):

  // Call the PowerShell.Create() method to create an 
  // empty pipeline.
  PowerShell ps = PowerShell.Create();

  // Call the PowerShell.AddCommand(string) method to add 
  // the Get-Process cmdlet to the pipeline. Do 
  // not include spaces before or after the cmdlet name 
  // because that will cause the command to fail.
  ps.AddCommand("Get-Process");

  Console.WriteLine("Process                 Id");
  Console.WriteLine("----------------------------");

  // Call the PowerShell.Invoke() method to run the 
  // commands of the pipeline.
  foreach (PSObject result in ps.Invoke())
  {
    Console.WriteLine(
            "{0,-24}{1}",
            result.Members["ProcessName"].Value,
            result.Members["Id"].Value);
  } 
} 

这篇关于在C#代码运行PSCmdLets(Citrix XenDesktop的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆