Hi guys!!
I am using Powershell through DollarU (Workload automation software) to execute SQL Agent job (all steps) or one specific step at a time. So far so good, everything is working!! However, when it gets to the point where I want to get the
History log information, I can't seem get what I want.
Here is my program:
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
}
When there are no specific step to execute... when only the jobName is provided, I get the proper information. When it is only a specific step, the second line (YYYY) returns information from past executions.
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)"
}
In the following picture, you will see the lines of History log information that I got in return for run executed at 10:42 on August 15th (???)
![]()
What I would like to get are the following lines of information:
![]()
So I tried the following lines of code without any success...
if ($job.EnumJobStepLogs($StepName).Rows[0] -ne $null)
{
write-host "xxxx $($job.EnumJobStepLogs($StepName).Rows[0].Message)"
}
if ($job.EnumJobStepLogs($StepName).Rows[1] -ne $null)
{
write-host "yyyyy $($job.EnumJobStepLogs($StepName).Rows[1].Message)"
}
and this...
if ($job.EnumHistory($stepName).Rows[0] -ne $null)
{
write-host "xxxx $($job.EnumHistory($stepName).Rows[0].Message)"
}
if ($job.EnumHistory($stepName).Rows[1] -ne $null)
{
write-host "yyyyy $($job.EnumHistory($stepName).Rows[1].Message)"
}
I really don't understand... and don't know what to try anymore :-(
Can you help me?
Mylene
Mylene Chalut