How to add a Sql code to a powershell script

Sara 441 Reputation points
2024-06-28T14:46:35.28+00:00

backup_report.txtI have the below sql script to check if any failover happened on the last 24 hrs and we want to use that in the existing powershell backup report script to check that condition before it reports, the below sql code only returns a timestamp

;WITH cte_HADR AS (SELECT object_name, CONVERT(XML, event_data) AS data FROM sys.fn_xe_file_target_read_file('AlwaysOn*.xel',null,null,null) WHERE object_name = 'error_reported' )

SELECT Top 1 data.value ('(/event/@timestamp)[1]','datetime') AS [timestamp] FROM cte_HADR WHERE data.value('(/event/data[@name=''error_number''])[1]','int') = 1480

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,549 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Sara 441 Reputation points
    2024-06-28T14:57:10.9433333+00:00
    0 comments No comments

  2. Rich Matheisen 46,811 Reputation points
    2024-06-28T15:14:50.1966667+00:00

    There's no "$Query" variable in your script. There's a "$BKQuery" though. Are you just using the wrong variable name as the Invoke-SQLCommand cmdlets "-Query" parameter value?


  3. Neuvi Jiang 1,450 Reputation points Microsoft Vendor
    2024-07-03T07:50:42.8166667+00:00

    Hi Sara,

    Thank you for posting in the Q&A Forums.

    If you want to use this SQL query in a PowerShell script, you can use the class in Invoke-Sqlcmd (requires the SQL Server PowerShell module) or the System.Data.SqlClient namespace to execute the SQL query.

    Here's an example of PowerShell using Invoke-Sqlcmd:

    $server = "YourServerName"

    $database = "master"

    $query = @"

    DECLARE @AvailabilityGroupName NVARCHAR(256) = 'YourAvailabilityGroupName';

    DECLARE @Threshold DATETIME = DATEADD(HOUR, -24, GETDATE());

    SELECT

    replica_server_name,

    primary_replica,

    is_local,

    last_failover_time,

    failover_ready

    FROM

    sys.dm_hadr_availability_replica_states

    WHERE

    group_id = (SELECT group_id FROM sys.availability_groups WHERE name = @AvailabilityGroupName)

    AND last_failover_time > @Threshold

    AND last_failover_time IS NOT NULL;

    "@

    $result = Invoke-Sqlcmd -ServerInstance $server -Database $database -Query $query

    if ($result -ne $null) {

    Write-Host "Failover detected within the last 24 hours:"

    $result | Format-Table

    } else {

    Write-Host "No failover detected within the last 24 hours."

    }

    Best regards

    NeuviJ

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.