【问题标题】:Microsoft.SqlServer.Management.Smo.Server.Jobs.Job.EnumHistory() is limited to 100 rowsMicrosoft.SqlServer.Management.Smo.Server.Jobs.Job.EnumHistory() 限制为 100 行
【发布时间】:2014-02-11 20:07:03
【问题描述】:

我正在尝试编写一个脚本来扫描 Sql 维护任务失败 - 请参阅下面的脚本。我似乎无法使用 EnumHistory() 处理超过 100 个条目。有没有人有办法解决这个问题?

Param(
  [int]$days="30"  # this hardly matters since EnumJobHistory is limited to 100 rows :-(
)

#http://powershell.com/cs/blogs/tobias/archive/2010/01/13/cancelling-a-pipeline.aspx
filter Stop-Pipeline([scriptblock]$condition = {$true}) 
{$_
    if (& $condition) {continue}
}

cls
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null

$instances = Get-Content "DailyMaintenanceMMCServerList.txt"

#loop through each instance
 foreach ($instance in $instances)
 {

    # Create an SMO connection to the instance
    $srv = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $instance
    $instance

    #get all the jobs on the server
    $jobs = $srv.JobServer.Jobs

    # Avoid exception on some servers?
    if (!$jobs.Count) 
    {
        continue
    }

    #go through each job and find failures in the job history
    $jobs |  % {
        do 
        {   $job = $_; $count = 0; 
            $_.EnumHistory() | 
            Stop-Pipeline { $_.Rundate -lt  [datetime]::Today.AddDays(-$days) } |
            #? {$_.Message -notlike "*succeeded*" } |
            % { " " + ++$count + " " + $job.Name + " " + $_.RunDate + " " + ($_.Message).Substring(0,20) }
        } while ($false)
    }
 }

正如 Ben Thul 所指出的,保留的最大历史记录行数由服务器实例配置:

【问题讨论】:

  • 呃!请将此作为答案,以便我标记它!谢谢!

标签: sql-server powershell jobs smo maintenance-plan


【解决方案1】:

检查代理配置保留多少历史记录。在 powershell 中,您可以从 JobServer 对象中的 MaximumJobHistoryRows 中获取此信息。或者右键单击 SSMS 中的代理并查看“历史记录”。我的猜测是它只配置为每个作业保留 100 个。

【讨论】:

    猜你喜欢
    • 2019-12-25
    • 2010-11-25
    • 1970-01-01
    • 2023-03-03
    • 2011-03-08
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多