Verbose or Debug?

This morning there was some discussion on Twitter about when to use Write-Verbose and when to use Write-Debug. They both can provide additional information about what your script or function is doing, although you have to write the code. Typically, I use Write-Verbose to provide trace and flow messages. When enabled, it makes it easier to follow what the script is doing and often the messages include variable information. Write-Debug is helpful for providing detailed debug messages, but it also has the effect of turning on debuggging when you include it.

Here’s a sample script that uses both cmdlets.

[cc lang=”PowerShell”]

#requires -version 2.0

[cmdletbinding()]
Param([string]$computername=$env:computername)

$start=Get-Date
Write-Verbose “Starting $($myinvocation.mycommand)”
Write-Debug “`$computername is $computername”
Write-Verbose “Connecting to $computername”

Try {
Write-Debug “Trying WMI”
$cs=get-wmiobject -class win32_computersystem -ComputerName $computername -errorAction Stop
}
Catch {
Write-Debug “Exception caught”
Write-Warning (“Failed to get WMI information from $computername. {0}” -f $_.Exception.Message)
}

if ($cs) {
Write-Verbose “Processing Results”
Write-Debug ($cs | Select * | Out-string)
$cs | Select Model,Manufacturer
}
$end=Get-Date
Write-Debug (“Total processing time {0}” -f ($end-$start).ToString())
Write-Verbose “Ending $($myinvocation.mycommand)”
[/cc]

The script can use the common -Verbose and -Debug common parameters because I include the [cmdletbinding()] attribute. You don’t need to define the parameters. When I run the script normally it runs as expected.

[cc lang=”DOS”]
PS S:\> .\debugdemo.ps1

Model Manufacturer
—– ————
Qosmio X505 TOSHIBA
[/cc]

When I use -Verbose, all the Write-Verbose commands “work”.

The -Debug parameter does the same thing for Write-Debug, but it also turns on debugging:

Not only do I get the Write-Debug messages, but I get a prompt for every command. I can drop into the debug prompt using the Suspend option and look at variables or run any other PowerShell commands. Some 3rd party script editors, like PrimalScript, also take advantage of Write-Debug messages. I can load the script into the editor and run it with Debug turned on (F7 or F5) and the debug messages show in the Debug window.

This ability to step through a script is very handy, but often (personally) I just need to see where I’m at in the script and Write-Verbose suffices. As you see you can have both types of commands, and you can certainly run the script with both parameters. One last note, both cmdlets require that the message be a string. When I want to write objects using either Write-Debug or Write-Verbose, I use expressions like this:

[cc lang=”PowerShell”]
Write-Debug ($cs | Select * | Out-string)
[/cc]

I encourage you to include Verbose/Debug messages from the very beginning of your script development. You only see the messages when you use the appropriate parameter. It may seem like a lot of work up front, but when the time comes to debug or trace a problem, you’ll realize it was time well spent.

PrimalForms 2009 Script Editor

primalforms2009-scriptSAPIEN’s Primal Forms 2009 now has an integrated script editor that you can use as a standalone editor for PowerShell scripts. The app has integrated help, popup command help, a PowerShell browser, a .NET object browser. As you can see in the screen shot I’ve started a very basic PowerShell script.

Once saved, I can run the script and view the results in the output panel.  You can open and edit existing scripts as well. If you want a more full featured editing experience with code samples, snippets and more, or if you still need to work in other languages like VBScript, you’ll want PrimalScript 2009. But let’s say you only require an easy to use PowerShell script editor that provides a Windows Form feature, then PrimalForms 2009 is the ticket.

One thing I think you’ll like is the ability to package the script as an EXE. PowerShell still needs to be installed on any computer where you will be running the package. When you captured_Image.pngpackage the script, you define a package name, the platform, an icon and a manifest, if required. In the platform drop down select either 32 or 64 bit Windows. Depending on what your script is doing you may need to pick a commandline platform. Otherwise, any object written to the pipeline will be displayed in a dialog box which is probably not the experience you were expecting.

You can package your script with alternate credentials as well as digitally sign the file.

captured_Image.png[5]

On the last page you can add metadata to your packaged script. This includes information such as version numbers, a description, file names and copyright.

When you are satisfied, click Build and your script will be packaged into an EXE. I love the idea of packaging my scripts. The content is protected and can’t be modified. Of course, the primary purpose for PrimalForms is to create a forms based PowerShell script which you can package captured_Image.png[7]following the same guidelines I just explained.

You can read more about the latest PrimalForms 2009 version on the SAPIEN blog.

SAPIEN New Year’s Eve Madness

It has been a tradition the last few years for SAPIEN Technologies to offer a mad-one-day-end-of-the-year special. This year is no exception.

On December 31, 2007 jump to ScriptingOutpost.com and look for this year’s New Year’s Eve special bundle.  Included will be a Primal Script 2007 Professional CD, a print copy of PrimalScript 2007: TFM, a print copy of my book WSH and VBScript Core: TFM AND the Windows PowerShell 101 Class on Disc.  Normally this is almost $500 worth of product. The one day sale price will be $249.00 plus shipping. 

So write your self a reminder and check it out on December 31st.

User Group Outreach

Did you know that SAPIEN Technologies has a user group outreach program? SAPIEN can provide your user group with information (including product trials) on SAPIEN PrimalScript, ScriptingAnswers.com LIVE! and Class-On-Disc training, and much more, for user groups who work with VBScript or Windows PowerShell. SAPIEN can provide your group’s members with training and product discounts from time to time, and even schedule expert speakers (such as Don Jones or myself) for your meetings. We even occasionally offer full product for raffles and door prizes. If you are interested, send an e-mail to training (at) sapien (period) com and provide your details to the outreach coordinator!

Technorati Tags:

The SAPIEN Technologies Story

I always like reading about the “back story” in movies. You know, the stuff that happened before the movie started or stuff that helps fill out the story. Companies and people can also have a back story. Maybe I’ll reveal some of mine someday. In the mean time, I came across SAPIEN Technologies back story at Wikipedia. Interesting reading.

Technorati Tags: