<############ Import the SQL Sentry Module See link to below documentation for path to your powershell module based upon Version number https://docs.sentryone.com/help/powershell-module If you are unfamiliar with DBATools here is the link on how to get the module installed https://dbatools.io/download/ #############> <################################ ################################ Script #1 ################################ ################################> $ErrorActionPreference = "stop" $InformationPreference = "Continue" Import-Module DBATools Import-Module "C:\Program Files\SolarWinds SQL Sentry\2022.0\Intercerve.SQLSentry.Powershell.psd1" ##Connect to you SQL Sentry Deployment Connect-SQLSentry -ServerName localhost -DatabaseName SQLSentry <# Use DBATools to populate Targets variable from my Server Inventory If you dont have a Server Inventory DB but have a CMS here is how to get those server as well $Targets = Get-DbaRegServer -SqlInstance | Select Name #> $TargetQuery = "Select Trim(SQLInstance) as SQLInstance from Servers.Servers" $Targets = Invoke-DbaQuery -SqlInstance localhost -Database StackOverFlow -Query $TargetQuery ##Loop through Machines to want to add as target to SQL Sentry Foreach($Target in $Targets) { Try ##Try\Catch block to catch any expections { Write-Information "Adding Target $($Target.SQLInstance)" ##Register the Windows Machine Register-Computer -Name $($Target.SQLInstance) -ComputerType Windows -AccessLevel Full ##Register the SQL Server and then Watch the target to being monitoring ##Remove the port parameter if using default ports Register-Connection -Name $($Target.SQLInstance) -AccessLevel Full -ConnectionType SqlServer -port 51111 | Invoke-WatchConnection }##try block catch [Exception] { #If this process bombs it will print to the screen why. write-information "GENERAL FAILURE! for $($Target.MachineName)" ##When I want to know the full exception type: $errormessage = $Error[0].Exception.Message -replace "'", "" write-information $errormessage }##catch block }##foreach block ##close connection to SQL Sentry Instance Disconnect-SQLSentry <################################ ################################ Script #2 ################################ ################################> Param( [Parameter(Mandatory = $true )] $PathToServerList = "C:\PS_Scripts\Servers.txt", [Parameter(Mandatory = $true )] $PathToSQLSentryModule = "C:\Users\XXXXX\Documents\sentryone\client\XXXXXXXX\SentryOne\bin\Intercerve.SQLSentry.Powershell.psd1", $InformationPreference = "Continue", $WarningPreference = "Continue" ) Import-Module $PathToSQLSentryModule $Instances = Get-Content -path $PathToServerList | Sort-Object | Select-Object -Unique foreach($Instance in $Instances){ # See if we already have the instance added $result = Get-Connection -name $Instance # If we didn't find any results in search for the instance, the script will add it if( $null -eq $result ){ $InfoMsg = "Need to add this instance $($Instance)" Write-Information -MessageData $InfoMsg # Add instance into SQL Sentry try{ Register-Connection -ConnectionType SqlServer -Name "$($Instance)" -AccessLevel Full | Invoke-WatchConnection Start-Sleep -s 10 $InfoMsg = "Done adding instance $($Instance)" Write-Information -MessageData $InfoMsg } # If any issues, the error will be thrown catch [Exception] { $ErrorMsgText = "Issue adding $($Instance)" Write-Error -Message $ErrorMsgText $ErrorMsgTextFull = $Error[0].Exception.Message -replace "'", "" write-Error -Message $ErrorMsgTextFull } } # If we already have the server added, the msg below will display else{ Write-Warning "We already have this instance $($Instance)" } }