Sql-Server-Agent

Get-SqlAgentJobHistory 呼叫者

  • December 18, 2021

我想使用 Get-SqlAgentJobHistory 報告失敗的 Sql Agent 作業,但排除了服務帳戶未呼叫的作業。我正在執行這個 cmd:

Get-SqlAgentJobHistory -ServerInstance $SqlServer -OutcomesType Failed -StartRunDate ((Get-Date).AddMinutes(-15)) 

我想排除這些:

InstanceID       : 146788
SqlMessageID     : 0
Message          : The job failed.  The Job was ***invoked by User domain\user***.  The last step to run was step 3 (packagename Pkg).
StepID           : 0
StepName         : (Job outcome)

但包括這些

  InstanceID       : 146789
 SqlMessageID     : 0
Message          : The job failed.  The Job was ***invoked by User domain\serviceaccount***.  The last step to run was step 3 (packagename Pkg).
StepID           : 0
StepName         : (Job outcome)

嘗試根據消息文本過濾返回的對象。

Get-SqlAgentJobHistory -ServerInstance $SqlServer -OutcomesType Failed -StartRunDate ((Get-Date).AddMinutes(-15)) | Where-Object {$_.message -Like "*invoked by User domain\serviceaccount*"}

kvprasoon 在 powershell.org 上建議了對我有用的解決方案

| Where-Object -FilterScript {$_.Message -notmacth 'serviceaccount'}

引用自:https://dba.stackexchange.com/questions/221719