pythonpowershelljupyter-notebookmarkdownazure-data-studio

Import table from same Notebook


How could one read a markdown table from a text block and import in into a variable in a code block in the same PowerShell notebook?

Attempted to import with PowerShell

$b=Import-Csv -Path <> -Delimiter '|'

enter image description here

Couldn't figure out how to point the -Path parameter to the text block in the same notebook. Using a .ipynb file in Azure Data Studio.


Solution

  • I ended up creating a custom function to import a Markdown table to consume in an Azure PowerShell notebook.

    function ConvertFrom-MdTable {param([string]$MdTable)
        $lines = $MdTable -split "`r?`n"
        $headers = ($lines[0] -split '\|') | ForEach-Object { $_.Trim() } | Where-Object { $_ }
        $lines[2..($lines.Length - 1)] | ForEach-Object {
            $cells = ($_ -split '\|') | ForEach-Object { $_.Trim() } | Where-Object { $_ }
            $obj = @{}
            for ($i = 0; $i -lt $headers.Count; $i++) {
                $obj[$headers[$i]] = $cells[$i]
            }
            [PSCustomObject]$obj
        }
    }
    

    The table below contains the VM parameters in a readable table format that could scale.

    $table = @"
    | VMName   | Size              | Subnet  | VNet       | Image                                                   |
    |----------|-------------------|---------|------------|---------------------------------------------------------|
    | TestVm1  | Standard_D2as_v5  | default | TestVmVn   | MicrosoftWindowsDesktop:Windows-11:win11-23h2-ent:latest |
    | TestVm2  | Standard_D2as_v5  | default | TestVmVn   | MicrosoftWindowsDesktop:Windows-11:win11-24h2-ent:latest |
    "@
    
    $VMs = ConvertFrom-MdTable -MdTable $Table
    $VMs | Format-Table
    

    Azure PowerShell can consume the object generated from the table to deploy VMs.

    $AzParams     = @{Location = 'NorthCentralUS'; ResourceGroupName  = 'TestVmRg'}
    $cred         = New-Object System.Management.Automation.PSCredential "<>",$(ConvertTo-SecureString '<>' -asplaintext -force)
    
    $VMs | ForEach-Object {
        New-AzVM @AzParams -Name $_.VMName -Size $_.Size -Image $_.Image -Credential $cred `
            -SubnetName $_.Subnet -VirtualNetworkName $_.VNet
    }
    

    Example notebook: Notebook to deploy Azure VM