

How To Tell If The Verbose Parameter Of A Function Is From [CmdletBinding()] Or...
source link: https://thomasrayner.ca/how-to-tell-if-the-verbose-parameter-of-a-function-is-from-cmdletbinding-or-manually-added/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How To Tell If The Verbose Parameter Of A Function Is From [CmdletBinding()] Or Manually Added
Pardon the long title. I had a task recently to go through a big folder full of scripts written by random people with equally random skill levels. Lots of the scripts had a -Verbose parameter, but they weren’t all done correctly.
Some scripts correctly included the [CmdletBinding()] line above the param() block. Some just had a [Switch]$Verbose parameter (wrong). Others had both (double wrong, script won’t even run).
Consider the following three functions, which illustrate the three categories I was dealing with.
function do-verbose1 { param([switch]$Verbose) @{} + $psboundparameters }
function do-verbose2 { [cmdletbinding()] param() @{} + $psboundparameters }
function do-verbose3 { [cmdletbinding()] param([switch]$Verbose) @{} + $psboundparameters }
The first one is bad, the second one is good, the third one is double bad.
Here’s a quick way you can check the scripts using PowerShell so you don’t have to open them all up.
As you can see, the first one has a parameter of -Verbose so Get-Help will show you info about it. The second one returns nothing. The third one returns an error.
For checking real scripts that have other parameters, you should pipe the results into where-object { $_.Name -eq ‘Verbose’ } to eliminate other parameters from being returned.
Recommend
-
37
README.md Created by: Ming Bai Script type: Utility Description
-
7
Overview So I was playing around with Python decorators the other day (as you do). I always wondered if you could get Python to validate the function parameter ty...
-
5
PowerShell 小技巧:除錯好幫手 - Write-Verbose()-黑暗執行緒呼叫 PowerShell 模組函式或跑 .ps1 程式難免會遇到程式跑完但結果不如預期的狀況,要找出哪裡出錯,加 Log 是最直接有效的做法。而 PowerShell 有一件好用武器,允許事先在程式偷偷埋入偵錯資訊,...
-
17
ERROR: An HTTP request took too long to complete. Retry with –verbose to obtain debug information 2021-07-19 这两天升级 Docker 中的 mysql 容器时报的错,其实这个解决方案不够完美,但我也不清楚具体原因,所以暂且这样...
-
9
How would you create a function template with more than one type parameter? If you don’t know what a function template is, then it would help if you read our quick
-
7
New issue Make shadow_reuse suggestion less verbose #7782
-
6
-
5
Verbose ownCloud upgrade 23 Jul 2015 ownCloud offers the ability to upgrade an owncloud instance via command line. In the past this was a very quiet command and only listed the very rough steps that were executed. On instances with more...
-
12
The unreasonable effectiveness of f-strings and re.VERBOSE May 2022 ∙ six minute read ... in which we look at one or two ways to make life easier when working with Python reg...
-
6
SivaLabsMy Experiments on TechnologyShare this: Few months ago I asked on...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK