Search code examples
c#powershellcitrix

Executing 3rd Party Powershell SDK commands with C#


This is an incredibly frustrating problem that nobody else seems to have the answer to so I'm trying to break my question down into the simplest thing possible. Surely there are other 3rd party Powershell SDK's out there that people have tried to access and use via C#. Does anybody know why this would get an error saying the following?

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

The same goes for other commands that this PSSnapin provides at the command prompt.

I can type this command in manually at a powershell command prompt and it works. So do the other commands. What's the deal?

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

UPDATE:

This new code as suggested in the answer below gets a new error: '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());
                }
            }
        }

UPDATE 2:

I also get this same error above trying to set the execution policy.

enter image description here

enter image description here

UPDATE 3:

Fixed, see comments in answer below. The syntax of the parameters line was incorrect.


Solution

  • Use RunspaceConfiguration.AddPSSnapIn to add PSSnapin and then add a command:

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