Search code examples
c#powershellcitrixxenapp

Citrix 7.6 Broker SDK C#/Powershell


Has anyone worked with the Citrix 7.6 BrokerSession SDK? I can't figure out how to execute a command like this for example:

GetBrokerSessionCommand getCmd = new GetBrokerSessionCommand();
getCmd.AdminAddress = "citrixServer:80";
var result = getCmd.Invoke();

This gives me an error message saying: "Cmdlets derived from PSCmdlet cannot be invoked directly.

In the earlier 6.5 SDK I could do like this:

string[] servers = new string[] { };
GetXAWorkerGroupByName workerGroup = new GetXAWorkerGroupByName();
workerGroup.WorkerGroupName = new string[] { workerGroupName };
workerGroup.ComputerName = XenAppController;
foreach (XAWorkerGroup _workerGroup in CitrixRunspaceFactory.DefaultRunspace.ExecuteCommand(workerGroup))
                {
                    servers = _workerGroup.ServerNames;
                }

            return servers;

But now the CitrixRunspaceFactory no longer exists? I want to avoid executing the command with the Powershell class and Powershell.Create() for the simple reason of handling exceptions in a simpler way.


Solution

  • Citrix 7.6 cmdlets derived not from Cmdlet class but from PSCmdlet. So they much more binded to the PowerShell engine and must be invoked inside it:

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

    AFAIK good times of strongly typed classes in Citrix SDK are gone.