Skip to content

Write-Debug expands -Message when it should SilentlyContinue #20738

@iRon7

Description

@iRon7

Prerequisites

Steps to reproduce

Write-Debug and Write-Verbose expand the -Message where the commands should SilentlyContinue.

Use case:
I am using a class with has a lazy evaluation (because not all results always require to be evaluated).
It concerns a recursive script where I would like to add some Write-Debug commands, like:

Write-Debug "This takes 3 seconds $(Sleep 3)"

Expected behavior

When the -Debug switch is omitted for my script, the script shouldn't take more time because of the debug commands.
In other words, the -Message should not be evaluated.

Actual behavior

When the -Debug switch is omitted for my script, the -Message is evaluated and takes 3 secondes (for the example).

Workaround

Function test {
    [CmdletBinding()] param() 
    if ($DebugPreference -in 'Stop', 'Continue', 'Inquire') { Write-Debug "Test $(Sleep 3)" }
}

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.10
PSEdition                      Core
GitCommitId                    7.3.10
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions