我有一个 powershell 类,我正在尝试使用新的 Powershell 5 类进行写入调试和详细写入输出。
例如:
class TestWriteDebug
{
TestWriteDebug()
{
Write-Debug "Constructor called"
}
verboseOutput()
{
Write-Verbose "Method `"verboseOutput()`" was called"
}
}
我通过 [TestWriteDebug]::new() 调用它
$test = [TestWriteDebug]::new()
$test.verboseOutput()
我似乎无法弄清楚如何在创建对象或调用它的方法时传递 -debug 和 -verbose 标志,谁能告诉我这是如何实现的?
感谢您的帮助。
最佳答案
因为您将它们作为表达式的一部分来调用,所以启用它们的最简单方法可能是使用 Preference变量:
$DebugPreference = 'Continue'
$VerbosePreference = 'Continue'
$test = [TestWriteDebug]::new()
$test.verboseOutput()
要将它们重置为静音,要么退出定义这些首选项的范围,要么将值重置为 'SilentlyContinue'
。如果您想在有限的上下文中启用它们,可以在脚本 block 中执行它们:
$test = &{$DebugPreference = 'continue'; [TestWriteDebug]::new()}
关于class - Powershell 5 类,构造函数或方法中的写入调试或写入详细输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39413401/