|
| 1 | +Function Get-SQLAgentJobOutPutFile |
| 2 | +{ |
| 3 | +<# |
| 4 | +.Synopsis |
| 5 | + Returns the OutPut File for each step of an agent job with the Job Names provided dynamically |
| 6 | +.DESCRIPTION |
| 7 | + This function returns the output file value for each step in an agent job with the Job Names provided dynamically |
| 8 | +.EXAMPLE |
| 9 | + Get-SQLAgentJobOutPutFile -instance SERVERNAME -JobName 'The Agent Job' |
| 10 | +
|
| 11 | + This will return the paths to the output files foreach job step of the The Agent Job Job on the SERVERNAME instance |
| 12 | +.NOTES |
| 13 | + AUTHOR - Rob Sewell https://sqldbawithabeard.com |
| 14 | + DATE - 30/10/2016 |
| 15 | +#> |
| 16 | +#Requires -Version 5 |
| 17 | +#Requires -Module sqlserver |
| 18 | +param |
| 19 | +(# The Server/instance |
| 20 | + [Parameter(Mandatory=$true,HelpMessage='The Instance', |
| 21 | + ValueFromPipeline=$true, |
| 22 | + ValueFromPipelineByPropertyName=$true, |
| 23 | + ValueFromRemainingArguments=$false, |
| 24 | + Position=0)] |
| 25 | + [ValidateNotNull()] |
| 26 | + [ValidateNotNullOrEmpty()] |
| 27 | + [string]$Instance) |
| 28 | + DynamicParam { |
| 29 | + # Set the dynamic parameters' name |
| 30 | + $ParameterName = 'JobName' |
| 31 | + |
| 32 | + # Create the dictionary |
| 33 | + $RuntimeParameterDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary |
| 34 | + |
| 35 | + # Create the collection of attributes |
| 36 | + $AttributeCollection = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute] |
| 37 | + |
| 38 | + # Create and set the parameters' attributes |
| 39 | + $ParameterAttribute = New-Object -TypeName System.Management.Automation.ParameterAttribute |
| 40 | + $ParameterAttribute.Mandatory = $true |
| 41 | + $ParameterAttribute.Position = 1 |
| 42 | + |
| 43 | + # Add the attributes to the attributes collection |
| 44 | + $AttributeCollection.Add($ParameterAttribute) |
| 45 | + |
| 46 | + # Generate and set the ValidateSet |
| 47 | + $arrSet = (Get-SQLAgentJob -ServerInstance $Server).Name |
| 48 | + $ValidateSetAttribute = New-Object -TypeName System.Management.Automation.ValidateSetAttribute -ArgumentList ($arrSet) |
| 49 | + |
| 50 | + # Add the ValidateSet to the attributes collection |
| 51 | + $AttributeCollection.Add($ValidateSetAttribute) |
| 52 | + |
| 53 | + # Create and return the dynamic parameter |
| 54 | + $RuntimeParameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter -ArgumentList ($ParameterName, [string], $AttributeCollection) |
| 55 | + $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter) |
| 56 | + return $RuntimeParameterDictionary |
| 57 | + } |
| 58 | + |
| 59 | + begin { |
| 60 | + # Bind the parameter to a friendly variable |
| 61 | + $JobName = $PsBoundParameters[$ParameterName] |
| 62 | + } |
| 63 | + process |
| 64 | + { |
| 65 | + $Job = Get-SQLAgentJob -ServerInstance $Instance -Name $JobName |
| 66 | + if($Instance.Contains('\')) |
| 67 | + { |
| 68 | + $Server = $Instance.Split('\')[0] |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + $Server = $Instance |
| 73 | + } |
| 74 | + foreach($Step in $Job.JobSteps) |
| 75 | + { |
| 76 | + $fileName = $Step.OutputFileName |
| 77 | + $Name = '\\' + $Server + '\' + $Filename.Replace(':','$') |
| 78 | + Write-Output $Name |
| 79 | + } |
| 80 | +} |
| 81 | +end{} |
| 82 | +} |
| 83 | + |
0 commit comments