Skip to content

Commit 03fe695

Browse files
jborean93andyleejordan
authored andcommitted
Expose pathMappings to Start-DebugAttachSession and fixup Name
1 parent fed67d5 commit 03fe695

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ function Start-DebugAttachSession {
4343
[string]
4444
$WindowActionOnEnd,
4545

46+
[Parameter()]
47+
[IDictionary[]]
48+
$PathMapping,
49+
4650
[Parameter()]
4751
[switch]
4852
$AsJob
@@ -110,11 +114,21 @@ function Start-DebugAttachSession {
110114
return
111115
}
112116

113-
$configuration.name = "Attach Process $ProcessId"
117+
if ($Name) {
118+
$configuration.name = $Name
119+
}
120+
else {
121+
$configuration.name = "Attach Process $ProcessId"
122+
}
114123
$configuration.processId = $ProcessId
115124
}
116125
elseif ($CustomPipeName) {
117-
$configuration.name = "Attach Pipe $CustomPipeName"
126+
if ($Name) {
127+
$configuration.name = $Name
128+
}
129+
else {
130+
$configuration.name = "Attach Pipe $CustomPipeName"
131+
}
118132
$configuration.customPipeName = $CustomPipeName
119133
}
120134
else {
@@ -136,6 +150,10 @@ function Start-DebugAttachSession {
136150
$configuration.temporaryConsoleWindowActionOnDebugEnd = $WindowActionOnEnd.ToLowerInvariant()
137151
}
138152

153+
if ($PathMapping) {
154+
$configuration.pathMappings = $PathMapping
155+
}
156+
139157
# https://microsoft.github.io/debug-adapter-protocol/specification#Reverse_Requests_StartDebugging
140158
$resp = $debugServer.SendRequest(
141159
'startDebugging',

module/docs/Start-DebugAttachSession.md

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ 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>] [-WindowActionOnEnd {Close | Hide | Keep}] [-AsJob] [<CommonParameters>]
19+
[-ComputerName <String>] [-WindowActionOnEnd {Close | Hide | Keep}] [-PathMapping <IDictionary[]>] [-AsJob]
20+
[<CommonParameters>]
2021
```
2122

2223
### CustomPipeName
2324
```
2425
Start-DebugAttachSession [-Name <String>] [-CustomPipeName <String>] [-RunspaceName <String>]
25-
[-RunspaceId <Int32>] [-ComputerName <String>] [-WindowActionOnEnd {Close | Hide | Keep}] [-AsJob]
26-
[<CommonParameters>]
26+
[-RunspaceId <Int32>] [-ComputerName <String>] [-WindowActionOnEnd {Close | Hide | Keep}]
27+
[-PathMapping <IDictionary[]>] [-AsJob] [<CommonParameters>]
2728
```
2829

2930
## DESCRIPTION
@@ -75,6 +76,27 @@ Write-Host "Test $a - $PID"
7576

7677
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.
7778

79+
### -------------------------- EXAMPLE 2 --------------------------
80+
81+
```powershell
82+
$attachParams = @{
83+
ComputerName = 'remote-windows'
84+
ProcessId = $remotePid
85+
RunspaceId = 1
86+
PathMapping = @(
87+
@{
88+
localRoot = 'C:\local\path\to\scripts\'
89+
remoteRoot = 'C:\remote\path\on\remote-windows\'
90+
}
91+
)
92+
}
93+
Start-DebugAttachSession @attachParams
94+
```
95+
96+
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`.
97+
98+
The debug client can see the remote files as local when setting breakpoints and inspecting the callstack with this mapped path.
99+
78100
## PARAMETERS
79101

80102
### -AsJob
@@ -143,6 +165,24 @@ Accept pipeline input: False
143165
Accept wildcard characters: False
144166
```
145167

168+
### -PathMapping
169+
170+
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.
171+
172+
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.
173+
174+
```yaml
175+
Type: IDictionary[]
176+
Parameter Sets: (All)
177+
Aliases:
178+
179+
Required: False
180+
Position: Named
181+
Default value: None
182+
Accept pipeline input: False
183+
Accept wildcard characters: False
184+
```
185+
146186
### -ProcessId
147187

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

0 commit comments

Comments
 (0)