-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
I'm running a PowerShell script remotely on my Windows 10 machine. Connected remotely via WinRM + PowerShell Runspace.
However, I'm experiencing issues with the Runspace Disconnect feature
My source machine: Linux
My target machine: Windows 10 x64, running PS version 5.1.18362.752, WinRM enabled, PS Remoting enabled
I've played around and tested PS Runspace Disconnect feature using this C# snippet:
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Net;
using System.Threading.Tasks;
namespace PSDisconnect
{
internal static class Program
{
public static void Main(string[] args)
{
MainAsync().GetAwaiter().GetResult();
}
private static async Task MainAsync()
{
var username = "username"; // < --- set username
var password = "password"; // < --- set password
var ipAddress = "10.10.220.104"; // < --- set ip address
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, ipAddress, 5985, "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", new PSCredential(username, new NetworkCredential("", password).SecurePassword));
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Negotiate;
using var runspacePool = RunspaceFactory.CreateRunspacePool(1, 5, connectionInfo);
runspacePool.Open();
var powerShell = PowerShell.Create();
powerShell.RunspacePool = runspacePool;
string scriptContent = "\nfunction Test-GetPSInfo {\nreturn $PSVersionTable.PSVersion\n}\n";
await powerShell.AddScript(scriptContent).InvokeAsync();
var results = await powerShell.AddCommand("Test-GetPSInfo").InvokeAsync();
foreach (var result in results)
{
Console.Out.WriteLine(result.ToString());
}
runspacePool.Disconnect();
}
}
}
This fail to work as expected. I'm getting the following error below stating wrongly that my target machine's PowerShell version is below version 3.0 which is incorrect since my target machine is running PowerShell version 5.1.18362.752
output:
5.1.18362.752
Unhandled exception. System.Management.Automation.PSInvalidOperationException: The Disconnect operation is not supported on the server. The server must be running Windows PowerShell 3.0 or greater for remote runspace pool disconnection support.
at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal.BeginDisconnect(AsyncCallback callback, Object state)
at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal.Disconnect()
at System.Management.Automation.Runspaces.RunspacePool.Disconnect()
If anyone knows if this is supported or what am I doing wrong - I would appreciate sharing 🙏