Skip to content

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

License

Notifications You must be signed in to change notification settings

ctmcisco/PowerShellNotebook

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PowerShell Notebook

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

TDD Status

Build Status

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

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.

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.

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

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PowerShell 71.6%
  • Jupyter Notebook 28.4%