Powershell script to view SQL Maintenance plan History

Loganathan R 106 Reputation points
2021-05-27T06:20:44.537+00:00

Hello All,
I'm having couple of SQL Server which is configured with Maintenance plan Differential Backup, Full Backup, Log Backup & Cleanup.
I need to view the History of Maintenance plan from one system using powershell script. Any one can help me out or other kindly share the how can i get the report from all the server maintenance plan history from single machine.

Regards,
Loganathan. R

SQL Server Other
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2021-05-27T06:35:39.197+00:00

    I need to view the History of Maintenance plan

    Maintenance plans don't have a history, but the SQL Server-Agent job execution the MP do have a history.
    You can use the PowerShell CmdLet dbatools => Get-DbaAgentJobHistory function to get the history.

    https://www.powershellgallery.com/packages/dbatools/0.9.380/Content/functions%5CGet-DbaAgentJobHistory.ps1


  2. CarrinWu-MSFT 6,891 Reputation points
    2021-05-28T02:50:05.573+00:00

    Hi @Loganathan R ,

    Welcome to Microsoft Q&A!

    For SQL Server, you could use below script to get some detail about maintenance plan:

    Select @@SERVERNAME [servername],  
    case when D.Succeeded=1 then 'Success' when D.succeeded=0 then 'Failed' End as Result,  
    A.name,B.subplan_name,D.line1,D.line2,D.line3,D.line4,  
    D.line5,D.start_time,D.end_time,D.command  
    From msdb.dbo.sysmaintplan_plans a inner join msdb.dbo.sysmaintplan_subplans b on a.id=b.plan_id  
    inner join msdb.dbo.sysmaintplan_log c on c.plan_id=b.plan_id and c.Subplan_id=b.subplan_id  
    inner join msdb.dbo.sysmaintplan_logdetail d on d.task_detail_id=c.task_detail_id  
    Order By D.start_time DESC  
    

    But I am not familiar with Powershell. After some research, I found a document as below, hope it will help you:
    Getting Details from a Maintenance Plan using PowerShell

    Best regards,
    Carrin


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    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.