How does a cmdlet know when it really should call WriteVerbose()
,
WriteDebug()
and etc.?
Perhaps I miss something simple but I cannot find the answer. All cmdlet
implementations I have seen so far just call WriteVerbose()
without any
hesitation. I know that it is correct to do so, but it is not effective.
Performance suffers when verbose mode is off but a cmdlet still prepares
data for WriteVerbose()
call, that is, for nothing.
In other words, in a cmdlet I would like to be able to:
if (<VerboseMode>)
{
.... data preparation, sometimes expensive ...
WriteVerbose(...);
}
But I don't know how to get this if (<VerboseMode>)
. Any ideas?
Conclusion: The @stej’s answer shows how get the required information in theory. In practice this is hacky and unlikely suitable. Thus, if a cmdlet produces really expensive verbose or debug output then it looks reasonable to introduce an additional parameter specifying verbosity levels.