{"id":131,"date":"2021-12-03T11:19:59","date_gmt":"2021-12-03T11:19:59","guid":{"rendered":"https:\/\/monitoringstuff.com\/?p=131"},"modified":"2021-12-03T11:20:00","modified_gmt":"2021-12-03T11:20:00","slug":"debugging-a-script-that-has-no-bugs","status":"publish","type":"post","link":"https:\/\/monitoringstuff.com\/?p=131","title":{"rendered":"Debugging a script that has no bugs"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Sometimes, simple things can take many hours to solve. While working on a custom MP that contains a Powershell script, I kept getting event 22406 (Powershell script error), stating <code>\"System.Management.Automation.ParseException:&nbsp;Missing closing '}' in statement block or type definition.\"<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, the error in itself seems quite obvious &#8211; and the search for the opening curly brace began. It&#8217;s not exactly my first rodeo when it comes to debugging semi-colons, single and double quotes, brackets and braces &#8211; syntax highlighting saved my bacon plenty of times. And although VSAE ( my MP building tool of choice) isn&#8217;t very good at Powershell syntax highlighting, it&#8217;s easy to open the script in Powershell ISE. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But even there, I found nothing wrong &#8211; the curly braces all neatly matched up. Then I ran the script on the targeted server, and&#8230; it worked fine. \ud83e\udd28<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To make sure I didn&#8217;t have any hidden characters, I copied the script content to Notepad, deleted it in VSAE and copy\/pasted it back from Notepad. Hit F5 to build and deploy to my test SCOM environment&#8230; no errors. \ud83d\ude42<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Checked the event log&#8230; and *poof*. The 22406es were back. \ud83d\ude20<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I then replaced the script contents with only a few lines (those of you that use Kevins MP fragments will recognize them):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span class=\"has-inline-color has-bright-blue-color\">$ScriptName = \"MyMPScript.ps1\"\n$EventID = \"1234\"\n#=================================================================================\n\n\n# Starting Script section - All scripts get this\n#=================================================================================\n# Gather the start time of the script\n$StartTime = Get-Date\n#Set variable to be used in logging events\n$whoami = whoami\n# Load MOMScript API\n$momapi = New-Object -comObject MOM.ScriptAPI\n#Log script event that we are starting task\n$momapi.LogScriptEvent($ScriptName,$EventID,0,\"`n Script is starting. `n Running as ($whoami). `n Management Group: ($MGName).\")\n#=================================================================================\n<\/span><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, I dare you to find <em>any<\/em> curly brace in there&#8230; yep. There aren&#8217;t any. Hit F5 to build and deploy to my test SCOM environment&#8230; no errors. \ud83d\ude42<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Checked the event log&#8230; and *poof*. The 22406es were back&#8230;  <code>\"System.Management.Automation.ParseException:&nbsp;Missing closing '}' in statement block or type definition.\"<\/code>  \ud83d\ude36<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So I opened the file again&#8230; removed al the contents and saved it. I checked the folder, expecting to find the file with 0 bytes length. However, the folder view kept showing 1kb (which is the minimum an Explorer window will show). Checked the properties&#8230; 3 bytes. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s when it dawned on me. I opened the file in Notepad++ and yep&#8230; for some reason, the file had become encoded as <a href=\"https:\/\/en.wikipedia.org\/wiki\/Byte_order_mark\" title=\"https:\/\/en.wikipedia.org\/wiki\/Byte_order_mark\">UTF-8-BOM<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/monitoringstuff.com\/wp-content\/uploads\/2021\/11\/image.png\" alt=\"\" class=\"wp-image-132\" width=\"147\" height=\"101\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Switched the file to UTF-8, repasted the content&#8230; saved&#8230;  hit F5 to build and deploy to my test SCOM environment&#8230; no errors. So far, so good. Checked the event log&#8230; and saw event 1234 popping up with the exact message I expected. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I then put the original script contents (which I had copied out to Notepad) back in, did the F5 and eventlog check again&#8230; and all was well. I got the event I expected and and at almost the same time, I saw the entities in the SCOM console turn from &#8220;Not monitored&#8221; to &#8220;Healthy&#8221;. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Considering the time this took me, I figured I&#8217;d share my experience &#8211; and some of my frustrations \ud83d\ude43<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Keep monitoring!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, simple things can take many hours to solve. While working on a custom MP that contains a Powershell script, I kept getting event 22406 (Powershell script error), stating &#8220;System.Management.Automation.ParseException:&nbsp;Missing closing &#8216;}&#8217; in statement block or type definition.&#8221; Now, the error in itself seems quite obvious &#8211; and the search for the opening curly brace &hellip; <a href=\"https:\/\/monitoringstuff.com\/?p=131\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Debugging a script that has no bugs<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-131","post","type-post","status-publish","format-standard","hentry","category-scom"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/monitoringstuff.com\/index.php?rest_route=\/wp\/v2\/posts\/131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monitoringstuff.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monitoringstuff.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monitoringstuff.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monitoringstuff.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=131"}],"version-history":[{"count":6,"href":"https:\/\/monitoringstuff.com\/index.php?rest_route=\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":140,"href":"https:\/\/monitoringstuff.com\/index.php?rest_route=\/wp\/v2\/posts\/131\/revisions\/140"}],"wp:attachment":[{"href":"https:\/\/monitoringstuff.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monitoringstuff.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monitoringstuff.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}