-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartDatabaseJob.ps1
More file actions
91 lines (55 loc) · 2.34 KB
/
startDatabaseJob.ps1
File metadata and controls
91 lines (55 loc) · 2.34 KB
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
<#
Purpose: To Start SQL Server Job with intergrated security
Date: 3/1/2016
Note:
- The client will pass the server name and the database job name
- the initiator of the call to connect to the database must have access to the database
#>
$sqlConn = New-Object System.Data.SqlClient.SqlConnection "server=$server;database=$database;Integrated Security=sspi;Application Name=Powershell Script"
# Open Database Connection
function DatabaseConnection {
#Vars for Server and JobName
param([string]$server, [string]$action, [string]$database)
# create new database object
$sqlConn = New-Object System.Data.SqlClient.SqlConnection "server=$server;database=$database;Integrated Security=sspi;Application Name=Powershell Script"
if ($action -eq "open") {
$sqlConn.Open()
}
else {
$sqlConn.Close()
}
return
}
function startDatabaseJob {
#Vars for Server and JobName
param([string]$Server, [string]$JobName)
#Create/Open Connection
DatabaseConnection -Action "open" -Server $Server -database "msdb"
#Create Command Obj
$sqlCommand = $sqlConn.CreateCommand()
$sqlCommand.CommandText = "EXEC dbo.sp_start_job N'$JobName'"
#Exec Command
$sqlCommand.ExecuteReader()
#Close Conneection
DatabaseConnection -Action "close" -Server $Server
}
<# function to update item with image information #>
function runTSQLScript ([string]$server, [string]$database, [string]$tsqlScript)
{
# open database connection
$connection = New-Object System.Data.SqlClient.SqlConnection "server=$server;database=$database;Integrated Security=sspi;Application Name=Powershell Script"
write-host (get-date -format('MM/dd/yyyy hh:mm:ss')) "Connecting to database $database"
$connection.Open()
# create database command object
$command = $connection.CreateCommand()
$query = $tsqlScript
# assign query string for execution
$command.CommandText = $query
write-host (get-date -format('MM/dd/yyyy hh:mm:ss')) "Running: $query "
# database call
$result = $command.ExecuteNonQuery()
# open database connection
$connection.Close()
write-host (get-date -format('MM/dd/yyyy hh:mm:ss')) "Closing connection to database $database"
return
}