Debugging with PowerShell
The test code sample has some other "default" and built in features that I want to point out. These items need to be considered during the design phase as they will have an impact on how the CmdLet is coded.
The Debug Switch ( -debug )
The "debug" switch is built into PowerShell and is applied to code in every CmdLet. The code elements (or methods) that are affected are: WriteVerbose, WriteDebug, WriteError and WriteWarning. Each of these is affected in different ways determined by the following.
- The verbose switch
- The debug switch
- $DebugPreferences
- $VerbosePreferences
- $ReportErrorShowSource
- $ReportErrorShowStackTrace
- $ReportErrorShowExceptionClass
- $ReportErrorShowInnerException
- $WarningPreference
- $ConfirmPreference
The test code uses "WriteVerbose" to add information about the execution of the command. Adding the "-verbose" switch to the CmdLet or changing the $VerbosePreference variable will allow this output to be displayed. The "-debug" switch has an affect on the output also.
My initial use of WriteVerbose is to allow the operator to see which computers are being skipped when the "-filter" switch has been defined. This can be useful for documenting what machines have not been contacted. This will probably change to a more complex filter that can handle multiple conditions along with the ability to define a log file or log variable. For now I am just testing different approaches to the layout and design of the CmdLet.
The Verbose Switch ( -verbose )
As above this switch allows output from the WriteVerbose method. I have sued it only for trace like info and outputting the failed pings. Currently it also outputs the value of the Parameter set chosen by PowerShell because of the selected switches or the input variable type.(edit1)
PS>get-computer -file d:\computers.txt -verbose
VERBOSE: FileSet
Services Name IsPingable
-------- ---- ----------
{Alerter, ALG, AppMgmt, aspnet_state...} omega True
test1 False
{Alerter, ALG, AppMgmt, aspnet_state...} omega True
test2 False
{Alerter, ALG, AppMgmt, aspnet_state...} omega True
Above shows that the "FileSet" parameterset was chosen for processing. With this set the "ping" verbose messages are purposely surpressed.
If we choose to add the-filter switch then we get more info:
PS>get-computer -file d:\computers.txt -verbose -filter
VERBOSE: FileSetVERBOSE: Successful ping omega
VERBOSE: Unable to ping test1
VERBOSE: Successful ping omega VERBOSE: Unable to ping test2
VERBOSE: Successful ping omega
By factoring this and other environmentall controllable behaviors we can optimize the flexibility and efficiency of the CmdLet and it's coding.
No comments:
Post a Comment