Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 49c9289

Browse files
Add examples for Start-DebugAttachSession
Adds examples to demonstrate how the new `Start-DebugAttachSession` function added in PowerShell Editor Services can be used.
1 parent 4cea222 commit 49c9289

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

‎examples/ChildDebugSessionAttach.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Example on how Start-DebugAttachSession can be used to attach to another
2+
# process. This launches a child process that runs ChildDebugSessionTarget.ps1
3+
# but it can be adapted to attach to any other PowerShell process that is
4+
# either already running or started like this example. To test this example,
5+
# add a breakpoint to ChildDebugSessionTarget.ps1, select the
6+
# 'PowerShell Launch Current File' configuration and press F5.
7+
8+
$pipeName = "TestPipe-$(New-Guid)"
9+
$scriptPath = Join-Path -Path $PSScriptRoot -ChildPath 'ChildDebugSessionTarget.ps1'
10+
11+
$procParams = @{
12+
FilePath = 'pwsh'
13+
ArgumentList = ('-CustomPipeName {0} -File "{1}" -WaitForAttach' -f $pipeName, $scriptPath)
14+
PassThru = $true
15+
}
16+
$proc = Start-Process @procParams
17+
18+
Start-DebugAttachSession -CustomPipeName $pipeName -RunspaceId 1
19+
20+
# We need to ensure this debug session stays alive until the process exits. If
21+
# we exit early then the child debug session will also exit.
22+
$proc | Wait-Process

‎examples/ChildDebugSessionTarget.ps1

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[CmdletBinding()]
2+
param (
3+
[Parameter()]
4+
[switch]
5+
$WaitForAttach
6+
)
7+
8+
if ($WaitForAttach) {
9+
# For an attach request we need to wait for the debug pipe runspace to be
10+
# opened before continuing. There is no builtin way to do this so we
11+
# poll the runspace list until a new one is created.
12+
$runspaces = Get-Runspace
13+
while ($true) {
14+
if (Get-Runspace | Where-Object { $_.Id -notin $runspaces.Id }) {
15+
break
16+
}
17+
Start-Sleep -Seconds 1
18+
}
19+
20+
# Windows PowerShell 5.1 will not sync breakpoints until the debugger has
21+
# stopped at least once. We use Wait-Debugger to make this happen.
22+
if ($PSVersionTable.PSVersion -lt '6.0') {
23+
Wait-Debugger
24+
}
25+
else {
26+
Start-Sleep -Seconds 1 # Give the debugger time to sync breakpoints
27+
}
28+
}
29+
30+
$processInfo = "This process is running with PID $PID and has runspace ID $([Runspace]::DefaultRunspace.Id)"
31+
Write-Host $processInfo # Place breakpoint here

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /