Avatar of Dustin Saunders
Dustin Saunders
Flag for United States of America asked on

c# PowerShell runspace woes

I have the following code, and an issue with it.  Starting with the PowerShell block:
Import-Module RemoteDesktop
$ConnBroker = (Get-RDConnectionBrokerHighAvailability -ConnectionBroker "dm3us050.dm3.wizmoworks.net").ActiveManagementServer
Set-RDSessionHost -SessionHost $server -ConnectionBroker $ConnBroke -NewConnectionAllowed $action

Open in new window

And the following c# code:
        public static void RunPowershellCommand(string scriptPath, string[,] variables = null)
        {
            RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
            Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
            runspace.Open();
            RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
            //scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
            
            Pipeline pipeline = runspace.CreatePipeline();

            Command myCommand = new Command(scriptPath, false);

            if (variables[0, 0] != null)
            {
                for (int i = 0; i < variables.GetLength(0); i++)  //Add all the powershell variables we might need in the partner specific.
                {
                    runspace.SessionStateProxy.SetVariable(variables[i, 0], variables[i, 1]);
                }
            }

            pipeline.Commands.Add(myCommand);
            Collection<PSObject> psObjects;
            psObjects = pipeline.Invoke();
            runspace.Close();
        }

Open in new window


The powershell script works independently, but when I run it from the c# runspace I get the following error:
{"The term 'Get-RDConnectionBrokerHighAvailability' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."}

Any ideas why Powershell can't use this cmdlet when invoked via my c# app?

Thanks,
PowershellC#

Avatar of undefined
Last Comment
SubSun

8/22/2022 - Mon
SubSun

My two cents :
Check the version of PowerShell when you invoke it from you app. I have see similar issues with our orchestrator server .net activity tasks, as it invokes/executes it’s commands still in PowerShell 2.0 instead of PS version installed on the computer.

As a work around, we are running the script in a sub process, example..

$Result = PowerShell { <Script Here> }

Check and see if there is a difference when you try these commands inside your app..

PowerShell {$Host.Version}
$Host.Version


Or it's possible that the RemoteDesktop module file is not accessible for the user account which you use to run the application/script.

To check the access issue, copy the module folder from default module path C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ to C:\Users\<UserName>\Documents\WindowsPowerShell\Modules and try to run app..
Dustin Saunders

ASKER
That seems to work for executing the code, but it doesn't pass the variables through- is there a way I can dump the variables I pass to the script in the sub process? (doesn't look like I can add any -args)
ASKER CERTIFIED SOLUTION
SubSun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Dustin Saunders

ASKER
Hey, that was incredibly helpful.  Thanks, greatly appreciated!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
SubSun

happy to help!.. :-)