Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c# PowerShell runspace woes

Posted on 2016-09-27
5
Medium Priority
?
226 Views
1 Endorsement
Last Modified: 2016-09-28
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,
1
Comment
Question by:Dustin Saunders
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 41819788
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..
0
 
LVL 13

Author Comment

by:Dustin Saunders
ID: 41819854
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)
0
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 41819897
-Args should work.. Try..
Powershell.exe {Write-host $Args[0] $Args[1]} -Args $message,$message1

Or

Powershell.exe {Param($message,$Message1) Write-host $Message $Message1} -Args $message,$message1

Open in new window

0
 
LVL 13

Author Closing Comment

by:Dustin Saunders
ID: 41819940
Hey, that was incredibly helpful.  Thanks, greatly appreciated!
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41819963
happy to help!.. :-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question