functions/Get-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
Function Get-DbaAgentJobOutputFile
{
<#
.Synopsis
   Returns the Output File for each step of one or many agent job with the Job Names provided dynamically if
   required for one or more SQL Instances
 
.DESCRIPTION
   This function returns for one or more SQL Instances the output file value for each step of one or many agent job with the Job Names
   provided dynamically. It will not return anything if there is no Output File
 
.PARAMETER SqlInstance
    The SQL Server that you're connecting to. Or an array of SQL Servers
 
.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. If ommitted all Agent Jobs will be used
 
.EXAMPLE
   Get-DbaAgentJobOutputFile -SqlServer SERVERNAME -Jobs 'The Agent Job'
 
   This will return the paths to the output files for each of the job step of the The Agent Job Job
   on the SERVERNAME instance
 
.EXAMPLE
   Get-DbaAgentJobOutputFile -SqlServer SERVERNAME
 
   This will return the paths to the output files for each of the job step of all the Agent Jobs
   on the SERVERNAME instance
 
.EXAMPLE
   Get-DbaAgentJobOutputFile -SqlServer SERVERNAME,SERVERNAME2 -Jobs 'The Agent Job'
 
   This will return the paths to the output files for each of the job step of the The Agent Job Job
   on the SERVERNAME instance and SERVERNAME2
 
.EXAMPLE
   $Servers = 'SERVER','SERVER\INSTANCE1'
   Get-DbaAgentJobOutputFile -SqlServer $Servers -Jobs 'The Agent Job' -OpenFile
 
   This will return the paths to the output files for each of the job step of the The Agent Job Job
   on the SERVER instance and the SERVER\INSTANCE1 and open the files if they are available
 
.EXAMPLE
   Get-DbaAgentJobOutputFile -SqlServer SERVERNAME | Out-GridView
 
   This will return the paths to the output files for each of the job step of all the Agent Jobs
   on the SERVERNAME instance and Pipe them to Out-GridView
 
.EXAMPLE
   (Get-DbaAgentJobOutputFile -SqlServer SERVERNAME | ogv -PassThru).FileName | Invoke-Item
 
   This will return the paths to the output files for each of the job step of all the Agent Jobs
   on the SERVERNAME instance and Pipe them to Out-GridView and enable you to choose the output
   file and open it
    
.EXAMPLE
   Get-DbaAgentJobOutputFile -SqlServer SERVERNAME -Verbose
 
   This will return the paths to the output files for each of the job step of all the Agent Jobs
   on the SERVERNAME instance and also show the job steps without an output file
 
.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/.
 
#>

    [CmdletBinding()]
    param
    (
        [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',
                   ValueFromPipelineByPropertyName = $true,
                   ValueFromRemainingArguments = $false,
                   Position = 1)]
        [System.Management.Automation.PSCredential]$SqlCredential
    )
    
    DynamicParam { if ($SqlInstance) { return (Get-ParamSqlJobs -SqlServer $SqlInstance[0] -SqlCredential $SqlCredential) } }
    
    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
            }
            
            $jobs = $Server.JobServer.Jobs
            
            if ($JobName)
            {
                $jobs = @()
                
                foreach ($name in $jobname)
                {
                    $jobs += $server.JobServer.Jobs[$name]
                }
            }
            else
            {
                $jobs = $server.JobServer.Jobs
            }
            
            foreach ($Job in $Jobs)
            {
                foreach ($Step in $Job.JobSteps)
                {
                    if ($Step.OutputFileName)
                    {
                        [pscustomobject]@{
                            ComputerName = $server.NetName
                            InstanceName = $server.ServiceName
                            SqlInstance = $server.DomainInstanceName
                            Job = $Job.Name
                            JobStep = $step.Name
                            OutputFileName = $Step.OutputFileName
                            RemoteOutputFileName = Join-AdminUNC $Server.ComputerNamePhysicalNetBIOS $Step.OutputFileName
                        }
                    }
                    else
                    {
                        Write-Verbose "$step for $job has no output file"
                    }
                }
            }
        }
    }
}