I'm using visual studio code to develop and debug powershell scripts. It's a nice IDE, everything is quite streamlined in many ways but I cannot find a way how to debug background jobs, those that are started by Start-Job with a script block. Let's say I have something as simple as this
New-Service -Name $Name -DisplayName $DisplayName -Description $Description -BinaryPathName $Location -StartupType Manual |
Start-Job -ScriptBlock {
$input | Start-Service
}
It works, but let's say I want to add something to the job script block and I want to go step by step over it. If I just put a breakpoint on the "$input | Start-Service" line it doesn't stop there. I read many articles and tried many suggestions but wasn't able to make the visual studio code debugger to stop on that line. I played with Wait-Debugger and Debug-Job but no matter what I did it never stopped on that line. It either did nothing at all or it waited as if for attaching another debugger but I didn't understand how to do it and where to attach it to.
Can anybody give a step by step instructions how to do it for visual studio code. There are some instructions for ISE, I tried to adapt them for visual studio code but nothing worked. Also, I'm really interested in debugging a script block, not putting that block into another file and starting that file as a job. I'd say it is too much to put a simple script block into another file.
UPDATE:
Here is one example that presumably should work somewhere, somehow but apparently not in Visual Studio Code. I wasn't able to make a debugger stop at any line of the job script block.
$ScriptBlock = {
$Foo = 5
Wait-Debugger
$Foo
$Foo + 1
$Foo + 2
};
# Invoke the Background Job
$Job = Start-Job -ScriptBlock $ScriptBlock
# Get the Background Job status. It should be 'AtBreakpoint'
$Job.State
# Enter the debugger
Debug-Job -Job $Job
$A = "Done"
Write-Host $A