I’m trying to set a conditional break in VS Code to break when a specific variable is equal to a specific value.
This is what I’m entering:
$_.HardwareID -eq 'hpofficejet_6000_e604cdc'
This is contained within a foreach-object loop but never breaks. However, if I change the -eq to -ne then it breaks on every iteration through the loop.
Would you like to show how you populate the variable $_.HardwareID? You may show a little more of your code anyway. The issue might be something else than you think. Is the string you show the complete hardware ID?
I believe that you can’t get it to work from within the foreach-object loop. When I add code to show the $_.HardwareID after the loop and set a break point on it - the conditional works.
The code is still incomplete. Where is the part with the condition? … and the closing curly brace is missing. If you post some code you should keep in mind that we will try to run the code to reproduce your issue. So please be as least so considerate to remove all unnecessary lines of code you commented out anyway and make it at least syntactically correct.
In VS Code, the line that contains the Hardware = $_.HardwareID is where I set the break. But, as I indicated at the end of my last post. I believe that you can’t get it to work from within the foreach-object loop.
Thanks for looking at this, but I think I answered my own question.
OK. Please don’t get me wrong but I give up. There’s still a line of code missing where you use a condition like you asked for in your initial question.
I think what you are asking for is how to setup a breakpoint in VSCode that has a condition on it. You need to make sure your VSCode “Run and Debug” is setup properly. Very basically, I used:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "PowerShell Launch Script",
"type": "PowerShell",
"request": "launch",
"script": "${file}",
"cwd": "${workspaceFolder}"
}
]
}
You need to make sure your script is saved in a folder that your VSCode has open. Then you can set a breakpoint with an expression - you do need to use the syntax “$somethings -eq Something”
I think that is what you re looking for.
Another thing to note - the debugger might not understand: “$_” means
If that is the case, then you could, refactor the code to be something along the lines of:
$DetailsObject = @()
Foreach ($detail in $printers){
$detailsObject += New-Object PSCustomObject -properties @{
Name = $detail.name
}
}
Then have the conditional breakpoint query being:
$deatil.name -eq “something” ##obviously substituting your values