Meilleur auteur de réponses
Get SQL Agent Job EnumHistory via Powershell

Question
-
Bonjour,
J'ai déjà posé cette question sur le forum Anglais... sans succès :-(
Alors voyons ici...
J'utilise Powershell via DollarU (application d'automatisation de la charge de travail) pour exécuter toutes les étapes, ou seulement qu'une de ma tâche SQL Agent. Tout fonctionne tel que prévu... jusqu'à ce que j'essai d'obtenir l'information du journal de l'historique. Je ne parviens pas à obtenir les bonnes lignes du journal (???)
Voici mon programme:
param ([parameter (Mandatory = $true)][string]$ServerName, [parameter (Mandatory = $true)][string]$JobName, [string]$StepName = "") write-host "Starting SQL Agent Job $($JobName) on Server $($ServerName)" $date=Get-Date write-host "It is now: $($date)" [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null $srv = New-Object Microsoft.SqlServer.Management.SMO.Server("$ServerName") $job = $srv.jobserver.jobs["$JobName"] $jobstart="No" if (($job)) { if ($StepName -ne '') { $job.Start($StepName) $jobstart="Yes" Start-Sleep -s 5 # Pause for 5 seconds (optional) - was 30 seconds (v1); v2=5 } else { $job.Start() $jobstart="Yes" Start-Sleep -s 5 } } else { $jobstart="Not found" } if ($jobstart -eq "Yes") { write-host "Job $($JobName) on Server $($ServerName) started" $i=0 do { $job.Refresh(); $iRem = $i % 5; $jobrunning=$job.CurrentRunStatus.ToString(); if ($iRem -eq 0) { $date=Get-Date write-host "Job $($JobName) Processing--Run Step:$($job.CurrentRunStep) Status:$($job.CurrentRunStatus.ToString())... at $($date)" } Start-Sleep -s 10; # Pause for 10 seconds - was 60 seconds (v1); v2=10 $i++; } while ($job.CurrentRunStatus.ToString() -ne "Idle") if ($job.LastRunOutcome -ne "Cancelled") { write-host "Job Processing done" } else { write-host "Job Processing cancelled/aborted" } # $jobRunning="TRUE" write-host "$($srv.name) $($job.name)" write-host "Last job outcome $($job.LastRunOutcome)" write-host "Last job outcome $($job.LastRunDate)" if ($job.EnumHistory().Rows[0] -ne $null) { write-host "xxxx $($job.EnumHistory().Rows[0].Message)" } if ($job.EnumHistory().Rows[1] -ne $null) { write-host "yyyyy $($job.EnumHistory().Rows[1].Message)" } $LastRun=$job.LastRunOutcome if ($StepName -ne '') { $JobStep = $Job.JobSteps[$StepName] Write-Host "Name: $($JobStep.Name) RunDate: $($JobStep.LastRunDate) Status: $($JobStep.LastRunOutCome)" } else { $StepCount = $job.JobSteps.Count - 1 for ($i=0;$i -le $StepCount;$i++) { $m = $job.JobSteps[$i].LastRunDate write-host "Name: $($job.JobSteps[$i].Name) RunDate: $($job.JobSteps[$i].LastRunDate) Status: $($job.JobSteps[$i].LastRunOutCome)" if ($job.LastRunDate -gt $m) { $LastRun="FailedOrAborted" } } } if ($LastRun -eq "Failed") { write-host "Job returned with Failed status" exit 2 } if ($LastRun -ne "FailedOrAborted") { if ($LastRun -ne "Cancelled") { exit 0 } else { write-host "Job Cancelled xxxxx" exit 3 } } else { write-host "Job Failed or Aborted" exit 2 } } else { write-host "Unable to Start Job $($JobName) on Server $($ServerName)" write-host "Reason: Job may not exist or not enabled." exit 1 }
Cette partie de code me retour (sans farce) les lignes du journal de façon tout à fait aléatoire!!
if ($job.EnumHistory().Rows[0] -ne $null) { write-host "xxxx $($job.EnumHistory().Rows[0].Message)" } if ($job.EnumHistory().Rows[1] -ne $null) { write-host "yyyyy $($job.EnumHistory().Rows[1].Message)" }
En voici quelques exemples...
Dans l'image suivante, vous allez voir les lignes d'information que mon execution du 15 août à 10h42 a reçu:
Alors j'ai essayé de changer mon code pour qu'il ressemble à ceci...
if ($job.EnumHistory().Rows[0]) { write-host "xxxx $($job.EnumHistory().Rows[0].Message)" } if ($job.EnumHistory().Rows[1]) { write-host "yyyyy $($job.EnumHistory().Rows[1].Message)" }
Voici ce que j'ai obtenu...
Output:
xxxx The job succeeded. The Job was invoked by User STATCAN\BrBpmsDev. The last step to run was step 1 (ExecuteCriticalAndSRP_xlsFormat). The job was requested to start at step 1 (ExecuteCriticalAndSRP_xlsFormat). LIGNE ENCADRÉE EN ROUGE
yyyyy Executed as user: STATCAN\BR_SQLDEV. Microsoft (R) SQL Server Execute Package Utility Version 10.50.6000.34 for 64-bit Copyright (C) Microsoft Corporation 2010. All rights reserved. Started: 7:15:17 AM DTExec: The package execution returned DTSER_SUCCESS (0). Started: 7:15:17 AM Finished: 7:15:19 AM Elapsed: 1.953 seconds. The package executed successfully. The step succeeded. LIGNE EN ARBORESCENCE BLEUE
Je crois qu'il est important de noter que j'ai exécuté l'étape 2 seulement dans ces exemples.
Voici ce que je voudrais recevoir :
J'utilise Window Server Standard 2008 / SQLServer 2008 R2. À L'AIDE S.V.P.!!!
Mylene Chalut
- Modifié MyleneC jeudi 25 août 2016 13:14
Réponses
-
Dans Windows PowerShell, il faut d'abord taper : sqlps
puis ensuite les instructions suivantes :
PS SQLSERVER:\> $srv = new-object Microsoft.SqlServer.Management.Smo.Server("brdsqldev01")
PS SQLSERVER:\> $jb = $srv.JobServer.Jobs["BC_FrameUpdate_CriticalList_Mylene"]PS SQLSERVER:\> $jb.EnumHistory().Rows[0].MessagePS SQLSERVER:\> $jb.EnumHistory().Rows[1].Message
Les conseils, informations ou suggestions proposés sont fournis sans aucune garantie.
- Proposé comme réponse Claude Couderc samedi 24 septembre 2016 12:18
- Marqué comme réponse MyleneC mercredi 9 novembre 2016 18:49
Toutes les réponses
-
Bonjour,
Si vous tapez les lignes suivantes dans une invite de commande Windows PowerShell du serveur SQL Server, qu'obtenez-vous ?
sqlps
$srv = new-object Microsoft.SqlServer.Management.Smo.Server("< nom de l'instance>")
$jb = $srv.JobServer.Jobs["<nom du job>"]
$jb.EnumHistory().Rows[0].Message
$jb.EnumHistory().Rows[1].Message
Les conseils, informations ou suggestions proposés sont fournis sans aucune garantie.
- Modifié Claude Couderc jeudi 25 août 2016 16:16
-
-
L'instruction $jb.EnumHistory().Rows[0].Message aurait du fournir un retour.
Il faut vérifier $jb n'est pas vide par exemple en tapant :
$jb.VersionNumber
Par ailleurs, il faudrait essayer la série de cmdlets sans passer par DollarUScript (?)
Pour information, voilà ce que ça donne chez moi :
PS SQLSERVER:\> $srv = new-object Microsoft.SqlServer.Management.Smo.Server("SHAREPOINT\SQL2012")
PS SQLSERVER:\> $jb = $srv.JobServer.Jobs["MaintenancePlan.Subplan_1"]
PS SQLSERVER:\> $jb.EnumHistory().Rows[0].Message
Le travail a réussi. Le travail a été appelé par Planification 9 (MaintenancePlan.Subplan_1). La dernière étape exécutée est l'étape 1 (Subplan_1).
PS SQLSERVER:\>
Les conseils, informations ou suggestions proposés sont fournis sans aucune garantie.
- Modifié Claude Couderc vendredi 26 août 2016 07:07
-
J'aimerais préciser que j'utilise Windows Powershell. Est-ce que ça fait une difference?
Voici ce que j'ai obtenu...
J'ai par contre execute ces lignes via DollarU....
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null$srv = New-Object Microsoft.SqlServer.Management.SMO.Server("brdsqldev01")$job = $srv.jobserver.jobs["BC_FrameUpdate_CriticalList_Mylene"]#$JobStep = $Job.JobSteps["ExecuteCriticalAndSRP_xlsxFormat"]write-host "**********************************************************************************"$job.EnumHistory().Rows[0].Message$job.EnumHistory().Rows[1].Messagewrite-host "**********************************************************************************"
**********************************************************************************The job succeeded. The Job was invoked by User STATCAN\BrBpmsDev. The last step to run was step 2 (ExecuteCriticalAndSRP_xlsxFormat). The job was requested to start at step 2 (ExecuteCriticalAndSRP_xlsxFormat).
Executed as user: STATCAN\BR_SQLDEV. Microsoft (R) SQL Server Execute Package Utility Version 10.50.6000.34 for 64-bit Copyright (C) Microsoft Corporation 2010. All rights reserved. Started: 9:58:44 AM DTExec: The package execution returned DTSER_SUCCESS (0). Started: 9:58:44 AM Finished: 9:58:45 AM Elapsed: 1.328 seconds. The package executed successfully. The step succeeded.
**********************************************************************************Le premier paragraphe représente bien la dernière execution de la tâche. Par contre, le deuxième paragraphe me renvoi l'information de l'exécution de 9h58 ce matin. Il est maintenant 14h30. Donc encore une fois, la ligne "$jb.EnumHistory().Rows[1].Message" me renvoi pas mal n'importe quoi!!!
Il n'y a pas moyen d'obtenir la bonne information ailleurs? Dans les tables système par exemple?
Mylene Chalut
-
Dans Windows PowerShell, il faut d'abord taper : sqlps
puis ensuite les instructions suivantes :
PS SQLSERVER:\> $srv = new-object Microsoft.SqlServer.Management.Smo.Server("brdsqldev01")
PS SQLSERVER:\> $jb = $srv.JobServer.Jobs["BC_FrameUpdate_CriticalList_Mylene"]PS SQLSERVER:\> $jb.EnumHistory().Rows[0].MessagePS SQLSERVER:\> $jb.EnumHistory().Rows[1].Message
Les conseils, informations ou suggestions proposés sont fournis sans aucune garantie.
- Proposé comme réponse Claude Couderc samedi 24 septembre 2016 12:18
- Marqué comme réponse MyleneC mercredi 9 novembre 2016 18:49
-