forked from DetectiveEric/PowerShell-ETL-SQL-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartJob-AndWait.ps1
130 lines (109 loc) · 7.44 KB
/
StartJob-AndWait.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
#requires -version 2.0
##############################################################################
## This script starts a remote SQL Agent Job and wait for it to complete
## until a MaximumRuntime is met
##############################################################################
#[CmdletBinding()]
Param (
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $ServerName,
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $JobName,
[int] $MaxRuntimeHours = 24
)
#Setup powershell execution params
$ErrorActionPreference ="Stop"
$DebugPreference ="Continue" #this shows all debug messges, "SilentlyContinue" will supress
#Setup internal script params
$err=0
$WaitIntervalSeconds = 4;
$CheckIntervalSeconds = 2;
function GetSqlServerObject([string] $Server) {
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null
$srv = New-Object Microsoft.SqlServer.Management.Smo.Server("$Server")
#We have the object, but need to confirm we can connect
if ($srv.Urn -eq $null) {
Write-Error "ERROR Connecting to SqlServer $Server"
throw "ERROR Connecting to SqlServer $Server"
}
return $srv
}
###############################################################################
########## Main body ##########################################################
Try
{
Write-Debug "ServerName=$ServerName"
Write-Debug "JobName=$JobName"
$SqlServer = GetSqlServerObject $ServerName
$Job = $SqlServer.JobServer.Jobs[$JobName]
echo "initial"
Write-Debug "Job.LastRunDate=$($Job.LastRunDate)"
Write-Debug "Job.LastRunOutcome=$($Job.LastRunOutcome)"
Write-Debug "Job.IsTouched=$($Job.IsTouched)"
Write-Debug "Job.IsObjectDirty=$($Job.IsObjectDirty)"
Write-Debug "Job.State=$($Job.State)"
Write-Debug "Job.CurrentRunStatus=$($Job.CurrentRunStatus)"
$Job.Start();
echo "after start"
Write-Debug "Job.LastRunDate=$($Job.LastRunDate)"
Write-Debug "Job.IsTouched=$($Job.IsTouched)"
Write-Debug "Job.IsObjectDirty=$($Job.IsObjectDirty)"
Write-Debug "Job.State=$($Job.State)"
Write-Debug "Job.CurrentRunStatus=$($Job.CurrentRunStatus)"
$Job.Refresh();
echo "after refresh"
Write-Debug "Job.LastRunDate=$($Job.LastRunDate)"
Write-Debug "Job.IsTouched=$($Job.IsTouched)"
Write-Debug "Job.IsObjectDirty=$($Job.IsObjectDirty)"
Write-Debug "Job.State=$($Job.State)"
Write-Debug "Job.CurrentRunStatus=$($Job.CurrentRunStatus)"
#If already running, exit w/failure
#Wait for SQL Server to start the job
Write-Debug "Begin wait for $WaitIntervalSeconds seconds";
Start-Sleep -Seconds $WaitIntervalSeconds;
$Job.Refresh();
echo "after wait interval & refresh"
Write-Debug "Job.LastRunDate=$($Job.LastRunDate)"
Write-Debug "Job.IsTouched=$($Job.IsTouched)"
Write-Debug "Job.IsObjectDirty=$($Job.IsObjectDirty)"
Write-Debug "Job.State=$($Job.State)"
Write-Debug "Job.CurrentRunStatus=$($Job.CurrentRunStatus)"
$i=0
#Loop while the job runs
while (
$Job.CurrentRunStatus -ne `
[Microsoft.SqlServer.Management.Smo.Agent.JobExecutionStatus]::Idle `
)
{
Start-Sleep -Seconds $CheckIntervalSeconds;
$Job.Refresh();
echo "i=$i"
echo "after loop interval & refresh"
Write-Debug "Job.LastRunDate=$($Job.LastRunDate)"
Write-Debug "Job.IsTouched=$($Job.IsTouched)"
Write-Debug "Job.IsObjectDirty=$($Job.IsObjectDirty)"
Write-Debug "Job.State=$($Job.State)"
Write-Debug "Job.CurrentRunStatus=$($Job.CurrentRunStatus)"
$i++
};
echo $Job.LastRunDate, $Job.LastRunOutcome
}
Catch [System.Exception]
{
$ex = $_.Exception
Write-Host $ex.Message
$err=1
}
Finally
{
if (!$err) {
Write-Host "Success!"
exit 0
}
else {
Write-Error "Failed!"
exit 1
}
}