Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hosting PowerShell core remoting in dot net core app? Has anyone made it work yet?

Tags:

c#

powershell

I've been trying for a month now to host powershell core remoting scripts, that use SSH protocol for remoting, in a dot net core application and so far nothing worked. One of my main issues is that AddScript method of system management automation seems to be doing absolutely nothing when you have Powershell remoting scripts in the block.

I was wondering if anyone has actually tried to host remoting scripts in dot net core C# application successfully?

I actually have an issue raised in the PowerShell github page, but no one is interested in it, hence the zero comments.

https://github.com/PowerShell/PowerShell/issues/7984

Thank you for your help.

like image 751
Justiii Avatar asked Oct 22 '18 15:10

Justiii


1 Answers

I might have found a simple workaround that I've tested and it works.

Basically instead of asking the don net application to call the remoting script via .AddScript method you ask the dot net app to call a local powershell process that executes the remoting script. The script file should be somewhere on the server you're executing the application from. To show this as a code I'm using the following.

string script  = "start-process pwsh-preview" -argument "path to script file"
using (Runspace runspace = RunspaceFactory.CreateRunspace())
using (Powershell powershell = Powershell.Create())
{
runspace.Open();
PSCommand command = new PSCommand();
command.AddScript(script);
 powershell.Commands = command;
powerhell.Runspace = runspace;
Collection<PSObject> results = new Collection<PSObject>();
results = powershell.Invoke();

I'm yet to test calling the script with parameters (not sure how to do it yet maybe someone here can help). As well as hosting in docker container the entire thing. Most likely PowerShell core will need to be installed as well as dot net core in the docker image for it to work.

Hope this help.

I'm still waiting for the GitHub PowerShell guys to look at the issue. I will post more information here once I test hosting this in docker.

like image 93
Justiii Avatar answered Nov 15 '22 20:11

Justiii