Blog Post

Strange PowerShell Notebook Behavior When Comments Are Involved

This is not the blog post I was intending to write. I started off writing about a different topic, and had some code that I wanted to share in a PowerShell Notebook. Getting that notebook to behave like it should have, however, is another story entirely and worthy of its own post – this one.

PowerShell Logo

PowerShell Notebooks are a great new feature in Azure Data Studio, first becoming available in the November 2019 release. Like SQL notebooks, PowerShell notebooks are based on Jupyter Notebooks format, which are interactive documents containing text and executable code blocks.

Having some working PowerShell code that I wanted to share along with explanations and examples, I created a PowerShell Notebook. The only problem was my functions would never initialize. Actually they would never stop initializing – I would run the cell they were defined in, and it would just keep running forever.

After several days of not being able to figure this out, I reached out to a true master, gentleman, scholar, and friend: Rob Sewell (@sqldbawithbeard). Rob took a look at this, and in under 10 minutes of experimentation, discovered that my personal habit of placing comments at the end of functions was causing the problem. Whenever I have a closing bracket, be it at the end of a function, class, file, or even a for loop, I like to make a comment to note what scope is ending. This is completely valid PowerShell, and in a .ps1 file it works just fine.

Function Do-Something {
   5
}# comment at end of Do-Something
Function Do-SomethingElse {
   8
}# end of Do-SomethingElse

PowerShell Notebooks, however, don’t like this for some reason. And to experiment further, I have none other than a PowerShell Notebook to demonstrate several different scenarios:

Think this is strange?

I do. And Rob does. So I’ve filed an issue with the Azure Data Studio team.

EDIT: It turns out this issue is fixed in a later version of the PowerShell kernel. See how to update it here.

Related Posts

By Bob Pusateri

Bob Pusateri is a Microsoft Data Platform MVP, Microsoft Certified Master of SQL Server, speaker, blogger, volunteer, and author who loves finding new and exciting ways to break and fix things. He works as a consultant and architect, specializing in data, virtualization, and cloud technologies.

Leave a Reply

Your email address will not be published. Required fields are marked *