Advaced question: Executing PowerShellHost.ExecuteInitScripts method in a different PowershellHost's Runspace (e.g. PowerGUI VSX)

Topics: General
Feb 6, 2013 at 8:45 AM
Edited Feb 6, 2013 at 9:09 AM
I would like to use PowerGUI to run init.ps1 scripts once the solution is loaded.
When exactly Nuget extension runs these scripts. I know that in VsConsole\PowerShellHost\PowerShellHost.cs file there is ExecuteInitScripts method that executes this code:
var localRepository = new SharedPackageRepository(repositorySettings.RepositoryPath);

// invoke init.ps1 files in the order of package dependency.
// if A -> B, we invoke B's init.ps1 before A's.

var sorter = new PackageSorter(targetFramework: null);
var sortedPackages = sorter.GetPackagesByDependencyOrder(localRepository);

foreach (var package in sortedPackages)
      string installPath = localRepository.PathResolver.GetInstallPath(package);

      AddPathToEnvironment(Path.Combine(installPath, "tools"));
      Runspace.ExecuteScript(installPath, "tools\\init.ps1", package);
I could see inside System.Management.Automation.dll there is Runspace.cs class inside this namespace: System.Management.Automation.Runspaces that allows assignment of a Runspace
using a static property:
    public static Runspace DefaultRunspace
            return ThreadSpecificDefaultRunspace;
            ThreadSpecificDefaultRunspace = value;
NuGetPSHost.cs uses is default Runspace that provide
    public Runspace Runspace
        get { return Runspace.DefaultRunspace; }
Will my PowerGUI have a chance to provide its runspace to Nuget package so ExecuteInitScripts executes in PowerGUI runspace context?

In what order are VS Packages executed? Which one will be handle SolutionOpened first.
I believe that Nuget Package will always run ExecuteInitScripts when a solution is opened no matter if the Nuget Package Console is opened. Is that right. If that is the case I could not have a way to setup a Runspace from PowerGUI console if Nuget package runs first. If yes, is there any way to suppress Nuget Package from doing it and only run ExecuteInitScripts when Nuget Package Console is actually opened by the user.

Feb 6, 2013 at 5:56 PM
No, the init.ps1 is only executed when the NuGet Package Console is opened.