/// <summary> /// Initializes a new instance of the PSListenerConsoleSample class. /// </summary> public PSListenerConsoleSample() { // Create the host and runspace instances for this interpreter. // Note that this application does not support console files so // only the default snap-ins will be available. this.myHost = new MyHost(this); this.myRunSpace = RunspaceFactory.CreateRunspace(this.myHost); this.myRunSpace.Open(); // Create a PowerShell object to run the commands used to create // $profile and load the profiles. lock (this.instanceLock) { this.currentPowerShell = PowerShell.Create(); } try { this.currentPowerShell.Runspace = this.myRunSpace; PSCommand[] profileCommands = HostUtilities.GetProfileCommands("SampleHost06"); foreach (PSCommand command in profileCommands) { this.currentPowerShell.Commands = command; this.currentPowerShell.Invoke(); } } finally { // Dispose the PowerShell object and set currentPowerShell // to null. It is locked because currentPowerShell may be // accessed by the ctrl-C handler. lock (this.instanceLock) { this.currentPowerShell.Dispose(); this.currentPowerShell = null; } } }
/// <summary> /// Gets an array of commands that can be run sequentially to set $profile and run the profile commands. /// </summary> /// <param name="shellId">The id identifying the host or shell used in profile file names.</param> /// <returns>An array of commands.</returns> public static PSCommand[] GetProfileCommands(string shellId) { return(HostUtilities.GetProfileCommands(shellId, false)); }
/// <summary> /// Used to get all profile file names for the current or all hosts and for the current or all users. /// </summary> /// <param name="shellId">null for all hosts, not null for the specified host</param> /// <param name="forCurrentUser">false for all users, true for the current user.</param> /// <returns>The profile file name matching the parameters.</returns> internal static string GetFullProfileFileName(string shellId, bool forCurrentUser) { return(HostUtilities.GetFullProfileFileName(shellId, forCurrentUser, false)); }