0

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
Alex
  • 655
  • 1
  • 8
  • 16
  • My guess is that you're running into trouble because you're spawning a background process with `Start-Job`. Your program isn't executing the script block that `Start-Job` is passed as a parameter. `Start-Job` is starting another thread which is executing it. That's why the `Debug-Job` command exists. I think you'd be able to accomplish what you're doing here with `New-Service ... | Foreach-Object -MethodName Start` or `New-Service ... | ForEach-Object { $_.Start(); $_.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Running) }` or similar. – Bacon Bits May 26 '21 at 16:03
  • But this is what I'm trying to avoid. Your example is synchronous. It was similar to that before. Now, I need it to be asynchronous and I used Start-Job and as I mentioned it works just fine. The only question is how to do step by step debugging. Also, I think it is worse than another thread, I remember I read somewhere that start Start-Job starts another powershell session in a separate process. The question still remains how to do step by step debugging of the background job. – Alex May 26 '21 at 16:40
  • Are you sure about that? I'm fairly certain that `ServiceController.Start()` is not a synchronous call. Else why would `WaitForStatus()` exist? Indeed, thinking about it I don't see why you can't use `New-Service ... | Start-Service`. It's not difficult to find [questions asking for `Start-Service` to work sychronously](https://stackoverflow.com/questions/28186904/powershell-wait-for-service-to-be-stopped-or-started) or [a question about `ServiceController.Stop()` being asynchronous](https://stackoverflow.com/q/48188096/696808). – Bacon Bits May 26 '21 at 16:48
  • I forgot one thing. I'd say .Start is asynchronous but in my case when I await debugger attaching in the static Main method of the service .Start waits. If I move debugger attaching await to OnStart service method .Start behaves asynchronously and doesn't wait. But still the main question is how to debug background jobs in visual studio code. – Alex May 26 '21 at 19:10

0 Answers0