Dan Hampshire
Dan Hampshire

Reputation: 115

Powershell 5 class, write-debug or write-verbose output in constructor or method

I have a powershell class, I'm trying to get write-debug and write-verbose to output while using a new Powershell 5 class.

For example:

class TestWriteDebug
{
    TestWriteDebug()
    {
        Write-Debug "Constructor called"
    }

    verboseOutput()
    {
        Write-Verbose "Method `"verboseOutput()`" was called"
    }
}

I'm calling this through [TestWriteDebug]::new()

$test = [TestWriteDebug]::new()
$test.verboseOutput()

I can't seem to figure out how to pass -debug and -verbose flags when creating the object or when calling it's methods, can anyone tell me how this is meant to be implemented?

Thanks for your help.

Upvotes: 2

Views: 2843

Answers (2)

JohnLBevan
JohnLBevan

Reputation: 24430

Classes act in similar way to a Cmdlets (i.e. CmdletBinding behaviour is in place by default). To get these methods to display, just add the -Verbose or -Debug switch when calling the cmdlet which uses this class

class DemoClass {

    [string]$Name

    DemoClass([string]$Name) {
        Write-Verbose "I'm told my name is $Name"
        $this.Name = $Name
    }

    [string]GetMyName() {
        Write-Verbose "I've been asked my name"
        return "Hello, my name is $($this.Name)"
    }

}

function Invoke-NormalFunction([string]$Name) {
    $myDemo = [DemoClass]::new($Name)
    $myDemo.GetMyName()
}

function Invoke-AwesomeCmdlet {
    [CmdletBinding()]
    param([string]$Name)
    $myDemo = [DemoClass]::new($Name)
    $myDemo.GetMyName()
}

Write-Host "Normal Function:" -ForegroundColor Green
Invoke-NormalFunction('DemoBoy')

Write-Host "Cmdlet Without Verbose Switch" -ForegroundColor Green
Invoke-AwesomeCmdlet('DemoMan')

Write-Host "Cmdlet With Verbose Switch" -ForegroundColor Green
Invoke-AwesomeCmdlet('Captain Demo') -Verbose

Output:

Expand the below snippet then click Run Code Snippet to view expected PS output.

div {
    background-color: DarkBlue;
    color: LightGray;
    font-weight: Bold;
}
.verbose {color: Cyan;}
.host {color: LightGreen;}
<div class='host'>Normal Function:</div>
<div>Hello, my name is DemoBoy</div>
<div class='host'>Cmdlet Without Verbose Switch</div>
<div>Hello, my name is DemoMan</div>
<div class='host'>Cmdlet With Verbose Switch</div>
<div class='verbose'>VERBOSE: I'm told my name is Captain Demo</div>
<div class='verbose'>VERBOSE: I've been asked my name</div>
<div>Hello, my name is Captain Demo</div>

More Info

If you want this to take effect for a whole script; not just those cmdlets called with the relevant switch, add the following to the top of your script file.

[CmdletBinding()]
param()

Upvotes: 3

Burt_Harris
Burt_Harris

Reputation: 6874

Because you are invoking them as part of an expression, the simplest way to enable these is probably with the Preference variables:

$DebugPreference = 'Continue'
$VerbosePreference = 'Continue'

$test = [TestWriteDebug]::new()
$test.verboseOutput()

To reset them to silent, either exit the scope where these preferences are defined, or reset the values to 'SilentlyContinue'. If you want to enable them in a limited context, executing them in a scriptblock can do this:

$test = &{$DebugPreference = 'continue'; [TestWriteDebug]::new()}

Upvotes: 5

Related Questions