9

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.

2 Answers 2

1

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.

1
  • I have similar requirements, has anyone made it work with parameters?
    – Prema Arya
    Aug 2, 2019 at 6:39
0

I struggled getting PS scripts to work through my app as well and made it work just a like a week ago.

Here I launch a script on a remote computer to make a Windows Toast Notification to pop up. You might have to download and install the Powershell.SDK NuGet package for the PowerShell scripts to work.

enter image description here

Here you can see I also get the output generated from the script. For some reason Verbose output was not captured even though verbose preference was set in the script. I had to capture the output by putting the statements in quotation marks like this: "OS Version: $OsVersion"

enter image description here

using System;
using System.Diagnostics;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;



namespace WindowsToasts
{
 
    public class WindowsToast
    {

        public void Send_WindwsUpdateToast(string computerName)
        {
            InitialSessionState initialSessionState = InitialSessionState.CreateDefault();
            initialSessionState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
            using Runspace runspace = RunspaceFactory.CreateRunspace(initialSessionState);
            runspace.Open();

            using PowerShell powerShell = PowerShell.Create(runspace);
         
            string PSPath = @"C:\temp\ToastText.ps1";

            powerShell.AddCommand("Invoke-Command");
            powerShell.AddParameter("ComputerName", computerName);
            powerShell.AddParameter("File", PSPath);

            Collection<PSObject> PSOutput = powerShell.Invoke();
    
            foreach (PSObject outputItem in PSOutput)
            {
                // if null object was dumped to the pipeline during the script then a null object may be present here
                if (outputItem != null)
                {
                    Debug.WriteLine($"Output line: [{outputItem}]");
                }
            }

        }

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.