Class Powershell 5类,在构造函数或方法中编写调试或编写详细输出

Class Powershell 5类,在构造函数或方法中编写调试或编写详细输出,class,powershell,powershell-5.0,Class,Powershell,Powershell 5.0,我有一个powershell类,在使用一个新的powershell 5类时,我尝试将write debug和write verbose写入输出 例如: class TestWriteDebug { TestWriteDebug() { Write-Debug "Constructor called" } verboseOutput() { Write-Verbose "Method `"verboseOutput()`"

我有一个powershell类,在使用一个新的powershell 5类时,我尝试将write debug和write verbose写入输出

例如:

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

    verboseOutput()
    {
        Write-Verbose "Method `"verboseOutput()`" was called"
    }
}
我通过[TestWritedBug]::new()调用此函数

我似乎不知道在创建对象或调用其方法时如何传递-debug和-verbose标志,有人能告诉我这是如何实现的吗


感谢您的帮助。

因为您将它们作为表达式的一部分进行调用,所以启用它们的最简单方法可能是使用以下变量:

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

$test = [TestWriteDebug]::new()
$test.verboseOutput()
要将它们重置为静默,请退出定义这些首选项的范围,或将值重置为
'SilentlyContinue'
。如果要在有限的上下文中启用它们,则在脚本块中执行它们可以做到以下几点:

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

类的行为方式与cmdlet类似(即默认情况下存在CmdletBinding行为)。要显示这些方法,只需在调用使用此类的cmdlet时添加
-Verbose
-Debug
开关

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
输出:

展开下面的代码段,然后单击
运行代码段
,以查看预期的PS输出

div{
背景色:深蓝色;
颜色:浅灰色;
字体大小:粗体;
}
.verbose{color:Cyan;}
.host{颜色:浅绿色;}
正常功能:
你好,我叫DemoBoy
不带详细开关的Cmdlet
你好,我叫德莫曼
带有详细开关的Cmdlet
我听说我的名字是迪莫船长
有人问我的名字
你好,我是Demo队长
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