Advanced Integration via PowerShell

EDITION: ENTERPRISE

It is also possible to integrate with SQL Monitor via PowerShell.

Example: Integrating with SQL Monitor for a PostgreSQL database

Instructions 

  1. Visit https://documentation.red-gate.com/sm/api/powershell-api to get familiar with SQL Monitor PowerShell API.
  2. Download SQL Monitor PowerShell module and paste the extracted contents into your migrations folder.
    For further information please see https://documentation.red-gate.com/sm/api/powershell-api/downloading-the-powershell-module
  3. Generate an authentication token in SQL Monitor. For further information please see https://documentation.red-gate.com/sm/api/powershell-api/authentication
  4. Create a new file called afterMigrateApplied.ps1 in your migrations folder.
  5. Copy and paste the following PowerShell code into this file. 

    afterMigrateApplied.ps1 (PostgreSQL)

    Import-Module .\migrations\RedgateSQM 
    $ErrorActionPreference = 'Stop' 
    Connect-SqlMonitor -ServerUrl '<ServerUrl>' -AuthToken '<AuthToken>' 
    
    # For more details about working with annotations see https://www.red-gate.com/SM13/powershell-annotations 
    
    $pgInstance = Get-SqlMonitorMonitoredObject -Type PostgresInstance -Name "<PostgreSQLInstanceName>" 
    
    Add-SqlMonitorAnnotation -Target $pgInstance -Description "Deployment" -UtcDate $([datetime]::UtcNow) -SpecificInfo "Flyway Deployment" 

    The following parameters need to be replaced:
    -ServerUrl and -AuthToken parameters need to be the SQL Monitor server, and the relevant authentication token generated in step 3.
    -Name parameter for the PostgreSQL instance
    -Description and -SpecificInfo parameters can be replaced with desired values.

  6. Save and commit/push to your repo. 

The next time Flyway migrates a PostgreSQL database, this PowerShell file will be executed and the SpecificInfo parameter in the PowerShell file will be displayed on the SQL Monitor activity graph for the relevant PostgreSQL instance. 

SQL Server

To use for SQL Server, use the following PowerShell instead, with the alterations described above:

afterMigrateApplied.ps1 (SQL Server)

Import-Module .\migrations\RedgateSQM 
$ErrorActionPreference = 'Stop' 
Connect-SqlMonitor -ServerUrl '<ServerUrl>' -AuthToken '<AuthToken>' 

# For more details about working with annotations see https://www.red-gate.com/SM13/powershell-annotations 

$sqlInstance = Get-SqlMonitorMonitoredObject -Type SqlServerInstance -Name "localhost\SQL2022" 
Add-SqlMonitorAnnotation -Target $sqlInstance -Description "Deployment" -UtcDate $([datetime]::UtcNow) -SpecificInfo "Flyway Deployment" 

Didn't find what you were looking for?