将调试消息从返回的PowerShell函数打印到控制台 [英] Print debug messages to console from a PowerShell function that returns

查看:1510
本文介绍了将调试消息从返回的PowerShell函数打印到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以将调试消息从PowerShell函数中打印出来并返回一个值?



示例:

 函数A 
{
$输出= 0

#真棒算法开始
WriteDebug#魔术函数将调试消息输出到控制台
#...
#真棒算法结束

返回$输出
}

#脚本body
$ result = A
Write-OutputResult =$ result

是否有适合此描述的PowerShell函数?



我知道 Write-Output 和Write- *,但在我所有使用这些函数的测试中像上面这样的函数不会写任何调试消息。我也知道,只是在不使用返回值的情况下调用该函数确实会导致该函数编写调试消息。 解决方案

当然,使用 Write-Debug cmdlet即可。请注意,默认情况下,您不会看到调试输出。为了查看调试输出,将 $ DebugPreference 设置为继续(而不是 SilentlyContinue )。对于简单的函数,我通常会做这样的事情:
$ b $ pre $ $ $ $ $ $ $函数A([switch] $ Debug){
if ($ Debug){$ DebugPreference ='Continue'}
Write-Debug关于某事的调试信息
#生成输出
从函数
中输出某些东西

请注意,我不建议使用 return $ output 。函数输出任何未被变量捕获,重定向到文件(或Out-Null)或转换为 [void] 的东西。如果你需要从一个函数提前返回,那么一定要用 return



对于高级函数,你可以得到调试功能更容易一些,因为PowerShell为您提供了无处不在的参数,包括 -Debug

 函数A {
[列出CmdletBinding()]
PARAM()

结束{
$ pscmdlet.WriteDebug( 调试消息)
从cmdlet输出
}
}

FYI,它是 param()语句中的 [CmdletBinding()] 属性是使得它成为高级函数的原因。 p>

也不要忘记约写冗长 $ pscmdlet.WriteVerbose()如果您只想输出与调试无关的其他信息。


Is there a way to print debug messages to the console from a PowerShell function that returns a value?

Example:

function A
{
    $output = 0

    # Start of awesome algorithm
    WriteDebug # Magic function that prints debug messages to the console
    #...
    # End of awesome algorithm

    return $output
}

# Script body
$result = A
Write-Output "Result=" $result

Is there a PowerShell function that fits this description?

I'm aware of Write-Output and Write-*, but in all my tests using any of those functions inside a function like the one above will not write any debug messages. I'm also aware that just calling the function without using the returned value will indeed cause the function to write debug messages.

解决方案

Sure, use the Write-Debug cmdlet to do so. Note that by default you will not see debug output. In order to see the debug output, set $DebugPreference to Continue (instead of SilentlyContinue). For simple functions I will usually do something like this:

function A ([switch]$Debug) {
    if ($Debug) { $DebugPreference = 'Continue' }
    Write-Debug "Debug message about something"
    # Generate output
    "Output something from function"
}

Note that I would not recommend using the form return $output. Functions output anything that isn't captured by a variable, redirected to a file (or Out-Null) or cast to [void]. If you need to return early from a function then by all means use return.

For advanced functions you can get debug functionality a bit more easily because PowerShell provides the ubiquitous parameters for you, including -Debug:

function A {
    [CmdletBinding()]
    param()

    End {
        $pscmdlet.WriteDebug("Debug message")
        "Output something from cmdlet"
    }
}

FYI, it's the [CmdletBinding()] attribute on the param() statement is what makes this an advanced function.

Also don't forget about Write-Verbose and $pscmdlet.WriteVerbose() if you just want a way to output additional information that isn't debug related.

这篇关于将调试消息从返回的PowerShell函数打印到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆