functions/New-ADSWorkBookCell.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
<#
.SYNOPSIS
Creates a cell for an Azure Data Studio Notebook
 
.DESCRIPTION
Creates a text (markdown) or code (T-SQL) cell for an Azure Data Studio Notebook
 
.PARAMETER Type
The type of cell to create (code or text)
 
.PARAMETER Text
The value for the cell (markdown for text and T-SQL for celll)
 
.EXAMPLE
$introCelltext = "# Welcome to my Auto Generated Notebook
 
## Automation
Using this we can automate the creation of notebooks for our use
"
$Intro = New-ADSWorkBookCell -Type Text -Text $introCelltext
 
Creates an Azure Data Studio Text cell and sets it to a variable for passing to New-AdsWorkBook
 
.NOTES
Rob Sewell 10/10/2019 - Initial
#>


function New-ADSWorkBookCell {
    [cmdletbinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "", Justification = "Because it doesnt really change anything")]
    Param (
        # The Type of cell
        [Parameter(Mandatory)]
        [ValidateSet('Code', 'Text')]
        [string]
        $Type,
        # The source for the cell
        [Parameter(Mandatory)]
        [string]
        $Text
    )
    $PSCmdlet.WriteVerbose('Lets create a Notebook Cell')
    switch ($type) {
        Text {
            $PSCmdlet.WriteVerbose('We are going to create a Text Cell')
            $PSCmdlet.WriteVerbose('Creating base object')
            $guid = [guid]::NewGuid().guid
            $basecell = [pscustomobject]@{
                cell_type = 'markdown'
                source    = @(
                )
                metadata  = [pscustomobject]@{
                    azdata_cell_guid = "$guid"
                }
            }
        }
        Code {
            $PSCmdlet.WriteVerbose('We are going to create a Code Cell')
            $PSCmdlet.WriteVerbose('Creating base object')
            $guid = [guid]::NewGuid().guid
            $basecell = [pscustomobject]@{
                cell_type       = 'code'
                source          = @(
                )
                metadata        = [pscustomobject]@{
                    azdata_cell_guid = "$guid"
                }
                outputs         = @()
                execution_count = 0
            }
        }
    }
    $PSCmdlet.WriteVerbose('Now we need to parse the text, first split it by line ending')
    $rawtext = $text -split "[`r`n]+"

    $PSCmdlet.WriteVerbose('Recreate the code as an array of strings with the correct line ending')
    $source = @()
    foreach ($line in $rawtext) {
       $source += '"' + $Line + '\r\n"'
    }
    $PSCmdlet.WriteVerbose('Source now looks like this - Each line should be a double quote and end with \r\n' + $source)
    $PSCmdlet.WriteVerbose('Add source to the base cell')
    $basecell.source = $source
    $basecell
    $PSCmdlet.WriteVerbose('Finished creating cell')
}