SQL Server 2012 – Using SMO in PowerShell to Append Jobstep Output File

powershellsmosql-server-2012

I deploy multiple SQL Server jobs to a fleet of servers using a PowerShell script. I can create the job steps, success fail action, and even the output file. This all works on multiple servers. What I cannot determine is how to specify that the output file is to be appended to instead of overwritten.

The following is the section from my main script. $Job is populated in a previous step.

$JobStep = new-object ('Microsoft.SqlServer.Management.Smo.Agent.JobStep') ($Job, 'MyJobStepName')
$JobStep.ID = 1
$JobStep.SubSystem = 'PowerShell'
$Command = 'dir d:\'
$JobStep.Command   = $Command
$JobStep.OnSuccessAction = 'GotoNextStep'
$JobStep.OnFailAction    = 'GotoNextStep'
$JobStep.OutputFileName  = 'D:\MyJobFile.txt'
$JobStep.Create()

I have been using this MSDN page as a reference.

I modified one of the steps using SQL Server Management Studio and traced the result. This looks promising. I can issue a SQL command after I create the jobsteps.

EXEC msdb.dbo.sp_update_jobstep @job_id=N'12156dcb-c8cd-4267-901a-55175dd44960', @step_id=1 ,
        @flags=2

Best Answer

I think it is more along below lines

$jobStep.JobStepFlags = "AppendToLogFile"
  #call the alter to modify
$jobStep.Alter()
  #display it
$jobStep.JobStepFlags