-
Notifications
You must be signed in to change notification settings - Fork 26
/
ExportAsPowerShellNotebook.ps1
45 lines (40 loc) · 1.31 KB
/
ExportAsPowerShellNotebook.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function Export-AsPowerShellNotebook {
<#
.Synopsis
Takes strings of PowerShell and creates and interactive Jupyter Notebook. Try exporting your PowerShell history to a notebook. Check the examples
.Example
Get-History | % command* | Out-ConsoleGridView | Export-AsPowerShellNotebook -OutputNotebook .\temp\testthis.ipynb
.Example
Get-History 7,14 | % comm* | Export-AsPowerShellNotebook -OutputNotebook d:\temp\testthis.ipynb
#>
param(
$OutputNotebook,
[Parameter(ValueFromPipeline)]
$PowerShellText
)
Begin {
if (!$OutputNotebook) {
throw '$OutputNotebook not specified'
}
$psCode = @()
}
Process {
$psCode += $PowerShellText
}
End {
$count = $psCode.Count
if ($count -gt 0) {
New-PSNotebook -NoteBookName $OutputNotebook {
for ($idx = 0; $idx -lt $count; $idx++) {
$currentText = $psCode[$idx]
if ($currentText.Trim().StartsWith('#')) {
Add-NotebookMarkdown -markdown $currentText
}
else {
Add-NotebookCode -code $currentText
}
}
}
}
}
}