Advanced Integration via PowerShell
Published 25 January 2024
EDITION: ENTERPRISE
It is also possible to integrate with SQL Monitor via PowerShell.
Example: Integrating with SQL Monitor for a PostgreSQL database
Instructions
- Visit https://documentation.red-gate.com/sm/api/powershell-api to get familiar with SQL Monitor PowerShell API.
- 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 - Generate an authentication token in SQL Monitor. For further information please see https://documentation.red-gate.com/sm/api/powershell-api/authentication
- Create a new file called afterMigrateApplied.ps1 in your migrations folder.
Copy and paste the following PowerShell code into this file.
afterMigrateApplied.ps1 (PostgreSQL) Toggle source code
- 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.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) Toggle source code
- 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"