1 minute read

The latest update to the ADSNotebook PowerShell module I blogged about here now enables the creation of PowerShell notebooks with PowerShell.

You can install the module with

Install-Module ADSNotebook

or if you have already installed it you can use

Update-Module ADSNotebook

In the latest release, there is an extra parameter for New-AdsWorkBook of -Type which will accept either SQL or PowerShell

Create a PowerShell Notebook with PowerShell Rob

OK!

Here is some code to create a PowerShell Notebook. First we will create some cells using New-AdsWorkBookCell including all the markdown to add images and links. You can find my notebooks which explain how to write the markdown for your notebooks in my GitHub Presentations Repository

<PRE class=wp-block-code>$introCelltext = "# Welcome to my Auto Generated PowerShell Notebook

dbatools

image

dbatools is an open-source PowerShell Module for administering SQL Servers. You can read more about dbatools and find the documentation at dbatools.io “ $SecondCelltext = “### Installation You can install dbatools from the PowerShell Gallery using Install-Module dbatools

$thirdcelltext = “Install-Module dbatools”

$fourthCelltext = “### Getting Help You should always use Get-Help to fins out how to use dbatools (and any PowerShell) commands”

$fifthcelltext = “Get-Help Get-DbaDatabase” $sixthCelltext = “Try a command now. get the name, owner and collation of the user databases on the local instance” $seventhCellText = “Get-DbaDatabase -SqlInstance localhost -ExcludeSystem | Select Name, Owner, Collation”

$Intro = New-ADSWorkBookCell -Type Text -Text $introCelltext $second = New-ADSWorkBookCell -Type Text -Text $SecondCelltext $third = New-ADSWorkBookCell -Type Code -Text $thirdcelltext $fourth = New-ADSWorkBookCell -Type Text -Text $fourthCelltext $fifth = New-ADSWorkBookCell -Type Code -Text $fifthcelltext $sixth = New-ADSWorkBookCell -Type Text -Text $sixthCelltext $seventh = New-ADSWorkBookCell -Type Code -Text $seventhCellText</CODE></PRE> Then we will create a new workbook using those cells

<PRE class=wp-block-code>$path = 'C:\temp\dbatools.ipynb' New-ADSWorkBook -Path $path -cells $Intro,$second,$third,$fourth,$fifth,$sixth,$Seventh -Type PowerShell</PRE> Then, when that code is run we can open the Notebook and ta-da

And it is super quick to run as well

UPDATE – Tyler Leonhardt t from the PowerShell team asked

Challenge accepted, with extra meta, here is the PowerShell to create a PowerShell Notebook which will create a PowerShell Notebook!!

<DIV class=wp-block-embed__wrapper><IFRAME title=2019-11-14_15-50-46-mp4 height=474 src="https://videopress.com/embed/5zyfyR2P?preloadContent=metadata&hd=0" frameBorder=0 width=630 allowfullscreen></IFRAME> </DIV>

Comments