Skip to content

Instantly share code, notes, and snippets.

@jhoneill
Last active November 23, 2020 18:06
Show Gist options
  • Save jhoneill/2d9025582faa32a2eb00ad9b91483262 to your computer and use it in GitHub Desktop.
Save jhoneill/2d9025582faa32a2eb00ad9b91483262 to your computer and use it in GitHub Desktop.
Changes I made to the module
Display the source blob
Display the rendered blob
Raw
{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "source": [ "## Set your own path !!\n", "cd ~\\Documents\\GitHub\\\\PowerShellNotebook\n", "import-module .\\powerShellNotebook.psd1 -force" ], "outputs": [] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "source": [ "Get-Notebook .\\__tests__\\NotebooksForUseWithInvokeOutfile\\NotebookMoreThanOneParameterCell.ipynb |\n", " Out-String" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "\r\nNoteBookName : NotebookMoreThanOneParameterCell.ipynb\r\nKernelName : .net-powershell\r\nCodeBlocks : 6\r\nMarkdownBlocks : 0\r\nNoteBookFullName : C:\\Users\\mcp\\Documents\\GitHub\\PowerShellNotebook\\__tests__\\NotebooksForUseWithInvokeOutfile\\NotebookMoreThanOneParameterCell.ipynb\r\nFormatStyle : PowerShell\r\nHasParameterizedCell : True\r\n\r\n\r\n\r\n" }, "execution_count": 1, "metadata": {} } ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Changes to** `Get-Notebook` \n", "Now outputs presence of Parameterized cells (`HasParameterizedCell`) and the lanaguage to be used in \\`\\`\\` blocks (`FormatStyle`) e.g. above will output as ` ```PowerShell `" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "source": [ "Get-NotebookContent .\\__tests__\\DotNetInteractiveNotebooks\\TestWithInvokePS.ipynb -Includeoutput -NoMarkdown" ], "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Changes to `Get-NotebookContent`**\n", "1. Now has Aliases for `JustCode` and `JustMarkdown` and these two are mutually exclusive\n", "2. Now has `-IncludeOutput` switch. (can see the first Item of the second cell). `IncludeOutput` tries to merge multiple text output rows (as generated by .NET Interactive)into a single block. It's been OK with what I've tested but it may need more work to make it properly robust\n", "3. Now copes with piped fileNames and wildcards. \n", "" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "source": [ "ConvertFrom-NotebookToMarkdown -Path .\\__tests__\\DotNetInteractiveNotebooks\\TestWithInvokePS.ipynb -destination demo.md | out-string\n", "ConvertFrom-NotebookToMarkdown -Path .\\__tests__\\DotNetInteractiveNotebooks\\TestWithInvokePS.ipynb -AsText -Includeoutput | out-string" ], "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Changes to `ConvertFrom-NotebookToMarkdown`**\n", "1. Now Supports `-Path` for the name of the source file\n", "2. Now Supports `-Destination` (if not specified uses $pwd\\\\\\<name\\>.md)\n", "3. Now supports `[switch] -IncludeOutput` to put output in a \\\"preformated\\\" block - see also get-NotebookContent \n", "4. Now supports `AsText` \n", "5. Now applies formatting (as understood by github) to \\`\\`\\` blocks so a code cell which starts with a `#!CSharp` \n", " magic command will be ` ```c# ` : without magic commands the whole file's lanaguage (see `Get-Notebook`) will be used. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "source": [ "dir .\\__tests__\\DotNetInteractiveNotebooks\\*.ipynb | Export-NotebookToPowerShellScript -AsText -IncludeTextCells" ], "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Changes to `Export-NotebookToPowerShellScript`**\n", "1. Header changed (also changed the SQL version) to \n", " * Align text\n", " * Format date as _local long date_ : long has month as words so it is unambiguous Previously date was culture invariant i.e. US-style 10/2/2020 which reads 10th Feb in the rest of the world. \n", "2. Now supports piped input of files \n", "3. Now supports `-AsText` to write to console instead of a file. \n", "4. (**Breaking**) `-IncludeTextCells` is now a `switch`, previously was `boolean`\n", "5. Parameter is now named \"Destination\" with outpath as an alias. If it specifies a directory output will be <<notebook-file-name>>.md\n", "6. Now if two consequtive cells are code, outputs \\\"<# #>\\\" between them, and does not output an empty final cell (this would cause a <# #> at the end of files)\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Changes to `Add-NotebookCode`** \n", "1. Can now be given a complex object not just a text stream to include in output. This needs more testing but is designed to support HTML blocks and similar.\n", "2. Can now opt out of creating the GUID. \n", "3. Will process the `#!about` and `#!Time` magic commands and remove the `#!Pwsh` command.\n", "4. When being run from New-PsNotebook, if -IncludeCodeResults was specified results are sent to verbose as well as to the notebook\n", "5. Gist doesn't seem to like CR-LF so I replace CR-LF in output text with LF. \n", "\n", "**Changes to `New-PsNotebook`**\n", "1. Relocated template for the empty notebook and now provide a second one for .NET Interactive notebooks. \n", "2. Runspace can be provided - remoting FTW! \n", "3. Now takes the absence of a notebookname as impying `-AsText`. Previously it loooked like specifying neither would cause the file to be named just \\\".ipynb\\\" \n", "4. Added a `PassThru` switch so it can go straight to a gist or whatever. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "source": [ "\"This session's version is $($PSVersionTable.PSVersion). Compare with version in remote session\"\n", "$pssession = New-PSSession -ComputerName localhost -EnableNetworkAccess ; $computerName = \"localhost\"\n", "# or better $pssession = New-PSSession -ComputerName $computername\n", "New-PSNotebook {\n", " Add-NotebookMarkdown \"# Run Remotely on $computername\"\n", " switch (Get-NotebookContent .\\__tests__\\DotNetInteractiveNotebooks\\TestWithInvokePS.ipynb) {\n", " { $_.Type -eq 'markdown' } { Add-NotebookMarkdown $_.Source }\n", " { $_.Type -eq 'code' } { Add-NotebookCode $_.source -Verbose }\n", " }} -IncludeCodeResults -DNI \"$computerName.ipynb\" -RunSpace $pssession.Runspace" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "This session's version is 7.0.3. Compare with version in remote session\r\n" }, "execution_count": 1, "metadata": {} }, { "output_type": "execute_result", "data": { "text/plain": "\u001b[93mVERBOSE: \r\nName Value \r\n---- ----- \r\nPSVersion 5.1.19041.610 \r\nPSEdition Desktop \r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} \r\nBuildVersion 10.0.19041.610 \r\nCLRVersion 4.0.30319.42000 \r\nWSManStackVersion 3.0 \r\nPSRemotingProtocolVersion 2.3 \r\nSerializationVersion 1.1.0.1 \r\n\r\n\r\n\u001b[0m\r\n" }, "execution_count": 1, "metadata": {} }, { "output_type": "execute_result", "data": { "text/plain": "\u001b[93mVERBOSE: 2\r\n4\r\n6\r\n8\r\n10\r\n12\r\n14\r\n16\r\n18\r\n20\r\n\u001b[0m\r\n" }, "execution_count": 1, "metadata": {} } ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "source": [ "psnotebook {\n", " switch (NotebookContent .\\input.ipynb){\n", " {$_.type -match'code'} {CodeCell -Verbose $_.source } ;\n", " default {MDCell $_.source} \n", " }\n", "} -DNI -IncludeCodeResults output.ipynb \n", "" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "\u001b[93mVERBOSE: alpha = 1.2, ratio = 3.7, and alpha * ratio = 4.44\r\n\u001b[0m\r\n" }, "execution_count": 1, "metadata": {} }, { "output_type": "execute_result", "data": { "text/plain": "\u001b[93mVERBOSE: a = 5 and twice = 10\r\n\u001b[0m\r\n" }, "execution_count": 1, "metadata": {} } ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**New Aliases**\n", "PsNotebook = New-PsNotebook\n", "CodeCell = Add-NotebookCode \n", "mdCell = NotebookMarkdown \n", "\n", "so the above reads input.ipynb, for each cell if it's a code cell insert it's source into a code cell and if not insert into a markdown cell.\n", "-Verbose on code cells writes the output, and we've asked for output, in Dot-Interactive file named output \n", " \n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**New-Command `Set-NotebookToPS`**\n", "\n", "Re-writes the metadata in the IPYNB file. \n", "DotNet Interactive likes to create the file specifying the language as C# and then use a magic command to say \"but this cell is PowerShell\" \n", "So this changes the metadata to say the language is PowerShell and remove the `#!pwsh` magic commands. It displays much more nicely in anything else that way.\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Changes to `New-GistNotebook`** \n", "1. Uses another script (which is *not* imported into the module) to get the GitHub private access token from the Windows Cred Store. (It is stored with the name Git:https://github.com)\n", "2. Public / private Gist is now a `-Public` switch (private by default) \n", "3. Accepts input by piping a file to it, see below - content comes from the file, file *name* can be over-ridden but will defualt to the file name. \n", "4. Had MASSIVE problems due to accented characters in some of my test data, so I added `-EscapeHandling EscapeNonAscii` to `ConvertTo-Json` and all is good. \n", "" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "source": [ "Set-NotebookToPS -Path .\\changelog.ipynb -OutputPath .\\changelog-j.ipynb -PassThru | \n", " New-GistNotebook -Public -FileName \"PowerShellNotebookModule.ipynb\" -GistDescription \"Changes I made to the module\"" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "https://gist.github.com/2d9025582faa32a2eb00ad9b91483262\r\n" }, "execution_count": 1, "metadata": {} } ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "source": [ "#Here's how the password is fetch I'll just give the length, no my actual key !\n", "(.\\Get-CredentialFromWindowsCredentialManager.ps1 -TargetName git:https://github.com `\n", " -PlainTextPasswordOnly).length" ], "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "40\r\n" }, "execution_count": 1, "metadata": {} } ] } ], "metadata": { "kernelspec": { "display_name": ".NET (PowerShell)", "language": "PowerShell", "name": ".net-powershell" }, "language_info": { "file_extension": ".ps1", "mimetype": "text/x-powershell", "name": "PowerShell", "pygments_lexer": "powerShell", "version": "7.0" } }, "nbformat": 4, "nbformat_minor": 4 }
@jhoneill
Copy link
Author

jhoneill commented Nov 21, 2020

Note this has been updated since the first upload , otherwise the URL in the last cell but one (and the last cell) wouldn't appear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment