0

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

工作历史限制

4

1 回答 1

1

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

于 2014-02-13T14:37:44.590 回答