Powershell 5 class,构造函数或方法中的写入调试或写入详细输出
Powershell 5 class, write-debug or write-verbose output in constructor or method
我有一个 powershell class,在使用新的 Powershell 5 class.
时,我正在尝试进行写调试和写详细输出
例如:
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'
。如果你想在有限的上下文中启用它们,在脚本块中执行它们可以这样做:
$test = &{$DebugPreference = 'continue'; [TestWriteDebug]::new()}
类 以类似于 Cmdlet 的方式运行(即默认情况下 CmdletBinding 行为就位)。要显示这些方法,只需在调用使用此 class
的 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
输出:
展开下面的代码片段,然后单击 Run Code Snippet
查看预期的 PS 输出。
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>
更多信息
如果你想让这个对整个脚本生效;不仅仅是那些使用相关开关调用的 cmdlet,将以下内容添加到脚本文件的顶部。
[CmdletBinding()]
param()
我有一个 powershell class,在使用新的 Powershell 5 class.
时,我正在尝试进行写调试和写详细输出例如:
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'
。如果你想在有限的上下文中启用它们,在脚本块中执行它们可以这样做:
$test = &{$DebugPreference = 'continue'; [TestWriteDebug]::new()}
类 以类似于 Cmdlet 的方式运行(即默认情况下 CmdletBinding 行为就位)。要显示这些方法,只需在调用使用此 class
的 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
输出:
展开下面的代码片段,然后单击 Run Code Snippet
查看预期的 PS 输出。
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>
更多信息
如果你想让这个对整个脚本生效;不仅仅是那些使用相关开关调用的 cmdlet,将以下内容添加到脚本文件的顶部。
[CmdletBinding()]
param()