Created
January 31, 2020 21:13
-
-
Save BobPusateri/5b99ff352e9e2166204be54ce671b954 to your computer and use it in GitHub Desktop.
Demonstrating behaviors of PowerShell notebooks that contain both functions and comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"kernelspec": { | |
"name": "powershell", | |
"display_name": "PowerShell" | |
}, | |
"language_info": { | |
"name": "powershell", | |
"codemirror_mode": "shell", | |
"mimetype": "text/x-sh", | |
"file_extension": ".ps1" | |
} | |
}, | |
"nbformat_minor": 2, | |
"nbformat": 4, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Define a function in a cell with a comment at the end of it.\n", | |
"\n", | |
"Even though this is valid PowerShell, **it will not work**. If you run the cell it will just spin forever." | |
], | |
"metadata": { | |
"azdata_cell_guid": "04728b8b-6fbc-4157-b5e2-4ce82bcff6b4" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Function Do-Something {\n", | |
" 5\n", | |
"}# comment at end of Do-Something\n", | |
"Function Do-SomethingElse {\n", | |
" 8\n", | |
"}# Do-SomethingElse" | |
], | |
"metadata": { | |
"azdata_cell_guid": "d17e814f-a1b5-44da-832b-9000cba88a86", | |
"tags": [] | |
}, | |
"outputs": [], | |
"execution_count": null | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Remove the comments at the end of the closing brackets and it works just fine.\r\n", | |
"\r\n", | |
"*Note: you may have to reload the window by hitting F1 and selecting \"Developer: Reload Window\" before this step.*" | |
], | |
"metadata": { | |
"azdata_cell_guid": "d707d356-d305-4423-a020-ff356f60f5ac" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Function Do-Something {\r\n", | |
" 5\r\n", | |
"}\r\n", | |
"Function Do-SomethingElse {\r\n", | |
" 8\r\n", | |
"}" | |
], | |
"metadata": { | |
"azdata_cell_guid": "436abc10-21dc-4f37-bb51-17e1fb1bbec3" | |
}, | |
"outputs": [], | |
"execution_count": null | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"And now you can call the functions and get the values they return." | |
], | |
"metadata": { | |
"azdata_cell_guid": "75d64c3e-628c-4650-b0de-4ef12ecc073b" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Do-Something\r\n", | |
"Do-SomethingElse" | |
], | |
"metadata": { | |
"azdata_cell_guid": "4e582cf6-b6e1-414c-bdf7-06dc568afbe4" | |
}, | |
"outputs": [], | |
"execution_count": null | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"What if there were a space between the closing bracket and the comment? This won't complete either." | |
], | |
"metadata": { | |
"azdata_cell_guid": "5cce53ca-28f8-46bf-a03d-af1a1a998822" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Function Do-Something {\r\n", | |
" 5\r\n", | |
"} # comment\r\n", | |
"Function Do-SomethingElse {\r\n", | |
" 8\r\n", | |
"} #comment" | |
], | |
"metadata": { | |
"azdata_cell_guid": "c2fbc866-41a8-4094-bf70-3dabeec6cd4c" | |
}, | |
"outputs": [], | |
"execution_count": null | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Put the comment on it's own line. This also doesn't work." | |
], | |
"metadata": { | |
"azdata_cell_guid": "7f7fcf5f-d8a4-438b-a3ee-b10d774f9e6c" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Function Do-Something {\r\n", | |
" 5\r\n", | |
"}\r\n", | |
"# comment\r\n", | |
"Function Do-SomethingElse {\r\n", | |
" 8\r\n", | |
"}\r\n", | |
"# comment 2" | |
], | |
"metadata": { | |
"azdata_cell_guid": "3a95af36-f33e-46c7-83c7-312c01f41357", | |
"tags": [] | |
}, | |
"outputs": [], | |
"execution_count": null | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"But if we use multi-line comments, and enclose with <# #>, it runs as expected.\r\n", | |
"\r\n", | |
"*Again, you may need to reload the window before running this.*" | |
], | |
"metadata": { | |
"azdata_cell_guid": "4eecfd64-c817-405b-945f-ecee528b2a0b" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Function Do-Something {\r\n", | |
" 5\r\n", | |
"} <# comment #>\r\n", | |
"Function Do-SomethingElse {\r\n", | |
" 8\r\n", | |
"} <# comment 2 #>" | |
], | |
"metadata": { | |
"azdata_cell_guid": "162ea450-7971-4d16-b387-780b12512fde" | |
}, | |
"outputs": [], | |
"execution_count": null | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"Do-Something\r\n", | |
"Do-SomethingElse" | |
], | |
"metadata": { | |
"azdata_cell_guid": "823ae349-8aaf-4c18-acea-e75254152b53" | |
}, | |
"outputs": [], | |
"execution_count": null | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment