forked from dfinke/PowerShellNotebook
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExportNotebookToPowerShellScript.ps1
More file actions
53 lines (42 loc) · 1.42 KB
/
Copy pathExportNotebookToPowerShellScript.ps1
File metadata and controls
53 lines (42 loc) · 1.42 KB
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
function Export-NotebookToPowerShellScript {
<#
.SYNOPSIS
Exports all code blocks from a PowerShell Notebook to a PowerShell script
.DESCRIPTION
Exports from either a local notebook or one on the internet
.Example
Export-NotebookToPowerShellScript .\TestPS.ipynb
Get-Content .\TestPS.ps1
.Example
Export-NotebookToPowerShellScript "https://raw.githubusercontent.com/dfinke/PowerShellNotebook/AddJupyterNotebookMetaInfo/samplenotebook/powershell.ipynb"
Get-Content .\powershell.ps1
#>
[CmdletBinding()]
param(
$FullName,
$outPath = "./"
)
Write-Progress -Activity "Exporting PowerShell Notebook" -Status $FullName
if ([System.Uri]::IsWellFormedUriString($FullName, [System.UriKind]::Absolute)) {
$outFile = $FullName.split('/')[-1]
}
else {
$outFile = (Split-Path -Leaf $FullName)
}
$outFile = $outFile -replace ".ipynb", ".ps1"
$fullOutFileName = $outPath + $outFile
$heading = @"
<#
Created from: $($FullName)
Created by: Export-NotebookToPowerShellScript
Created on: $(Get-Date)
#>
"@
$heading | Set-Content $fullOutFileName
$result = foreach ($sourceBlock in (Get-NotebookContent $FullName -JustCode).Source) {
$sourceBlock
""
}
$result | Add-Content $fullOutFileName
Write-Verbose "$($outFile) created"
}