Repository navigation
[feature] Add -CustomPipeName to pwsh and Enter-PSHostProcess
#8889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
51efb81
37f0956
a27b6b5
6689e89
6e7578c
5d03cb6
7fa9459
d7df174
79eabb2
f8ab953
25fbad7
646bbd6
730f73e
3944254
8b4ac8f
2eb4ee7
a98573e
f2fa1c4
4da3a72
72d76ab
64ebaa8
11d6e74
ea81cb3
8909814
d02170f
2ba7604
f264743
dff15c7
1795fb7
aecf00e
6ad9763
78136f9
c0a0050
19ce341
c8e59a5
69621ca
73cd2ce
636c401
f8260cf
d79da98
5549bd8
d9d1779
1edba6c
9c3dfb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| using System.Management.Automation.Internal; | ||
| using System.Management.Automation.Remoting; | ||
| using System.Management.Automation.Runspaces; | ||
| using System.Text; | ||
|
|
||
| namespace Microsoft.PowerShell.Commands | ||
| { | ||
|
|
@@ -37,6 +38,7 @@ public sealed class EnterPSHostProcessCommand : PSCmdlet | |
| private const string ProcessParameterSet = "ProcessParameterSet"; | ||
| private const string ProcessNameParameterSet = "ProcessNameParameterSet"; | ||
| private const string ProcessIdParameterSet = "ProcessIdParameterSet"; | ||
| private const string PipeNameParameterSet = "PipePipeNameParameterSet"; | ||
| private const string PSHostProcessInfoParameterSet = "PSHostProcessInfoParameterSet"; | ||
|
|
||
| private const string NamedPipeRunspaceName = "PSAttachRunspace"; | ||
|
|
@@ -91,6 +93,16 @@ public PSHostProcessInfo HostProcessInfo | |
| set; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the custom named pipe name to connect to. This is usually used in conjunction with `pwsh -CustomPipeName`. | ||
| /// </summary> | ||
| [Parameter(Mandatory = true, ParameterSetName = EnterPSHostProcessCommand.PipeNameParameterSet)] | ||
| public string CustomPipeName | ||
| { | ||
| get; | ||
| set; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Optional name of AppDomain in process to enter. If not specified then the default AppDomain is used. | ||
| /// </summary> | ||
|
|
@@ -129,25 +141,34 @@ protected override void EndProcessing() | |
| } | ||
|
|
||
| // Check selected process for existence, and whether it hosts PowerShell. | ||
| Runspace namedPipeRunspace = null; | ||
| switch (ParameterSetName) | ||
| { | ||
| case ProcessIdParameterSet: | ||
| Process = GetProcessById(Id); | ||
| VerifyProcess(Process); | ||
| namedPipeRunspace = CreateNamedPipeRunspace(Process.Id, AppDomainName); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting, do we really need the VerifyProcess() check (that is File.Exist())? Perhaps the runtime error is informative enough.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not quite sure what you're saying. This |
||
| break; | ||
|
|
||
| case ProcessNameParameterSet: | ||
| Process = GetProcessByName(Name); | ||
| VerifyProcess(Process); | ||
| namedPipeRunspace = CreateNamedPipeRunspace(Process.Id, AppDomainName); | ||
| break; | ||
|
|
||
| case PSHostProcessInfoParameterSet: | ||
| Process = GetProcessByHostProcessInfo(HostProcessInfo); | ||
| break; | ||
| } | ||
| VerifyProcess(Process); | ||
|
|
||
| VerifyProcess(Process); | ||
| // Create named pipe runspace for selected process and open. | ||
| namedPipeRunspace = CreateNamedPipeRunspace(Process.Id, AppDomainName); | ||
| break; | ||
|
|
||
| // Create named pipe runspace for selected process and open. | ||
| Runspace namedPipeRunspace = CreateNamedPipeRunspace(Process.Id, AppDomainName); | ||
| case PipeNameParameterSet: | ||
| VerifyPipeName(CustomPipeName); | ||
| namedPipeRunspace = CreateNamedPipeRunspace(CustomPipeName); | ||
| break; | ||
| } | ||
|
|
||
| // Set runspace prompt. The runspace is closed on pop so we don't | ||
| // have to reverse this change. | ||
|
|
@@ -187,9 +208,20 @@ protected override void StopProcessing() | |
|
|
||
| #region Private Methods | ||
|
|
||
| private Runspace CreateNamedPipeRunspace(string customPipeName) | ||
| { | ||
| NamedPipeConnectionInfo connectionInfo = new NamedPipeConnectionInfo(customPipeName); | ||
| return CreateNamedPipeRunspace(connectionInfo); | ||
| } | ||
|
|
||
| private Runspace CreateNamedPipeRunspace(int procId, string appDomainName) | ||
| { | ||
| NamedPipeConnectionInfo connectionInfo = new NamedPipeConnectionInfo(procId, appDomainName); | ||
| return CreateNamedPipeRunspace(connectionInfo); | ||
| } | ||
|
|
||
| private Runspace CreateNamedPipeRunspace(NamedPipeConnectionInfo connectionInfo) | ||
| { | ||
| TypeTable typeTable = TypeTable.LoadDefaultTypeFiles(); | ||
| RemoteRunspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo, this.Host, typeTable) as RemoteRunspace; | ||
| remoteRunspace.Name = NamedPipeRunspaceName; | ||
|
|
@@ -203,20 +235,40 @@ private Runspace CreateNamedPipeRunspace(int procId, string appDomainName) | |
| } | ||
| catch (RuntimeException e) | ||
| { | ||
| string msgAppDomainName = (!string.IsNullOrEmpty(appDomainName)) ? appDomainName : NamedPipeUtils.DefaultAppDomainName; | ||
|
|
||
| // Unwrap inner exception for original error message, if any. | ||
| string errorMessage = (e.InnerException != null) ? (e.InnerException.Message ?? string.Empty) : string.Empty; | ||
|
|
||
| ThrowTerminatingError( | ||
| new ErrorRecord( | ||
| new RuntimeException( | ||
| StringUtil.Format(RemotingErrorIdStrings.EnterPSHostProcessCannotConnectToProcess, | ||
| msgAppDomainName, procId, errorMessage), | ||
| e.InnerException), | ||
| "EnterPSHostProcessCannotConnectToProcess", | ||
| ErrorCategory.OperationTimeout, | ||
| this)); | ||
| if (connectionInfo.CustomPipeName != null) | ||
| { | ||
| ThrowTerminatingError( | ||
| new ErrorRecord( | ||
| new RuntimeException( | ||
| StringUtil.Format( | ||
| RemotingErrorIdStrings.EnterPSHostProcessCannotConnectToPipe, | ||
| connectionInfo.CustomPipeName, | ||
| errorMessage), | ||
| e.InnerException), | ||
| "EnterPSHostProcessCannotConnectToPipe", | ||
| ErrorCategory.OperationTimeout, | ||
| this)); | ||
| } | ||
| else | ||
| { | ||
| string msgAppDomainName = connectionInfo.AppDomainName ?? NamedPipeUtils.DefaultAppDomainName; | ||
|
|
||
| ThrowTerminatingError( | ||
| new ErrorRecord( | ||
| new RuntimeException( | ||
| StringUtil.Format( | ||
| RemotingErrorIdStrings.EnterPSHostProcessCannotConnectToProcess, | ||
| msgAppDomainName, | ||
| connectionInfo.ProcessId, | ||
| errorMessage), | ||
| e.InnerException), | ||
| "EnterPSHostProcessCannotConnectToProcess", | ||
| ErrorCategory.OperationTimeout, | ||
| this)); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
|
|
@@ -345,6 +397,33 @@ private void VerifyProcess(Process process) | |
| } | ||
| } | ||
|
|
||
| private void VerifyPipeName(string customPipeName) | ||
| { | ||
| // Named Pipes are represented differently on Windows vs macOS & Linux | ||
| var sb = new StringBuilder(customPipeName.Length); | ||
| if (Platform.IsWindows) | ||
| { | ||
| sb.Append(@"\\.\pipe\"); | ||
| } | ||
| else | ||
| { | ||
| sb.Append(Path.GetTempPath()).Append("CoreFxPipe_"); | ||
| } | ||
|
|
||
| sb.Append(customPipeName); | ||
|
|
||
| string pipePath = sb.ToString(); | ||
| if (!File.Exists(pipePath)) | ||
| { | ||
| ThrowTerminatingError( | ||
| new ErrorRecord( | ||
| new PSArgumentException(StringUtil.Format(RemotingErrorIdStrings.EnterPSHostProcessNoNamedPipeFound, customPipeName)), | ||
| "EnterPSHostProcessNoNamedPipeFound", | ||
| ErrorCategory.InvalidArgument, | ||
| this)); | ||
| } | ||
| } | ||
|
|
||
| #endregion | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.