0

I am writing a C# application that needs to make use of a Powershell script file that is full of functions. What I need to do from my WPF app is:

  1. Launch the Powershell ISE (working fine)
  2. I need to have functions that are in a .ps1 file on the filesystem available to the user when they open the ISE. (not working)

Some pseudo code might look like this:

Process.Start("powershell_ise.exe");
PowershellSession session = GetPsSession("this user");
session.Load("functions.ps1");

I've been looking around the System.Management.Automation classes, but can't find anything useful. Thank you.

Dbloom
  • 1,302
  • 3
  • 18
  • 45
  • Any reason to start separate `powershell_ise.exe` instead of hosting PowerShell in your app? – user4003407 Apr 21 '17 at 05:18
  • 1
    Possible duplicate of [Importing PowerShell module in C#](http://stackoverflow.com/questions/17070747/importing-powershell-module-in-c-sharp) – Ali Bahrami Apr 21 '17 at 05:19
  • As stated in above comments. Check the answer of this [link](http://stackoverflow.com/questions/17070747/importing-powershell-module-in-c-sharp). – Moerwald Apr 21 '17 at 05:32
  • @PetSerAl - Because the ISE is a decent editor, and coding up a new editor is way more work than I want/need to do. – Dbloom Apr 21 '17 at 05:58
  • 3
    @Dbloom Sounds like you're overdoing it. If you want ISE to launch with a module or a script being avaliable at startup, just distribute [a profile](https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_profiles) for `powershell_ise.exe` – Mathias R. Jessen Apr 21 '17 at 06:27
  • @MathiasR.Jessen I have entertained going that route. But this is an app that others are going to use to create packages, and I am trying to present a seamless experience. So if my app can take care of the dot sourcing for the user, all the better. – Dbloom Apr 21 '17 at 20:19
  • `$ise=Start-Process powershell_ise -PassThru; $ci= $( [Type]( 'System', 'Management', 'Automation', 'Runspaces', 'NamedPipeConnectionInfo' -join '.') )::new( $ise.Id ); $r = [runspacefactory]::CreateRunspace( $ci ); $r.Open(); $ps = [powershell]::Create(); $ps.Runspace = $r; $ps.AddScript{ Get-Runspace | ? Name -ne RemoteHost | % { $ps = [powershell]::Create().AddCommand( 'C:\Users\PetSerAl\Test\Test.ps1', $false ) } { $ps.Runspace = $_; $ps.Invoke() } }.Invoke()` – user4003407 Apr 22 '17 at 04:21

0 Answers0