0

I want to add PowerShell capabilities to a C# program and I have embedded the system.managment.automation.dll assembly as a reference. I have found on StackOverflow two ways to call a PowerShell scrit from C# :

  • Using runspaces:

  • Not using runspaces:

    PowerShell powerShell = PowerShell.Create();
    powerShell.AddScript("my script");
    powerShell.Invoke();
    

According to this website, it seems the Powershell instance is tied to its C# host.

The Powershell runtime is invoked as a runspace and will, by default, process commands synchronously and deliver the results of processing to the host application so that they can be returned to the user.

Furthermore, the PowerShell script uses the embedded system.managment.automation.dll assembly and not the system-wide one, so the script would logically stop with its C# host.

Does the Powershell script would still run after the user close my program ? What about the two methods ?

PS: I want to support PowerShell v2.

cgcmake
  • 103
  • 3

1 Answers1

0

Let's take this in reverse chronology:

What about the two methods ?

There's no such thing as running powershell code by "not using runspaces".

The simple explanation is that a runspace is a core prerequisite - as the name implies, a runspace is the space in which powershell code runs.

When you do:

PowerShell powerShell = PowerShell.Create()

a runspace is implicitly created and attached to the PowerShell instance.

The only difference is that you simply defer control over the Runspace creation to the System.Management.Automation API, rather than instantiating it yourself.

Does the Powershell script would still run after the user close my program ?

The only circumstance under which the script would continue execution after the host application has terminated, would be when you connect to a remote runspace, ie. a runspace hosted in another process or on another computer.

Otherwise, the runspace (implicit or not) will be disposed along with the rest of the host application when it exits.

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Is there a way/trick for the PowerShell script to continue after the program die ? (calling PowerShell.exe on the host maybe ?) – cgcmake Jul 25 '16 at 21:33
  • Multiple options. Schedule a task, launch `powershell.exe` etc. What are you trying to accomplish exactly? – Mathias R. Jessen Jul 25 '16 at 21:37
  • Alert a user that a file/folder has been created/deleted in a directory specified by the user through a GUI. But this would be just one of the features of the program and I don't want to stop watching this directory because the user closes the program. BTW I have an another idea: creating a new process? – cgcmake Jul 25 '16 at 22:11
  • Schedule a task then (you can use [Register-ScheduledTask](https://technet.microsoft.com/en-us/library/jj649811(v=wps.630).aspx)) – Mathias R. Jessen Jul 25 '16 at 22:16
  • But the task would have to be scheduled every 5 seconds in order to be 'live', and I just want it to run for the current session, not after reboot etc..., an in-memory solution would be better for this no? – cgcmake Jul 25 '16 at 22:24