Skip to content

Commit 87cb702

Browse files
added Get-SQLAgentJobDuration
1 parent 2028753 commit 87cb702

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

Get-SQLAgentJobDuration.ps1

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
Function Get-SQLAgentJobDuration
2+
{
3+
<#
4+
.Synopsis
5+
Returns the Job Duration for an agent job on an instance or number of instances with the Job Names provided dynamically
6+
.DESCRIPTION
7+
This function returns the Job Duration for an agent job on an instance or number of instances with the Job Names provided dynamically
8+
It can also output to CSV and prompt if youwould like to open the file
9+
.EXAMPLE
10+
Get-SQLAgentJobDuration -Instances SERVER -JobName 'Job must run quickly'
11+
12+
This will return the servername. jobname, rundate and duration of the 'Job must run quickly' job on the Instance SERVER
13+
.EXAMPLE
14+
Get-SQLAgentJobDuration -Instances SERVER -JobName 'Job must run quickly' -CSV
15+
16+
This will output the servername. jobname, rundate and duration of the 'Job must run quickly' job on the Instance SERVER into a CSV file
17+
located in the Users MyDocuments folder named JobName_Date_Time.csv
18+
.EXAMPLE
19+
Get-SQLAgentJobDuration -Instances SERVER -JobName 'Job must run quickly' -CSV -Path c:\temp
20+
21+
This will output the servername. jobname, rundate and duration of the 'Job must run quickly' job on the Instance SERVER into a CSV file
22+
located in C:\Temp named JobName_Date_Time.csv
23+
.NOTES
24+
AUTHOR - Rob Sewell https://sqldbawithabeard.com
25+
DATE - 30/10/2016
26+
#>
27+
#Requires -Version 5
28+
#Requires -Module sqlserver
29+
param
30+
(# The Server/instance
31+
[Parameter(Mandatory=$true,HelpMessage='The Instance',
32+
ValueFromPipeline=$true,
33+
ValueFromPipelineByPropertyName=$true,
34+
ValueFromRemainingArguments=$false,
35+
Position=0)]
36+
[ValidateNotNull()]
37+
[ValidateNotNullOrEmpty()]
38+
[object]$Instances,
39+
# CSV required
40+
[Parameter(Mandatory=$false,HelpMessage='Want to output to CSV')]
41+
[switch]$CSV,
42+
# Path for CSV
43+
[Parameter(Mandatory=$false,HelpMessage='Path for CSV')]
44+
[object]$Path
45+
)
46+
DynamicParam {
47+
# Set the dynamic parameters' name
48+
$ParameterName = 'JobName'
49+
50+
# Create the dictionary
51+
$RuntimeParameterDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
52+
53+
# Create the collection of attributes
54+
$AttributeCollection = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
55+
56+
# Create and set the parameters' attributes
57+
$ParameterAttribute = New-Object -TypeName System.Management.Automation.ParameterAttribute
58+
$ParameterAttribute.Mandatory = $true
59+
$ParameterAttribute.Position = 1
60+
61+
# Add the attributes to the attributes collection
62+
$AttributeCollection.Add($ParameterAttribute)
63+
64+
# Generate and set the ValidateSet
65+
$arrSet = (Get-SQLAgentJob -ServerInstance $Instances).Name
66+
$ValidateSetAttribute = New-Object -TypeName System.Management.Automation.ValidateSetAttribute -ArgumentList ($arrSet)
67+
68+
# Add the ValidateSet to the attributes collection
69+
$AttributeCollection.Add($ValidateSetAttribute)
70+
71+
# Create and return the dynamic parameter
72+
$RuntimeParameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter -ArgumentList ($ParameterName, [string], $AttributeCollection)
73+
$RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
74+
return $RuntimeParameterDictionary
75+
}
76+
77+
begin {
78+
# Bind the parameter to a friendly variable
79+
$JobName = $PsBoundParameters[$ParameterName]
80+
$FormattedDuration = @{Name = 'FormattedDuration';Expression = {[timespan]$_.RunDuration.ToString().PadLeft(6,'0').insert(4,':').insert(2,':')}}
81+
}
82+
Process {
83+
$JObs = (Get-SQLAgentJobHistory -ServerInstance $Instances).Where{$_.JoBName -eq $JobName}
84+
$Result = $Jobs.Where{$_.Stepid -eq 0} | Select Server, JobName,RunDate,$FormattedDuration
85+
}
86+
End {
87+
if($CSV)
88+
{
89+
if(!$Path)
90+
{
91+
$docs = [Environment]::GetFolderPath("mydocuments")
92+
$Path = $Docs
93+
}
94+
95+
$Date = Get-Date -Format yyyyMMdd_HHmmss
96+
$FilePath = $Path + '\' + $jobname + '_' + $Date + '.csv'
97+
$Result | Export-Csv -Path $FilePath -NoTypeInformation
98+
99+
# Prompt to create and then create.
100+
$title = "Want to Open the file??"
101+
$message = "Would you like to open the CSV file now? (Y/N)"
102+
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Will continue"
103+
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Will exit"
104+
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
105+
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
106+
107+
if ($result -eq 1)
108+
{
109+
return "OK File is located at $FilePath"
110+
}
111+
else
112+
{
113+
Invoke-Item -Path $FilePath
114+
}
115+
}
116+
else
117+
{
118+
$Result
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)