I recently wrote a post about how to create log files with PowerShell transcription capabilities.
What I like about PowerShell transcripts is simplicity. However, as I said in my former post if you want to get the most of it, you should use the Verbose parameter with each cmdlet you launch. Thus, when a warning or an error happens, you know in which context it happened.
But some cmdlets are very, very, very verbose… For example, FSRM cmdlets are some of those extremely verbose cmdlets. In such cases, instead of writing:
New-FSRMQuota -Path C:\FolderToMonitor -Template 'TemplateName' -Verbose
You probably want to write something like:
Write-Verbose -Message "New FSRM quota for $Path with template $TemplateName" New-FSRMQuota -Path $Path -Template $TemplateName
Unfortunately, if you did not a specific action before, nothing will be captured in the transcript file. Let’s see why…
What is captured
To have a global overview of the different possibilities you have here is an example of script.
Start-Transcript -Path C:\Temp\Transcript.log Write-Output 'Output message' Write-Warning -Message 'Warning message' Write-Debug -Message 'Debug message' Write-Verbose -Message 'Verbose message' Write-Information -MessageData 'Information message' Write-Host 'Host message' Write-Error -Message 'Error message' throw 'Throw exception' Stop-Transcript #this line should never been reached
As you can notice in the output, not everything goes to the console.
Write-Information don’t display anything.
And if you have a look at the log file, nothing has been capture neither for these three cmdlets.
Note: I was a little surprised because despite the Write-Host cmdlet does not go down the pipeline, it has still been captured by the transcript.
How to capture Information, Debug, and Verbose messages
In fact, the behavior we experienced above happens because of the default values of the preference variables.
Thus if you want to display and capture messages for
Write-Information cmdlets, you just have to set their preference variables to Continue.
Start-Transcript -Path C:\Temp\Transcript.log $DebugPreference = 'Continue' $VerbosePreference = 'Continue' $InformationPreference = 'Continue' Write-Output 'Output message' Write-Warning -Message 'Warning message' Write-Debug -Message 'Debug message' Write-Verbose -Message 'Verbose message' Write-Information -MessageData 'Information message' Write-Host 'Host message' Write-Error -Message 'Error message' throw 'Throw exception' Stop-Transcript #this line should never been reached
As you can notice now, everything is captured.
When you set preference variables inside a function, the impact is limited to the function.
However, when you set preference variables inside a script, they continue to apply to the current host even after the script has finished.
The Write-Verbose case
Depending on the cmdlets, scripts, and function you will use, preference variables will apply differently.
For example, with the
New-Item cmdlet, the $VerbosePreference variable set to Continue has no impact. As you can see in the following example, you have to use the Verbose parameter in order to get some verbose output.
Conversely, and as you can see in the following example, there is no need to use the Verbose parameter with the
Import-Module when the $VerbosePreference variable is set to Continue.
While setting the Information and Debug variable to Continue has no big impact, it becomes quickly an annoyance with the Verbose preference variable. Then how can we output and capture messages from the
Write-Verbose cmdlet without setting the $VerbosePreference variable to Continue?
Just use the Verbose parameter with the
$VerbosePreference = 'SilentlyContinue' Write-Verbose -Message "This is a verbose message without the verbose parameter" Write-Verbose -Message "This is a verbose message with the verbose parameter" -Verbose
Based on my personal experience, to get the most of your transcript log files:
- Use the Verbose parameter with most, if not all, cmdlets
- Keep the $VerbosePreference variable set to SilentlyContinue and use the Verbose parameter with the
- If needed, you can set the $DebugPreference and the $InformationPreference variables to Continue
And you? How do you get the most of your transcript log files?
Feel free to share your personal tips and tricks in the comment section below…