File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments