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
"System.Management.Automation.ParseException: Missing closing '}' in statement block or type definition."
Now, the error in itself seems quite obvious – and the search for the opening curly brace began. It’s not exactly my first rodeo when it comes to debugging semi-colons, single and double quotes, brackets and braces – syntax highlighting saved my bacon plenty of times. And although VSAE ( my MP building tool of choice) isn’t very good at Powershell syntax highlighting, it’s easy to open the script in Powershell ISE.
But even there, I found nothing wrong – the curly braces all neatly matched up. Then I ran the script on the targeted server, and… it worked fine. 🤨
To make sure I didn’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… no errors. 🙂
Checked the event log… and *poof*. The 22406es were back. 😠
I then replaced the script contents with only a few lines (those of you that use Kevins MP fragments will recognize them):
$ScriptName = "MyMPScript.ps1" $EventID = "1234" #================================================================================= # Starting Script section - All scripts get this #================================================================================= # Gather the start time of the script $StartTime = Get-Date #Set variable to be used in logging events $whoami = whoami # Load MOMScript API $momapi = New-Object -comObject MOM.ScriptAPI #Log script event that we are starting task $momapi.LogScriptEvent($ScriptName,$EventID,0,"`n Script is starting. `n Running as ($whoami). `n Management Group: ($MGName).") #=================================================================================
Now, I dare you to find any curly brace in there… yep. There aren’t any. Hit F5 to build and deploy to my test SCOM environment… no errors. 🙂
Checked the event log… and *poof*. The 22406es were back…
"System.Management.Automation.ParseException: Missing closing '}' in statement block or type definition." 😶
So I opened the file again… 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… 3 bytes.
That’s when it dawned on me. I opened the file in Notepad++ and yep… for some reason, the file had become encoded as UTF-8-BOM.
Switched the file to UTF-8, repasted the content… saved… hit F5 to build and deploy to my test SCOM environment… no errors. So far, so good. Checked the event log… and saw event 1234 popping up with the exact message I expected.
I then put the original script contents (which I had copied out to Notepad) back in, did the F5 and eventlog check again… 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 “Not monitored” to “Healthy”.
Considering the time this took me, I figured I’d share my experience – and some of my frustrations 🙃