-
Notifications
You must be signed in to change notification settings - Fork 0
/
Start SQL query.ps1
53 lines (48 loc) · 1.3 KB
/
Start SQL query.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
#Requires -Version 7
#Requires -Modules SqlServer
Param (
[Parameter(Mandatory)]
[String]$ComputerName,
[Parameter(Mandatory)]
[String]$DatabaseName,
[Parameter(Mandatory)]
[String[]]$SqlFileContents,
[Parameter(Mandatory)]
[String[]]$SqlFilePaths
)
$i = 0
foreach ($fileContent in $SqlFileContents) {
try {
$result = [PSCustomObject]@{
ComputerName = $ComputerName
DatabaseName = $DatabaseName
SqlFile = $SqlFilePaths[$i]
StartTime = Get-Date
EndTime = $null
Executed = $false
Error = $null
Output = @()
}
$result.StartTime = Get-Date
$params = @{
ServerInstance = $ComputerName
Database = $DatabaseName
Query = $fileContent
TrustServerCertificate = $true
QueryTimeout = '1000'
ConnectionTimeout = '20'
ErrorAction = 'Stop'
}
$result.Output += Invoke-Sqlcmd @params
$result.Executed = $true
}
catch {
$result.Error = $_
$error.RemoveAt(0)
}
finally {
$result.EndTime = Get-Date
$i++
$result
}
}