Skip to content

Instantly share code, notes, and snippets.

@SQLvariant
Last active May 27, 2022 09:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SQLvariant/15cb08237a998e6fbf50ec6eecc6988c to your computer and use it in GitHub Desktop.
Save SQLvariant/15cb08237a998e6fbf50ec6eecc6988c to your computer and use it in GitHub Desktop.
3 different options for using the Invoke-SqlNotebook cmdlet

Use with Registered Servers

Use Registered Servers or Central Management Server to run Invoke-SqlNotebook against multiple servers.

$datetime = Get-Date -Format yyyyMMddhhmm

dir 'SQLSERVER:\SQLRegistration\Database Engine Server Group' |
WHERE { $_.Mode -ne 'd'} |            
foreach {
    Get-SqlInstance -ServerInstance $_.Name |
    foreach {
            Invoke-SqlNotebook -ServerInstance $_.Name -Database master -InputFile '$home\Documents\SQL Server Management Studio\BPCheck.ipynb' `
            -OutputFile "BPCheck_output_$($_.NetName)_$($datetime).ipynb"
            }
        }

Open Results in Azure Data Studio

Install the PowerShell extension from the Azure Data Studio marketplace. Use the PowerShell Integrated Console in Azure Data Studio to run Invoke-SqlNotebook and use Open-EditorFile to open the results of the SQL Notebook directly in Azure Data Studio.

Invoke-SqlNotebook -ServerInstance ServerA -Database master -InputFile "$home\Documents\SQL Server Management Studio\BPCheck.ipynb" |
Open-EditorFile
dir 'SQLSERVER:\SQLRegistration\Database Engine Server Group' |
WHERE { $_.Mode -ne 'd'} |
foreach {
Get-SqlInstance -ServerInstance $_.Name |
foreach {
Invoke-SqlNotebook -ServerInstance $_.Name -Database master -InputFile '$home\Documents\SQL Server Management Studio\BPCheck.ipynb' `
-OutputFile "BPCheck_output_$($_.NetName)_$($datetime).ipynb"
}
}
@sqlworldwide
Copy link

Any idea why am I getting this error:

[TAIOB] C:\Users\taiob
--> Invoke-SqlNotebook -ServerInstance DESKTOP-50O69FS -Database master -InputFile "C:\Presentation\NotebookJobs\DatabaseConfiguration.ipynb" -OutputFile "C:\Presentation\NotebookJobs\outtest.ipynb" | Open-EditorFile

MethodInvocationException: C:\Users\taiob.azuredatastudio\extensions\microsoft.powershell-2021.12.0\modules\PowerShellEditorServices\Commands\Public\CmdletInterface.ps1:199:13
Line |
199 | $psEditor.Workspace.OpenFile($_.FullName, $preview)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Exception calling "OpenFile" with "2" argument(s): "One or more errors occurred. (Internal error. )"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment