functions/Set-DbaAgentJobOutputFile.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
Function Set-DbaAgentJobOutputFile
{
<#
.Synopsis
   Sets the OutPut File for a step of an agent job with the Job Names and steps provided dynamically
.DESCRIPTION
   Sets the OutPut File for a step of an agent job with the Job Names and steps provided dynamically if required
 
.PARAMETER SqlInstance
    The SQL Server that you're connecting to.
 
.PARAMETER SQLCredential
    Credential object used to connect to the SQL Server as a different user be it Windows or SQL Server. Windows users are determiend by the existence of a backslash, so if you are intending to use an alternative Windows connection instead of a SQL login, ensure it contains a backslash.
 
.PARAMETER JobName
    The Agent Job Name to provide Output File Path for. Also available dynamically
 
.PARAMETER Step
    The Agent Job Step to provide Output File Path for. Also available dynamically
 
.PARAMETER OutputFile
    The Full Path to the New Output file
  
.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
 
.PARAMETER Confirm
Prompts you for confirmation before executing any changing operations within the command.
  
.EXAMPLE
   Set-DbaAgentJobOutputFile -sqlserver SERVERNAME -JobName 'The Agent Job' -OutPutFile E:\Logs\AgentJobStepOutput.txt
 
   Sets the Job step for The Agent job on SERVERNAME to E:\Logs\AgentJobStepOutput.txt
.NOTES
   AUTHOR - Rob Sewell https://sqldbawithabeard.com
   DATE - 30/10/2016
 
    dbatools PowerShell module (https://dbatools.io)
    Copyright (C) 2016 Chrissy LeMaire
    This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
  
 # todo - allow piping and add -All
#>

    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (# The Server/instance
        [Parameter(Mandatory = $true, HelpMessage = 'The SQL Server Instance',
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName = $true,
                   ValueFromRemainingArguments = $false,
                   Position = 0)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias("ServerInstance", "SqlServer")]
        [object[]]$SqlInstance,
        [Parameter(Mandatory = $false, HelpMessage = 'SQL Credential',
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName = $true,
                   ValueFromRemainingArguments = $false)]
        [System.Management.Automation.PSCredential]$SqlCredential,
        [Parameter(Mandatory = $true, HelpMessage = 'The Full Output File Path',
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName = $true,
                   ValueFromRemainingArguments = $false)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [string]$OutputFile,
        [Parameter(Mandatory = $false, HelpMessage = 'The Job Step name',
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [object[]]$Step)
    
    DynamicParam { if ($sqlinstance) { return (Get-ParamSqlJobs -SqlServer $sqlinstance[0] -SqlCredential $SourceSqlCredential) } }
    
    BEGIN
    {
        $JobName = $psboundparameters.Jobs
    }
    PROCESS
    {
        foreach ($instance in $sqlinstance)
        {
            try
            {
                $server = Connect-SqlServer -SqlServer $instance -SqlCredential $sqlcredential
            }
            catch
            {
                Write-Warning "Failed to connect to: $instance"
                continue
            }
            
            if (!$JobName)
            {
                # This is because jobname isn't yet required
                Write-Warning "You must specify a jobname using -Jobs"
                return
            }
            
            foreach ($name in $JobName)
            {
                $Job = $server.JobServer.Jobs[$name]
                
                If ($step)
                {
                    $steps = $Job.JobSteps | Where-Object Name -in $step
                    
                    if (!$steps)
                    {
                        Write-Warning "$step didn't return any steps"
                        return
                    }
                }
                else
                {
                    if (($Job.JobSteps).Count -gt 1)
                    {
                        Write-output "Which Job Step do you wish to add output file to?"
                        $steps = $Job.JobSteps | Out-GridView -Title "Choose the Job Steps to add an output file to" -PassThru -Verbose
                    }
                    else
                    {
                        $steps = $Job.JobSteps
                    }
                }
                
                if (!$steps)
                {
                    $steps = $Job.JobSteps
                }
                
                foreach ($jobstep in $steps)
                {
                    $currentoutputfile = $jobstep.OutputFileName
                    
                    Write-Verbose "Current Output File for $job is $currentoutputfile"
                    Write-Verbose "Adding $OutputFile to $jobstep for $Job"
                    
                    try
                    {
                        If ($Pscmdlet.ShouldProcess($jobstep, "Changing Output File from $currentoutputfile to $OutputFile"))
                        {
                            $jobstep.OutputFileName = $OutputFile
                            $jobstep.Alter()
                            $jobstep.Refresh()
                            
                            [pscustomobject]@{
                                ComputerName = $server.NetName
                                InstanceName = $server.ServiceName
                                SqlInstance = $server.DomainInstanceName
                                Job = $Job.Name
                                JobStep = $jobstep.Name
                                OutputFileName = $currentoutputfile
                            }
                        }
                    }
                    catch
                    {
                        Write-Warning "Failed to add $OutputFile to $jobstep for $JobName"
                    }
                }
            }
        }
    }
}