gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 20:17:08 24 4
gpt4 key购买 nike

我正在尝试使用 Citrix 的 XenDesktop SDK 编写一个 Web 应用程序来管理我们的 XenDesktop 环境。

作为快速测试,我引用了 Citrix BrokerSnapIn.dll,它看起来为我提供了很好的 C# 类。但是,当我点击 .Invoke 并显示以下错误消息时:“无法直接调用从 PSCmdlet 派生的 Cmdlet。”

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

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();
}
}
}

最佳答案

您需要托管 PowerShell 引擎才能执行 PSCmdlet,例如(来自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# - 在 C# 代码中运行 PSCmdLets (Citrix XenDesktop),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12712196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com