forked from linus123/TinyReturns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mssql.psm1
114 lines (92 loc) · 3.34 KB
/
mssql.psm1
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
Function RunDatabaseScriptsFromFolder([string]$serverName, [string]$databaseName, [string]$databaseDirectory) {
$files = Get-ChildItem "$databaseDirectory\*.sql"
foreach ($file in $files)
{
$fileName = $file.name
Write-Host "Applying script: $fileName"
$fileContents = Get-Content "$file"
$sql = [string]::Join([Environment]::NewLine, $fileContents);
ExecuteSqlQuery $serverName $databaseName $sql
}
}
Function ExecuteSqlFile([string]$serverName, [string]$databaseName, [string]$filePath) {
Write-Host "Applying script $filePath on server $serverName database $databaseName"
$fileContents = Get-Content "$filePath"
$sql = [string]::Join([Environment]::NewLine, $fileContents);
ExecuteSqlQuery $serverName $databaseName $sql
}
Function ExecuteSqlQuery([string]$serverName, [string]$databaseName, [string]$sql) {
$null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
$null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum")
$null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$Server = new-object Microsoft.SqlServer.Management.Smo.Server($serverName)
$db = $server.Databases[$databaseName]
Try
{
$db.ExecuteNonQuery($sql)
}
Catch [system.exception]
{
ResolveError $_.Exception
throw
}
}
Function CreateSqlDatabase([string] $serverName, [string] $databaseName) {
Write-Host "Attempting to create database $databaseName on server $serverName"
Try{
$database = New-Object ('Microsoft.SqlServer.Management.Smo.Database') -argumentlist $serverName,$databaseName
$database.Create()
}
Catch [system.exception] {
ResolveError $_.Exception
throw
}
}
Function KillProcessesOnDatabase([string] $serverName, [string] $databaseName) {
Write-Host "Attempting kill all processes $databaseName on server $serverName"
Try{
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
$SMOserver = New-Object ('Microsoft.SqlServer.Management.Smo.Server') -argumentlist $serverName
if ($SMOserver.Databases[$databaseName] -ne $null) {
Write-Host "Killing all processes on datbase $databaseName."
$SMOserver.KillAllProcesses($databaseName)
}
else {
Write-Host "$databaseName does not exist."
}
}
Catch [system.exception] {
ResolveError $_.Exception
throw
}
}
Function DropSqlDatabase([string] $serverName, [string] $databaseName) {
Write-Host "Attempting to drop database $databaseName on server $serverName"
Try{
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
$SMOserver = New-Object ('Microsoft.SqlServer.Management.Smo.Server') -argumentlist $serverName
if ($SMOserver.Databases[$databaseName] -ne $null) {
Write-Host "Killing all processes on datbase $databaseName."
$SMOserver.KillAllProcesses($databaseName)
Write-Host "Dropping database $databaseName."
$SMOserver.Databases[$databaseName].drop()
}
else {
Write-Host "$databaseName does not exist."
}
}
Catch [system.exception] {
ResolveError $_.Exception
throw
}
}
function ResolveError($ErrorRecord=$Error[0])
{
$ErrorRecord | Format-List * -Force
$ErrorRecord.InvocationInfo |Format-List *
$Exception = $ErrorRecord.Exception
for ($i = 0; $Exception; $i++, ($Exception = $Exception.InnerException))
{ "$i" * 80
$Exception |Format-List * -Force
}
}