Seeking help with conditional break

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.

What’s wrong with my condition?

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. :wink: Is the string you show the complete hardware ID?

Here is the section of code I’m using:

$drivers = Get-PrinterDriver -Name * -ComputerName 'SERVER'
$drivers | ForEach-Object -Process {
    $PrintDriverDetails = [PSCustomObject]@{
        ColorProfiles = $_.ColorProfiles
        ComputerName = $_.ComputerName
        ConfigFile = $_.ConfigFile
        DataFile = $_.DataFile
        DefaultDatatype = $_.DefaultDatatype
        DependentFiles = $_.DependentFiles
        DriverVersion = $_.DriverVersion.ToString()
        HardwareID = $_.HardwareID
        HelpFile = $_.HelpFile
        InfPath = $_.InfPath
        IsPackageAware = $_.IsPackageAware
        MajorVersion = $_.MajorVersion
        Manufacturer = $_.Manufacturer
        Monitor = $_.Monitor
        Name = $_.Name
        OEMUrl = $_.OEMUrl
        Path = $_.Path
        PreviousCompatibleNames = $_.PreviousCompatibleNames
        PrinterEnvironment = $_.PrinterEnvironment
        PrintProcessor = $_.PrintProcessor
        provider = $_.provider
    }
}

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.

You seem not sure at all. Why not give it a shot and post the rest of your code. There might be another (a better) way.

<#
.SYNOPSIS
    Get printer information
.DESCRIPTION
    Get printer information with an eye to recreate conditions on a new computer
.NOTES
    
.LINK
    N/A
.EXAMPLE
    Get-PrinterInfo -Verbose
    Retrieves all printers and driver information on the local computer
.EXAMPLE
    Get-PrinterInfo -computer SERVER01
    Retrieves all printers, drives and queue information on the specified computer
#>

Remove-Item -Path .\PrintersInServer01.xlsx -Force
Remove-Item -Path .\ColorProfiles.txt -Force
Add-Content -Path .\ColorProfiles.txt -Value "$(Get-Date)" -Encoding UTF8

{0}
$printers = @()
$AllPrinterDetails = @()
$drivers = @()
$AllDriverDetails = @()

$printers = Get-Printer -Name * -ComputerName 'Server01'
$printers | ForEach-Object -Process {
    $PrinterDetails = [PSCustomObject]@{
        Name = $_.Name
        PortName = $_.PortName
        JobCount = $_.JobCount
        Published = $_.Published
        Shared = $_.Shared
        ShareName = $_.ShareName
        PrinterStatus = $_.PrinterStatus
        Comment = $_.Comment
        Location = $_.Location
        #BranchOfficeOfflineLogSizeMB = $_.BranchOfficeOfflineLogSizeMB
        #Caption = $_.Caption
        #CommunicationStatus = $_.CommunicationStatus
        ComputerName = $_.ComputerName
        Datatype = $_.Datatype
        DefaultJobPriority = $_.DefaultJobPriority
        #Description = $_.Description
        #DetailedStatus = $_.DetailedStatus
        #DisableBranchOfficeLogging = $_.DisableBranchOfficeLogging
        DriverName = $_.DriverName
        #ElementName = $_.ElementName
        #HealthState = $_.HealthState
        #InstallDate = $_.InstallDate
        #InstanceID = $_.InstanceID
        KeepPrintedJobs = $_.KeepPrintedJobs
        #OperatingStatus = $_.OperatingStatus
        #OperationalStatus = $_.OperationalStatus
        #PermissionSDDL = $_.PermissionSDDL
        #PrimaryStatus = $_.PrimaryStatus
        PrintProcessor = $_.PrintProcessor
        Priority = $_.Priority
        #SeparatorPageFile = $_.SeparatorPageFile
        StartTime = $_.StartTime
        #Status = $_.Status
        #StatusDescriptions = $_.StatusDescriptions
        UntilTime = $_.UntilTime
        #WorkflowPolicy = $_.WorkflowPolicy
        DeviceType = $_.DeviceType
        #RenderingMode = $_.RenderingMode
        Type = $_.Type
    }
    $AllPrinterDetails += $PrinterDetails
}
$AllPrinterDetails | Export-Excel -Path .\PrintersInPrintman.xlsx -WorksheetName 'Server01 Printers' -FreezeTopRow -AutoSize -AutoFilter -TableStyle 'Medium7'
$drivers = Get-PrinterDriver -Name * -ComputerName 'Server01'
$drivers | ForEach-Object -Process {
    $PrintDriverDetails = [PSCustomObject]@{
        #Caption = $_.Caption
        ColorProfiles = $_.ColorProfiles
        #CommunicationStatus = $_.CommunicationStatus
        ComputerName = $_.ComputerName
        ConfigFile = $_.ConfigFile
        #CoreDriverDependencies = $_.CoreDriverDependencies
        DataFile = $_.DataFile
        #Date = $_.Date
        DefaultDatatype = $_.DefaultDatatype
        DependentFiles = $_.DependentFiles
        #Description = $_.Description
        #DetailedStatus = $_.DetailedStatus
        DriverVersion = $_.DriverVersion.ToString()
        #ElementName = $_.ElementName
        HardwareID = $_.HardwareID
        #HealthState = $_.HealthState
        HelpFile = $_.HelpFile
        InfPath = $_.InfPath
        #InstallDate = $_.InstallDate
        #InstanceID = $_.InstanceID
        IsPackageAware = $_.IsPackageAware
        MajorVersion = $_.MajorVersion
        Manufacturer = $_.Manufacturer
        Monitor = $_.Monitor
        Name = $_.Name
        OEMUrl = $_.OEMUrl
        #OperatingStatus = $_.OperatingStatus
        #OperationalStatus = $_.OperationalStatus
        Path = $_.Path
        PreviousCompatibleNames = $_.PreviousCompatibleNames
        #PrimaryStatus = $_.PrimaryStatus
        PrinterEnvironment = $_.PrinterEnvironment
        PrintProcessor = $_.PrintProcessor
        provider = $_.provider
        #Status = $_.Status
        #StatusDescriptions = $_.StatusDescriptions
        #VendorSetup = $_.VendorSetup
    }
    if($_.ColorProfiles.Count -ge 1) {
        ForEach-Object -Process {
            Add-Content -Value "$($_.Name) is using color profile: $($_.ColorProfiles)" -Path .\ColorProfiles.txt
        }
    }
    $AllDriverDetails += $PrintDriverDetails
}
$AllDriverDetails | Export-Excel -Path .\PrintersInPrintman.xlsx -WorksheetName 'Server01 Printer Drivers' -FreezeTopRow -AutoSize -AutoFilter -TableStyle 'Medium7' -show

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.

1 Like

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