我正在学习PowerShell,并且正在使用Write-Host检查新PowerShell脚本文件中的变量赋值.然后我读了这篇文章:http: //windowsitpro.com/blog/what-do-not-do-powershell-part-1
所以,在我的.ps1文件中,我替换了这样的语句:
Write-Host "Start"
Write-Host "End"
Run Code Online (Sandbox Code Playgroud)
... 有了这个:
Write-Debug "Start"
Write-Debug "End"
Run Code Online (Sandbox Code Playgroud)
但是当我在Windows PowerShell ISE中运行保存的脚本时,没有输出写入控制台.我将-debug附加到调用脚本的语句中,如下所示:
PS E:\trialrun> .\MyScript.ps1 -debug
Run Code Online (Sandbox Code Playgroud)
但同样,输出不会写入控制台.显然我正在使用Write-Debug错误.如何将调试输出写入控制台?谢谢.
mkl*_*nt0 26
tl;博士:
运行$DebugPreference = 'Continue'以开始查看来自Write-Debug呼叫的输出.
完成后,使用将恢复首选项变量恢复$DebugPreference为默认值$DebugPreference = 'SilentlyContinue'
是否Write-Debug打印语句的输出由两种机制控制:
范围范围:通过$DebugPreference首选变量的值- 请参阅Get-Help about_Preference_Variables.
Ad-hoc,命令范围,当使用common参数调用cmdlet或高级脚本/函数(覆盖$DebugPreference值)时- 请参阅.-DebugGet-Help about_CommonParameters
$DebugPreference默认为SilentlyContinue,这解释了为什么Write-Debug默认情况下您没有看到语句的任何输出.
当你使用common参数时-Debug,你基本上$DebugPreference 只为被调用的命令设置,并且你总是将它设置为值Inquire,这不仅会打印 Write-Debug消息,还会在每个这样的语句中暂停,以询问你想如何继续.
Continue公共参数的自定义脚本或函数,必须使用-Debug其[CmdletBinding()]块的属性声明它,如Mathias的答案所示.由于这种即时param()通知行为可能具有破坏性,因此Write-Debug可能是更好的方法.
Mat*_*sen 11
CmdletBinding如果要支持常用参数(包括-Debug),则需要脚本中的属性:
[CmdletBinding()]
param()
Write-Debug Start
Write-Debug End
Run Code Online (Sandbox Code Playgroud)
我建议看看about_Functions_CmdletBindingAttribute帮助文件
| 归档时间: |
|
| 查看次数: |
12081 次 |
| 最近记录: |