debugging - Powershell 不会将详细级别传递给被调用的函数

标签 debugging powershell scripting verbosity

考虑以下脚本:

function a{
    [CmdletBinding()]
    Param()
    Write-Verbose "A VERBOSE"
    Write-Host "A NORMAL"
}

function b{
    [CmdletBinding()]
    Param()

    Write-Verbose "B VERBOSE"
    Write-Host "B NORMAL"

    a
}

b -Verbose

如果我们使用指定的详细参数开关调用函数“b”,则函数“a”(在“b”中调用)也会使用隐式详细参数调用。有什么办法可以避免这种情况吗? (换句话说,使用 Verbose 开关调用“b”,不使用它调用“a”)。

最佳答案

如果你想抑制来自外部函数 ba 的详细输出,你可以使用 $PSDefaultParameterValues 变量,从 PowerShell 开始v3.

function a{
    [CmdletBinding()]
    Param()
    Write-Verbose "A VERBOSE"
    Write-Host "A NORMAL"
}

function b{
    [CmdletBinding()]
    Param()

    Write-Verbose "B VERBOSE"
    Write-Host "B NORMAL"

    a
}

$PSDefaultParameterValues['a:Verbose'] = $False
b -Verbose

对于 PowerShell v2,当您从 b 函数调用 a 时,您必须将 verbose 设置为 $False

function b{
    [CmdletBinding()]
    Param()

    Write-Verbose "B VERBOSE"
    Write-Host "B NORMAL"

    a -Verbose:$false
}

关于debugging - Powershell 不会将详细级别传递给被调用的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23277783/

相关文章:

bash - 如何比较 Bash 中 'if' 语句中的两个字符串变量?

html - 可以在 Mac、PC 或其他设备上镜像 iOS Safari 调试控制台吗?

javascript - 在不创建 html/javascript 测试文件的情况下在浏览器中测试 JavaScript 脚本

user-interface - 从 Web GUI 远程执行交互式 shell 脚本

powershell - 使用不同的凭据运行 powershell 脚本

file - PowerShell:设置内容与 "file already in use"有问题

bash - 替换exec中的部分文件路径

linux - 什么时候需要使用gcc/g++生成Map文件? "-g"足以进行调试吗?

debugging - 无法在 Android 设备上调试 Unity 应用程序/Debug 和 profiler 无法正确连接到手机

powershell - NuGet install.ps1 脚本中的 $package 和 $project 参数包含什么?