首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C#代码中运行PSCmdLets (Citrix XenDesktop)

在C#代码中运行PSCmdLets (Citrix XenDesktop)
EN

Stack Overflow用户
提问于 2012-10-03 23:52:17
回答 1查看 3.6K关注 0票数 2

我刚接触PowerShell,并且在C#中运行PowerShell cmd-let。具体地说,我正在尝试使用Citrix的XenDesktop开发工具包来编写一个web应用程序来管理我们的XenDesktop环境。

作为一个快速测试,我引用了Citrix BrokerSnapIn.dll,它看起来给了我很好的C#类。但是,当我使用以下错误消息访问.Invoke时:“无法直接调用从PSCmdlet派生的Cmdlet。”

我搜索并尝试了很多东西,但不知道如何调用PSCmdlets。我有点想,我必须使用字符串和运行空间/管道等来做这件事。

感谢高级,NB

代码语言:javascript
复制
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();
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-04 00:27:02

您需要托管PowerShell引擎才能执行PSCmdlet,例如(从MSDN docs):

代码语言:javascript
复制
  // 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);
  } 
} 
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12712196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档