-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathInvokePowerShellNotebook.ps1
112 lines (89 loc) · 3.76 KB
/
InvokePowerShellNotebook.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
function Invoke-PowerShellNotebook {
<#
.SYNOPSIS
Invoke-PowerShellNotebook executes all the PowerShell code blocks in a PowerShell Notebook.
.Example
Invoke-PowerShellNotebook .\SingleCodeBlock.ipynb
Region Item TotalSold
------ ---- ---------
South lime 20
West melon 76
#>
param(
[Parameter(ValueFromPipelineByPropertyName)]
$NoteBookFullName,
$OutFile,
[Switch]$AsExcel,
[Switch]$Show
)
Process {
if ($OutFile) {
if (Test-Path $OutFile) {
throw "$OutFile already exists"
}
$PSNotebookRunspace = New-PSNotebookRunspace
$null = Copy-Item $NoteBookFullName $OutFile
$notebookJson = Get-Content $OutFile | ConvertFrom-Json
$codeCells = $notebookJson.cells | Where-Object { $_.'cell_type' -eq 'code' }
foreach ($codeCell in $codeCells) {
# Clear Errors
$PSNotebookRunspace.PowerShell.Streams.Error.Clear()
# Clear Commands
$PSNotebookRunspace.PowerShell.Commands.Clear()
$result = $PSNotebookRunspace.Invoke($codeCell.source)
if ($PSNotebookRunspace.PowerShell.Streams.Error.Count -gt 0) {
$text = $PSNotebookRunspace.PowerShell.Streams.Error | Out-String
}
else {
$text = $result
}
$codeCell.outputs = @()
$codeCell.outputs += [ordered]@{
name = "stdout"
text = $text -join "`n"
output_type = "stream"
}
}
$PSNotebookRunspace.Close()
$notebookJson | ConvertTo-Json -Depth 5 | Set-Content $OutFile -Encoding UTF8
}
else {
$codeBlocks = @(Get-NotebookContent $NoteBookFullName -JustCode)
$codeBlockCount = $codeBlocks.Count
$SheetCount = 0
for ($idx = 0; $idx -lt $codeBlockCount; $idx++) {
$targetCode = $codeblocks[$idx].source
Write-Progress -Activity "Executing PowerShell code block - [$(Get-Date)]" -Status (-join $targetCode) -PercentComplete (($idx + 1) / $codeBlockCount * 100)
if ($AsExcel) {
if (!(Get-Module -ListAvailable ImportExcel -ErrorAction SilentlyContinue)) {
throw "This feature requires the ImportExcel PowerShell module. Use 'Install-Module -Name ImportExcel' to get it from the PS Gallery."
}
if ($idx -eq 0) {
$notebookFileName = Split-Path $NoteBookFullName -Leaf
$xlFileName = $notebookFileName -replace ".ipynb", ".xlsx"
$xlfile = "{0}\{1}" -f $pwd.Path, $xlFileName
Remove-Item $xlfile -ErrorAction SilentlyContinue
}
foreach ($dataSet in , @($targetCode | Invoke-Expression)) {
if ($dataSet) {
$SheetCount++
$uniqueName = "Sheet$($SheetCount)"
Export-Excel -InputObject $dataSet -Path $xlfile -WorksheetName $uniqueName -AutoSize -TableName $uniqueName
}
}
}
else {
, @($targetCode | Invoke-Expression)
}
}
if ($AsExcel) {
if ($Show) {
Invoke-Item $xlfile
}
else {
$xlfile
}
}
}
}
}