0

I am attempting to remotely execute a powershell script passing in a object parameter. I have successfully managed to do this, but now I would like to just start the script, disconnect my remote session and have the powershell script continue to execute on the remote machine. Unfortunately I have been unable to do this.

Here is my code so far, this successfully runs the remote powershell script but if I dispose of my session the powershell script also stops executing:

PowerShell ps = PowerShell.Create();
ps.Runspace = _runspace;
ps.AddCommand(scriptPath);

if (scriptParameters != null)
{
    foreach (var parameter in scriptParameters)
    {
        ps.AddParameter(parameter.Key, parameter.Value);
    }
}

ps.Invoke();

How can I just kick off the .ps1 script and leave it running even when I disconnect my remote session?

Matt
  • 45,022
  • 8
  • 78
  • 119
Francium123
  • 451
  • 5
  • 19

1 Answers1

0

You can only do this with Powershell 3+. I haven't tested it, but I imagine both sides of the connection need to support disconnected sessions.

In .Net you'd use the Runspace.Disconnect Method.

The Powershell equivalent would be Disconnect-PSSession.

briantist
  • 45,546
  • 6
  • 82
  • 127