Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用powershell进行预处理器/宏扩展_Powershell_Powershell 2.0_Preprocessor - Fatal编程技术网

使用powershell进行预处理器/宏扩展

使用powershell进行预处理器/宏扩展,powershell,powershell-2.0,preprocessor,Powershell,Powershell 2.0,Preprocessor,有时,我会在脚本中使用write host来执行穷人调试/日志记录,但每次运行时都会触发所有的调试/日志记录会让我的屏幕上堆满大量不必要的信息。 当然,我可以注释/取消注释当前运行所需的内容,但这需要大量编辑,而且非常容易出错 以下是我最终得出的结论: [cmdletbinding()] param( [parameter()] [string[]]$dbg_points=@('*') ) $debug = ($PSCmdlet.myInvocation.BoundPara

有时,我会在脚本中使用
write host
来执行穷人调试/日志记录,但每次运行时都会触发所有的调试/日志记录会让我的屏幕上堆满大量不必要的信息。
当然,我可以注释/取消注释当前运行所需的内容,但这需要大量编辑,而且非常容易出错

以下是我最终得出的结论:

[cmdletbinding()]
param(
    [parameter()]
    [string[]]$dbg_points=@('*')
)

$debug   = ($PSCmdlet.myInvocation.BoundParameters['Debug'].isPresent -eq $true)
$dbg_all = $dbg_points -contains '*'

if(($debug) -and ($dbg_all -or ($dbg_points -contains 'foo'))) { Write-host '@foo debug point' }
if(($debug) -and ($dbg_all -or ($dbg_points -contains 'bar'))) { Write-host '@bar debug point' }

if(($debug) -and ($dbg_all -or ($dbg_points -contains 'foo') -or ($dbg_points -contains 'bar'))) { Write-host 'common debug point' }
这项工作可以完成,但在我想要获得一些调试/日志信息的每个地方都需要大量键入,特别是对于公共点

使用其他语言,我会定义某种宏/预处理器指令,比如说
DBG(foo){…}
DBG(foo,bar){…}
,它们会分别扩展到

if(($debug) -and ($dbg_all -or ($dbg_points -contains 'foo'))) {...}

但我认为这在powershell中是不可能的

你知道一种更好、更像地狱的方法吗


哦,我忘了告诉你。。。我被那台计算机上的v2卡住了,无法安装更新的版本

似乎这就是
Write Debug
设计用于…使用函数(或日志记录/等框架)的那种东西.Bill_Stewart:
写调试
只是一个
写主机
的国王,只有在命令行上使用-debug时才会触发,但它也会暂停执行,并且没有任何条件。如果你不喜欢
写调试
,你也可以使用
写详细的
。Bill_Stewart:我的问题与两者之间的关系不大{和}但要看前面发生的事情。我的问题是如何在保持scriptblock的有条件执行的同时防止所有这些键入。
if(($debug) -and ($dbg_all -or ($dbg_points -contains 'foo') -or ($dbg_points -contains 'bar'))) {...}