{"id":572,"date":"2008-08-06T09:28:02","date_gmt":"2008-08-06T17:28:02","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=572"},"modified":"2008-08-06T09:30:01","modified_gmt":"2008-08-06T17:30:01","slug":"more-fun-with-debug-messages","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/08\/06\/more-fun-with-debug-messages\/","title":{"rendered":"More Fun with Debug Messages"},"content":{"rendered":"<p>A few weeks ago I posted an <a href=\"http:\/\/www.sapien.com\/blog\/2008\/07\/23\/fun-with-debug-messages\" target=\"_blank\">entry<\/a> on creating customized debug messages that you can use in your scripting. After some more testing and tweaking I revised and renamed the function. Write-DebugMessage can now be used within your script to send messages to the debug pipeline. I moved the $debugpreference setting to the function itself. If you set it to Continue,the message will be written to the debug pipeline.<\/p>\n<p>The other major change to the function is that I reformatted the date time to reflect milliseconds.<\/p>\n<p><span style=\"font-family: Consolas; color: #0000ff;\">$timestamp=get-date -f &#8216;MM\/dd\/yyyy HH:mm:ss:ffff&#8217; <\/span><\/p>\n<p>It all works great. But one of my online PowerShell students asked about saving debug messages to a file. That seemed like a reasonable request. Unfortunately, PowerShell v1.0 doesn&#8217;t appear to have any provision for redirecting the debug pipeline. Undaunted I dug in and came up with this solution.<\/p>\n<p>When you record a PowerShell transcript, everything, including debug messages is written to it. My solution was to create a transcript file, parse out the debug messages and save them to a new file.<\/p>\n<div style=\"border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; font-size: 8pt; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\">\n<div style=\"border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\">\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   1:<\/span> $debuglog=$TRUE<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   2:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   3:<\/span> <span style=\"color: #0000ff;\">if<\/span> ($debuglog) {<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   4:<\/span>     <span style=\"color: #008000;\">#define a temporary name for the transcript file<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   5:<\/span>     $transcriptName=<span style=\"color: #006080;\">\"{0}_transcript.txt\"<\/span> -f $myinvocation.mycommand.name<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   6:<\/span>     $transcript=(Join-Path \"C:\\\" $transcriptName )<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   7:<\/span>     Start-Transcript $transcript | Out-Null<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   8:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   9:<\/span> }<\/pre>\n<\/div>\n<\/div>\n<p>Set the $debuglog variable to $True if you want to log your debug messages. If so, I define a transcript name generated from the script name which I pull from $myinvocation.mycommand.name. I could probably parse it further to drop the extension but it works and it is temporary for the most part. You must start the transcript before you have any debug message code.<\/p>\n<p>At the end of the script, I have similar code to create a debug log file based on the script name.<\/p>\n<div style=\"border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; font-size: 8pt; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\">\n<div style=\"border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\">\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   1:<\/span> <span style=\"color: #0000ff;\">if<\/span> ($debuglog) {<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   2:<\/span>     $filename=<span style=\"color: #006080;\">\"{0}_debug.txt\"<\/span> -f $myinvocation.mycommand.name<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   3:<\/span>     $debuglogfile=(Join-Path <span style=\"color: #006080;\">\"c:\\\" $filename)<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   4:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   5:<\/span>     Stop-Transcript | Out-Null<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   6:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   7:<\/span>     Get-Content $transcript | Select-String -Pattern \"DEBUG:<span style=\"color: #006080;\">\" | Out-File $debuglogfile<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   8:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   9:<\/span>     notepad $debuglogfile<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  10:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  11:<\/span>     $rc=Read-Host \"Do you want to delete $transcript [YN]?<span style=\"color: #006080;\">\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  12:<\/span>     if ($rc -match \"y\") {<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  13:<\/span>         Remove-Item $transcript<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  14:<\/span>     }<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  15:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  16:<\/span> }<\/pre>\n<\/div>\n<\/div>\n<p>The Get-Content expression parses out the debug lines and writes them to the debug log file. At the end, I&#8217;ve added code to prompt you to delete the temporary transcript file.<\/p>\n<p>Here&#8217;s a sample script that uses the Write-DebugMessage function and my debug logging code.<\/p>\n<div style=\"border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; font-size: 8pt; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\">\n<div style=\"border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\">\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   1:<\/span> Function Write-DebugMessage {<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   2:<\/span>        Param([string]$text)<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   3:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   4:<\/span>        <span style=\"color: #008000;\">#set to Continue to turn on debug messages<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   5:<\/span>        $debugpreference=<span style=\"color: #006080;\">\"continue\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   6:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   7:<\/span>        <span style=\"color: #008000;\"># build a text string of information that can be passed<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   8:<\/span>        <span style=\"color: #008000;\"># to the Debug pipeline<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   9:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  10:<\/span>        <span style=\"color: #008000;\">#here are some variables you might want to work with<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  11:<\/span>        $computer=$env:computername<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  12:<\/span>        $domain=$env:userdomain<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  13:<\/span>        $user=$env:username<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  14:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  15:<\/span>        <span style=\"color: #008000;\">#return a timestamp value with milliseconds<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  16:<\/span>        <span style=\"color: #008000;\">#like 08\/06\/2008 11:08:37:4361<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  17:<\/span>        <span style=\"color: #008000;\">#Or you can use an expression like this<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  18:<\/span>        <span style=\"color: #008000;\">#(Get-Date -Format g).toString()<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  19:<\/span>        $timestamp=get-date -f <span style=\"color: #006080;\">'MM\/dd\/yyyy HH:mm:ss:ffff'<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  20:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  21:<\/span>        $line=$($myinvocation.scriptlinenumber)<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  22:<\/span>        $<span style=\"color: #0000ff;\">script<\/span>=$($myinvocation.invocationname)<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  23:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  24:<\/span>        [string]$msg=<span style=\"color: #006080;\">\"[{0}:{1}\\{2}] {3} Line:{4} {5}\"<\/span> -f $computer,$domain,$user,$timestamp,$line,$text<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  25:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  26:<\/span>        Write-Debug $msg<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  27:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  28:<\/span>    }<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  29:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  30:<\/span> Function Foo-Me {<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  31:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  32:<\/span>    Write-DebugMessage <span style=\"color: #006080;\">\"in function $($myinvocation.invocationname)\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  33:<\/span>    Write-DebugMessage <span style=\"color: #006080;\">\"getting services\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  34:<\/span>    Get-Service<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  35:<\/span>    Write-DebugMessage <span style=\"color: #006080;\">\"exiting function\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  36:<\/span> }<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  37:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  38:<\/span> <span style=\"color: #008000;\">#setting this to $TRUE doesn't do any good if the debug pipeline is <\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  39:<\/span> <span style=\"color: #008000;\">#turned off    <\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  40:<\/span> debuglog=$TRUE<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  41:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  42:<\/span> <span style=\"color: #0000ff;\">if<\/span> ($debuglog) {<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  43:<\/span>    <span style=\"color: #008000;\">#define a temporary name for the transcript file<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  44:<\/span>    $transcriptName=<span style=\"color: #006080;\">\"{0}_transcript.txt\"<\/span> -f $myinvocation.mycommand.name<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  45:<\/span>    $transcript=(Join-Path <span style=\"color: #006080;\">\"C:\\\" $transcriptName ) <\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  46:<\/span>    Start-Transcript $transcript | Out-Null<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  47:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  48:<\/span> write-DebugMessage \"starting $($myinvocation.invocationname)<span style=\"color: #006080;\">\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  49:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  50:<\/span> sleep 4<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  51:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  52:<\/span> write-DebugMessage \"Getting Date<span style=\"color: #006080;\">\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  53:<\/span> get-Date<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  54:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  55:<\/span> write-DebugMessage \"Getting powershell <span style=\"color: #0000ff;\">process<\/span><span style=\"color: #006080;\">\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  56:<\/span> get-Process powershell<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  57:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  58:<\/span> write-DebugMessage \"Calling Foo-Me<span style=\"color: #006080;\">\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  59:<\/span> foo-Me<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  60:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  61:<\/span> write-DebugMessage \"Getting WMI Information<span style=\"color: #006080;\">\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  62:<\/span> $d=Get-WmiObject win32_logicaldisk -filter \"deviceid=<span style=\"color: #006080;\">'c:'<\/span><span style=\"color: #006080;\">\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  63:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  64:<\/span> write-DebugMessage (\"Drive C size is {0}GB<span style=\"color: #006080;\">\" -f ([int]($d.Size\/1GB)))<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  65:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  66:<\/span> a=@(1,2,3,4,5)<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  67:<\/span> write-DebugMessage $a<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  68:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  69:<\/span> write-DebugMessage \"finished<span style=\"color: #006080;\">\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  70:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  71:<\/span> if ($debuglog) {<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  72:<\/span>    $filename=\"{0}_debug.txt<span style=\"color: #006080;\">\" -f $myinvocation.mycommand.name<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  73:<\/span>    $debuglogfile=(Join-Path \"c:\\<span style=\"color: #006080;\">\" $filename)<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  74:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  75:<\/span>    Stop-Transcript | Out-Null<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  76:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  77:<\/span>    Get-Content $transcript | Select-String -Pattern \"DEBUG:<span style=\"color: #006080;\">\" | Out-File $debuglogfile<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  78:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  79:<\/span>    notepad $debuglogfile<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  80:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  81:<\/span>    $rc=Read-Host \"Do you want to delete $transcript [YN]?<span style=\"color: #006080;\">\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  82:<\/span>    if ($rc -match \"y\") {<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  83:<\/span>        Remove-Item $transcript<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  84:<\/span>    }<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  85:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  86:<\/span><\/pre>\n<\/div>\n<\/div>\n<p>There is one major caveat to this technique which may or not be relevant to you. PowerShell only allows one transcript per session. If you are running a transcript in your PowerShell console and try to run this script you&#8217;ll get an error message and no debug log will be created.<\/p>\n<p>If you have any suggestions on this topic, I&#8217;d love to hear them.<\/p>\n<div id=\"scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:827183ff-597f-4379-a2b8-8cc932e0d6c4\" class=\"wlWriterSmartContent\" style=\"padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<p>You can download the debug demo script <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/08\/demo-debuglog.txt\" target=\"_blank\">here<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A few weeks ago I posted an entry on creating customized debug messages that you can use in your scripting. After some more testing and tweaking I revised and renamed the function. Write-DebugMessage can now be used within your script to send messages to the debug pipeline. I moved the $debugpreference setting to the function itself and made other improvements.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[25],"tags":[259,260,28,57,261,255],"class_list":["post-572","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-debug","tag-get-date","tag-powershell","tag-scripting","tag-transcript","tag-write-debug"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=572"}],"version-history":[{"count":1,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/572\/revisions"}],"predecessor-version":[{"id":573,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/572\/revisions\/573"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}