是否可以检查 Powershell 中是否给出了 -Verbose 参数? [英] Is it possible to check if -Verbose argument was given in Powershell?

查看:42
本文介绍了是否可以检查 Powershell 中是否给出了 -Verbose 参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了自己的 Powershell 日志记录函数 Log,其中包含参数 stream(在哪个流上写入消息)和 message(消息写).

I have written my own Powershell logging function Log with parameters stream (on which stream to write the message) and message (the message to write).

这个想法是我可以将输出写入控制台和日志文件.我在函数中所做的基本上是确定在哪个流上发布消息(使用 switch 语句),然后将消息写入流和日志文件:

The idea is that i can write the outputs both to the console and to a log-file. What I do in the function is basically determine on which stream to publish the message (with a switch statement) and then write the message to the stream and the log-file:

switch ($stream) {
    Verbose {
        Write-Output "$logDate [VERBOSE] $message" | Out-File -FilePath $sgLogFileName -Append
        Write-Verbose $message
        break
    }
}

现在的问题是,是否可以检查是否给出了 -Verbose 参数?

The question is now, is it possible to check if the -Verbose argument was given?

目标是仅在给出 -Verbose 时才将消息写入日志文件.

The goal is to write the message to the log-file only if the -Verbose was given.

我已经查看了以下帮助文档,但没有找到任何有用的内容:
- 帮助 about_Parameters
- 帮助 about_commonparameters

I looked already in the following help docs but didn't find anything helpful:
- help about_Parameters
- help about_commonparameters

此外,-WhatIf 参数不适用于 Write-Verbose.

Also, the -WhatIf parameter does not work with Write-Verbose.

非常感谢您的回答!

推荐答案

在你的脚本中检查:

$PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent

这篇关于是否可以检查 Powershell 中是否给出了 -Verbose 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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