Skip to content

Instantly share code, notes, and snippets.

@SQLvariant
Last active October 15, 2020 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SQLvariant/5cbad3dd52093fbd053ec46769a0fc22 to your computer and use it in GitHub Desktop.
Save SQLvariant/5cbad3dd52093fbd053ec46769a0fc22 to your computer and use it in GitHub Desktop.
Example PowerShell Notebook to show you examples of what will happen when you use the ConvertTo-SQLNoteBook function.
irm https://gist.githubusercontent.com/SQLvariant/5cbad3dd52093fbd053ec46769a0fc22/raw/1ecf6c292a6c0b9419af01d7360d373a2adff3cd/Using_ConvertTo-SQLNoteBook.ipynb -OutFile Using_ConvertTo-SQLNoteBook.ipynb
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"kernelspec": {
"name": ".net-powershell",
"display_name": ".NET (PowerShell)"
},
"language_info": {
"name": "PowerShell",
"version": "7.0",
"mimetype": "text/x-powershell",
"file_extension": ".ps1",
"pygments_lexer": "powershell"
}
},
"nbformat_minor": 2,
"nbformat": 4,
"cells": [
{
"cell_type": "markdown",
"source": [
"<img src=\"https://raw.githubusercontent.com/PowerShell/PowerShell/master/assets/avatar_128.svg\" width=\"8%\"> ➕\r\n",
"<img src=\"https://user-images.githubusercontent.com/67258/89468449-77bfdf80-d745-11ea-894b-0cbaa4f06809.png?raw=true\" width=\"12.5%\"> ➕\r\n",
"<img src=\"https://github.com/Microsoft/azuredatastudio/blob/master/samples/notebookSamples/Graphics/AzureDataStudioLogo.png?raw=true\" width=\"7.5%\"> = ❤\r\n",
"# PowerShell to convert .SQL files into Notebooks"
],
"metadata": {
"azdata_cell_guid": "64208091-949a-4fb0-8055-60b1720b6977"
}
},
{
"cell_type": "markdown",
"source": [
"## First up, install the `PowerShellNotebook` module from the PowerShell Gallery..."
],
"metadata": {
"azdata_cell_guid": "dfb672e2-24f6-4ddb-8149-d28f8675e2ac"
}
},
{
"cell_type": "code",
"source": [
"Install-Module PowerShellNotebook"
],
"metadata": {
"azdata_cell_guid": "54c77f28-f528-4fd1-be75-075b911cf44d"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"(optional step, for demo purposes)\r\n",
"## Make a directory to store some .SQL files"
],
"metadata": {
"azdata_cell_guid": "cbbb4e1e-83fd-460e-8936-70871e488ed3"
}
},
{
"cell_type": "code",
"source": [
"mkdir c:\\temp\\SQLFiles"
],
"metadata": {
"azdata_cell_guid": "9e3c8aa9-cdfe-4f32-8ecf-80c1ae714b0b"
},
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"Switch to a folder where you have a lot of `.SQL` files."
],
"metadata": {
"azdata_cell_guid": "a185ac01-c80c-4478-bd09-4a9e31f8b7ba"
}
},
{
"cell_type": "code",
"source": [
"cd c:\\temp\\SQLFiles"
],
"metadata": {
"azdata_cell_guid": "bdcb4c54-4231-4e82-a602-25ae17b0c186",
"tags": []
},
"outputs": [],
"execution_count": 1
},
{
"cell_type": "markdown",
"source": [
"### If you don't have any .SQL files handy, download some from GitHub\r\n",
"(use the step below.)"
],
"metadata": {
"azdata_cell_guid": "78ed849d-d307-4bbf-b6b8-b769d40f4e49"
}
},
{
"cell_type": "code",
"source": [
"\r\n",
"irm https://gist.githubusercontent.com/MsSQLGirl/799d3613c6b3aba58cb4decbb30da139/raw/433ffdcefcbc4db0e5f5c9b53e1e9bde139f885d/SQLSample_01_ServerProperties.sql > '.\\SQLSample_01_ServerProperties.sql'\r\n",
"irm https://gist.githubusercontent.com/MsSQLGirl/799d3613c6b3aba58cb4decbb30da139/raw/433ffdcefcbc4db0e5f5c9b53e1e9bde139f885d/SQLSample_02_WWI.sql > '.\\SQLSample_02_WWI.sql'\r\n",
"irm https://gist.githubusercontent.com/MsSQLGirl/799d3613c6b3aba58cb4decbb30da139/raw/433ffdcefcbc4db0e5f5c9b53e1e9bde139f885d/SQLSample_03_StringDynamics.sql > '.\\SQLSample_03_StringDynamics.sql'\r\n",
"irm https://gist.githubusercontent.com/MsSQLGirl/799d3613c6b3aba58cb4decbb30da139/raw/433ffdcefcbc4db0e5f5c9b53e1e9bde139f885d/SQLSample_04_VariableBatchConundrum.sql > '.\\SQLSample_04_VariableBatchConundrum.sql'\r\n",
"irm https://gist.githubusercontent.com/vickyharp/d188b5ab2ceec12896b4a514ea52e5b6/raw/f2e4b1bc4d6a2fb293aebb9989129bd722d6a25e/AdventureWorksAddress.sql > '.\\AdventureWorksAddress.sql'\r\n",
"irm https://gist.githubusercontent.com/vickyharp/6c254d63d3de9850b20b5861b061b5f5/raw/0ff7d7c5da9f216fb7534994c8be60fe0e7efaf3/AdventureWorksMultiStatementSBatch.sql > '.\\AdventureWorksMultiStatementSBatch.sql'\r\n",
"irm https://raw.githubusercontent.com/microsoft/tigertoolbox/master/BPCheck/Check_BP_Servers.sql > '.\\Check_BP_Servers.sql'\r\n",
""
],
"metadata": {
"azdata_cell_guid": "2d8cb1df-8723-433e-b261-37fd5050c44c"
},
"outputs": [],
"execution_count": 2
},
{
"cell_type": "markdown",
"source": [
"## Here's the part where it gets good!\r\n",
"\r\n",
"Now use `dir` to loop over all the .SQL files in the directory, and use the `ConvertTo-SQLNoteBook` function to turn them into SQL Notebooks."
],
"metadata": {
"azdata_cell_guid": "a4734ec1-443f-47c7-bf0a-5bf2d3d2a42e"
}
},
{
"cell_type": "code",
"source": [
"dir -Filter *.SQL | \r\n",
"foreach {\r\n",
" ConvertTo-SQLNoteBook -InputFileName $_.FullName -OutputNotebookName (Join-Path -Path (Split-Path -Path $_.FullName -Parent) -ChildPath ($_.Name -replace '.sql', '.ipynb'))\r\n",
"}"
],
"metadata": {
"azdata_cell_guid": "40f78b02-3934-41ed-9844-377ddf2d4d23"
},
"outputs": [],
"execution_count": 3
},
{
"cell_type": "markdown",
"source": [
"Check inside that same directory, and you should now see a bunch of .IPYNB files."
],
"metadata": {
"azdata_cell_guid": "75c77c74-176b-47c6-9b63-ca3103d4ffee"
}
},
{
"cell_type": "code",
"source": [
"dir -Filter *.ipynb"
],
"metadata": {
"azdata_cell_guid": "830d3125-d6e7-40e9-8c68-adb97f4de872",
"tags": []
},
"outputs": [
{
"name": "stdout",
"text": "\r\n",
"output_type": "stream"
},
{
"name": "stdout",
"text": "\r\n",
"output_type": "stream"
},
{
"name": "stdout",
"text": " Directory: C:\\temp\\SQLFiles\r\n",
"output_type": "stream"
},
{
"name": "stdout",
"text": "\r\n",
"output_type": "stream"
},
{
"name": "stdout",
"text": "Mode LastWriteTime Length Name\r\n",
"output_type": "stream"
},
{
"name": "stdout",
"text": "---- ------------- ------ ----\r\n",
"output_type": "stream"
},
{
"name": "stdout",
"text": "-a--- 8/17/2020 1:42 PM 1754 AdventureWorksAddress.ipynb\r\n",
"output_type": "stream"
},
{
"name": "stdout",
"text": "-a--- 8/17/2020 1:42 PM 3656 AdventureWorksMultiStatementSBatch.ipynb\r\n",
"output_type": "stream"
},
{
"name": "stdout",
"text": "-a--- 8/17/2020 1:42 PM 928883 Check_BP_Servers.ipynb\r\n",
"output_type": "stream"
},
{
"name": "stdout",
"text": "-a--- 8/17/2020 1:42 PM 2252 SQLSample_01_ServerProperties.ipynb\r\n",
"output_type": "stream"
},
{
"name": "stdout",
"text": "-a--- 8/17/2020 1:42 PM 5274 SQLSample_02_WWI.ipynb\r\n",
"output_type": "stream"
},
{
"name": "stdout",
"text": "-a--- 8/17/2020 1:42 PM 3100 SQLSample_03_StringDynamics.ipynb\r\n",
"output_type": "stream"
},
{
"name": "stdout",
"text": "-a--- 8/17/2020 1:42 PM 1478 SQLSample_04_VariableBatchConundrum.ipynb\r\n",
"output_type": "stream"
},
{
"name": "stdout",
"text": "\r\n",
"output_type": "stream"
}
],
"execution_count": 4
},
{
"cell_type": "markdown",
"source": [
"Now let's have a look at one of the Notebooks we just built. Grab one and open it up in Azure Data Studio."
],
"metadata": {
"azdata_cell_guid": "a797d2ab-3540-4e4f-a571-aad3f2dad2f3"
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment