How to add a SQL code to a existing PS script

Sara 441 Reputation points
2024-07-01T13:52:38.3666667+00:00

I 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,561 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 34,516 Reputation points
    2024-07-01T15:12:35.4133333+00:00

    Please don't ask duplicate questions. Stick with your original question since that has the script attached to it.

    https://learn.microsoft.com/en-us/answers/questions/1785022/how-to-add-a-sql-code-to-a-powershell-script

    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.