Skip to content
 
 

Latest commit

 

History

96 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PowerShell Notebook

This module includes the function Invoke-PowerShellNotebook which enables you to run the cells inside the PowerShell notebook.

Continuous Integration Status

Build Status

Check out the Video Here

In a nutshell.

  1. Author your markdown with Chapter Start and End, then use fence blocks ``` to indic
  2. In Azure Data Studio PowerShell console, run Convert-MarkdownToNoteBook .\demo.md -watch
    • When you save the file, Convert-MarkdownToNoteBook detects and auto converts it to a PowerShell Notebook
  3. The converted Interactive PowerShell Notebook. Note: Convert-MarkdownToNoteBook also runs the code from the markdown file and includes the results.

Convert a Markdown File to a PowerShell Notebook

Convert-MarkdownToNoteBook converts your markdown, recognizes fenced code blocks and converts it, and "runs" the PowerShell, including that snippets results in an executable document, a PowerShell notebook.

  • Share it with colleagues to better illustrate concepts
  • Develop workflows other can use, for diagnostics, testing, and more
Convert-MarkdownToNoteBook .\multiplePSLines.md

Exclude Results

Sometimes you don't the results to be included in the converted PowerShell Notebook.

You can suppress evaluating the PowerShell code being run and the results being included by have the following comment as the first line in the fence block.

# Exclude Results

    ```ps
    # Exclude Results
    1+1
    ```

This will include the 1+1 in the interactive notebook, but will not evaluate the PowerShell statement and include it.

Available Functions

Function Synopsis
Add-NotebookCode Add-NotebookCode adds PowerShell code to a code block
Add-NotebookMarkdown Add-NotebookMarkdown adds Markdown to a markdown block
Add-NotebookMarkdown Add-NotebookMarkdown is intended to be used in a New-PSNotebook scriptblock
ConvertFrom-NotebookToMarkdown Take and exiting PowerShell Notebook and convert it to markdown
Convert-MarkdownToNoteBook Convert a markdown file to an interactive PowerShell Notebook
Export-NotebookToPowerShellScript Exports all code blocks from a PowerShell Notebook to a PowerShell script
Get-Notebook Get-Notebook reads the metadata of a single (or folder of) Jupyter Notebooks
Get-NotebookContent Get-NotebookContents reads the contents of a Jupyter Notebooks
Invoke-PowerShellNotebook Invoke-PowerShellNotebook executes all the PowerShell code blocks in a PowerShell Notebook.
New-PSNotebook Creates a new PowerShell Notebook that can be returned as text or saves as a ipynb file.
New-PSNotebookRunspace New-PSNotebookRunspace instantiates the PSNotebookRunspace

A PowerShell Notebook with Cells

Below is a PowerShell Notebook with three cells, each containing a PowerShell "script".

Notice the second cell has the results of running get-process | select company, name, handles -first 10

Automate the PowerShell Notebook

Bonus Points: Using the -AsExcel switch

Invoke-PowerShellNotebook sports an AsExcel switch. This lets you execute each cell in the PowerShell notebook and the function exports the results to a separate sheet in an Excel file.

You need to have the PowerShell ImportExcel module installed. The module is on the PowerShell Gallery, use Install-Module ImportExcel to install it on you machine.

A Little Language to Create PowerShell Notebooks

PowerShell Little Language

Create and Save a PowerShell Notebook

You can also create PowerShell Notebooks outside if Azure Data Studio with this module. Here is an example. It creates two code blocks and a markdown block, and saves it to a file C:\Temp\test.ipnyb.

New-PSNotebook -NoteBookName c:\temp\test.ipynb {
            Add-NotebookCode '$a=8'
            Add-NotebookCode '$a+12'
            Add-NotebookCode '$a+3'
            Add-NotebookMarkdown @'
## Math

- show addition
- show other
'@
}

Open the PowerShell Notebook in Azure Data Studio

You can open c:\temp\test.ipynb in Azure Data Studio and click Run Cells

image

Convert a demo.txt file to a PowerShell Notebook

If you've used start-demo.ps1 to setup PowerShell demos, this function will convert that format into a PowerShell Notebook.

ConvertTo-PowerShellNoteBook .\demo.txt .\demo.ipynb

Converts this to a PowerShell Notebook.

# Get first 10 process
ps | select -first 10

# Get first 10 services
gsv | select -first 10

# Create a function
function SayHello($p) {"Hello $p"}

# Use the function
SayHello World

Here it is in Azure Data Studio

About

Let's you automate PowerShell Notebooks with PowerShell at the command line, exports to Excel and more. On the gallery

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages