Skip to content

Commit ba8d36c

Browse files
committed
Expose pathMappings to Start-DebugAttachSession and fixup Name
1 parent 23ef91b commit ba8d36c

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

module/PowerShellEditorServices/Commands/Public/Start-DebugAttachSession.ps1

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ function Start-DebugAttachSession {
3838
[string]
3939
$ComputerName,
4040

41+
[Parameter()]
42+
[IDictionary[]]
43+
$PathMapping,
44+
4145
[Parameter()]
4246
[switch]
4347
$AsJob
@@ -105,11 +109,21 @@ function Start-DebugAttachSession {
105109
return
106110
}
107111

108-
$configuration.name = "Attach Process $ProcessId"
112+
if ($Name) {
113+
$configuration.name = $Name
114+
}
115+
else {
116+
$configuration.name = "Attach Process $ProcessId"
117+
}
109118
$configuration.processId = $ProcessId
110119
}
111120
elseif ($CustomPipeName) {
112-
$configuration.name = "Attach Pipe $CustomPipeName"
121+
if ($Name) {
122+
$configuration.name = $Name
123+
}
124+
else {
125+
$configuration.name = "Attach Pipe $CustomPipeName"
126+
}
113127
$configuration.customPipeName = $CustomPipeName
114128
}
115129
else {
@@ -127,6 +141,10 @@ function Start-DebugAttachSession {
127141
$configuration.runspaceName = $RunspaceName
128142
}
129143

144+
if ($PathMapping) {
145+
$configuration.pathMappings = $PathMapping
146+
}
147+
130148
# https://microsoft.github.io/debug-adapter-protocol/specification#Reverse_Requests_StartDebugging
131149
$resp = $debugServer.SendRequest(
132150
'startDebugging',

module/docs/Start-DebugAttachSession.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ Starts a new debug session attached to the specified PowerShell instance.
1616
### ProcessId (Default)
1717
```
1818
Start-DebugAttachSession [-Name <String>] [-ProcessId <Int32>] [-RunspaceName <String>] [-RunspaceId <Int32>]
19-
[-ComputerName <String>] [-AsJob] [<CommonParameters>]
19+
[-ComputerName <String>] [-PathMapping <IDictionary[]>] [-AsJob] [<CommonParameters>]
2020
```
2121

2222
### CustomPipeName
2323
```
2424
Start-DebugAttachSession [-Name <String>] [-CustomPipeName <String>] [-RunspaceName <String>]
25-
[-RunspaceId <Int32>] [-ComputerName <String>] [-AsJob] [<CommonParameters>]
25+
[-RunspaceId <Int32>] [-ComputerName <String>] [-PathMapping <IDictionary[]>] [-AsJob]
26+
[<CommonParameters>]
2627
```
2728

2829
## DESCRIPTION
@@ -74,6 +75,27 @@ Write-Host "Test $a - $PID"
7475

7576
Launches a new PowerShell process with a custom pipe and starts a new attach configuration that will debug the new process under a child debugging session. The caller waits until the new process ends before ending the parent session.
7677

78+
### -------------------------- EXAMPLE 2 --------------------------
79+
80+
```powershell
81+
$attachParams = @{
82+
ComputerName = 'remote-windows'
83+
ProcessId = $remotePid
84+
RunspaceId = 1
85+
PathMapping = @(
86+
@{
87+
localRoot = 'C:\local\path\to\scripts\'
88+
remoteRoot = 'C:\remote\path\on\remote-windows\'
89+
}
90+
)
91+
}
92+
Start-DebugAttachSession @attachParams
93+
```
94+
95+
Attaches to a remote PSSession through the WSMan parameter and maps the remote path running the script in the PSSession to the same copy of files locally. For example `remote-windows` is running the script `C:\remote\path\on\remote-windows\script.ps1` but the same script(s) are located locally on the current host `C:\local\path\to\scripts\script.ps1`.
96+
97+
The debug client can see the remote files as local when setting breakpoints and inspecting the callstack with this mapped path.
98+
7799
## PARAMETERS
78100

79101
### -AsJob
@@ -142,6 +164,24 @@ Accept pipeline input: False
142164
Accept wildcard characters: False
143165
```
144166

167+
### -PathMapping
168+
169+
An array of dictionaries with the keys `localRoot` and `remoteRoot` that maps a local and remote path root to each other. This option is useful when attaching to a PSSession running a script that is not accessible locally but can be found under a different path.
170+
171+
It is a good idea to ensure the `localRoot` and `remoteRoot` entries are either the absolute path to a script or ends with the trailing directory separator if specifying a directory. A path can also be mapped from a Windows and non-Windows path, just ensure the correct directory separators are used for each OS type. For example `/` for non-Windows and `\` for Windows.
172+
173+
```yaml
174+
Type: IDictionary[]
175+
Parameter Sets: (All)
176+
Aliases:
177+
178+
Required: False
179+
Position: Named
180+
Default value: None
181+
Accept pipeline input: False
182+
Accept wildcard characters: False
183+
```
184+
145185
### -ProcessId
146186

147187
The ID of the PowerShell host process that should be attached. This option is mutually exclusive with `-CustomPipeName`.

0 commit comments

Comments
 (0)