{"id":526,"date":"2008-07-23T06:00:07","date_gmt":"2008-07-23T14:00:07","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/07\/22\/fun-with-debug-messages\/"},"modified":"2008-07-22T04:43:27","modified_gmt":"2008-07-22T12:43:27","slug":"fun-with-debug-messages","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/07\/23\/fun-with-debug-messages\/","title":{"rendered":"Fun with Debug Messages"},"content":{"rendered":"<p>In my script and function development, I often add the line $DebugPreference=&quot;SilentlyContinue&quot; to the beginning of my code. Throughout the script I add lines using Write-Debug that tell me what the script is doing or the value of a particular variable. This has been very helpful. When I want to see the debug messages, I set $DebugPreference to &quot;Continue&quot; and voila!.&nbsp; In PrimalScript all the messages are written to the debug panel.&nbsp; But I wanted to do more than merely have a line like:<\/p>\n<p><font color=\"#0000ff\">write-debug &quot;creating user account&quot;<\/font><\/p>\n<p>I wanted to see more information like a time stamp or user credentials. I also found it difficult sometimes to pass object properties to the debug pipeline. So I came up with a function I call New-DebugMessage.<\/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: rgb(244, 244, 244);\">\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: rgb(244, 244, 244);\">\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: rgb(96, 96, 96);\">   1:<\/span> Function New-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: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">   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: rgb(96, 96, 96);\">   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: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">   4:<\/span>        <span style=\"color: rgb(0, 128, 0);\"># 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: white;\"><span style=\"color: rgb(96, 96, 96);\">   5:<\/span>        <span style=\"color: rgb(0, 128, 0);\"># to different pipelines like Debug<\/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: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">   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: rgb(96, 96, 96);\">   7:<\/span>        <span style=\"color: rgb(0, 128, 0);\">#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: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">   8:<\/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: white;\"><span style=\"color: rgb(96, 96, 96);\">   9:<\/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: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  10:<\/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: white;\"><span style=\"color: rgb(96, 96, 96);\">  11:<\/span>        $timestamp=(Get-Date -Format g).toString()<\/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: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  12:<\/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: white;\"><span style=\"color: rgb(96, 96, 96);\">  13:<\/span>        $<span style=\"color: rgb(0, 0, 255);\">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: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  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: rgb(96, 96, 96);\">  15:<\/span>        [string]$msg=<span style=\"color: rgb(0, 96, 128);\">&quot;[{0}:{1}\\{2}] {3} Line:{4} {5}&quot;<\/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: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  16:<\/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: rgb(96, 96, 96);\">  17:<\/span>        write $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: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  18:<\/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: rgb(96, 96, 96);\">  19:<\/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: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  20:<\/span>  <\/pre>\n<\/div>\n<\/div>\n<p>The function returns a string that can be used by Write-Debug:<\/p>\n<p><font color=\"#0000ff\">Write-Debug (New-DebugMessage &quot;Getting powershell process&quot;)<\/font><\/p>\n<p>The string is passed to the function. Within the function I&#8217;ve set some variables that you might find handy in your debug message. Most of them should be self-explanatory. The $line variable will display the line number of your script where the Write-Debug message occurs. If you place your Write-Debug command before key commands, it should be easy to jump to those sections.<\/p>\n<p>Here&#8217;s a short script that I tested this with.<\/p>\n<p><font color=\"#0000ff\">$debugpreference=&quot;continue&quot; <\/font><\/p>\n<p><font color=\"#0000ff\">Write-Debug (New-DebugMessage &quot;starting $($myinvocation.invocationname)&quot;)<br \/>\nWrite-Debug (New-DebugMessage &quot;Getting Date&quot;)<br \/>\nGet-Date<br \/>\nWrite-Debug (New-DebugMessage &quot;Getting powershell process&quot;)<br \/>\nGet-Process powershell<br \/>\nWrite-Debug (New-DebugMessage &quot;finished&quot;)<\/font><\/p>\n<p>When I run the script I get this output (click the image for a larger view):<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/07\/debug.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" height=\"79\" border=\"0\" width=\"244\" style=\"border: 0px none ;\" alt=\"debug\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/07\/debug-thumb.png\" \/><\/a><\/p>\n<p>Does this look useful to you?&nbsp; What other things would you like to add or did you add to your debug messages?<\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:0b4cf4e4-3920-40bb-b7df-9729660c9416\" style=\"margin: 0px; padding: 0px; display: inline;\">\n<p>Download the function <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/07\/new-debugmessage.txt\" target=\"_blank\">here.<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In my script and function development, I often add the line $DebugPreference=&#8221;SilentlyContinue&#8221; to the beginning of my code. Throughout the script I add lines using Write-Debug that tell me what the script is doing or the value of a particular variable. This has been very helpful. When I want to see the debug messages, I set $DebugPreference to &#8220;Continue&#8221; and voila!.  In PrimalScript all the messages are written to the debug panel.  But I wanted more .<\/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":[222,198,28,57,255],"class_list":["post-526","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-debugging","tag-function","tag-powershell","tag-scripting","tag-write-debug"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/526","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=526"}],"version-history":[{"count":3,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/526\/revisions"}],"predecessor-version":[{"id":570,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/526\/revisions\/570"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}