Microsoft.SqlServer.Management.Smo.Server.Jobs.Job.EnumHistory()限制为100行

时间:2014-02-11 20:07:03

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

我正在尝试编写一个脚本来扫描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所指出的,保留的最大历史行数由服务器实例配置:

Job History Limits

1 个答案:

答案 0 :(得分:1)

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