Repository navigation
Expand file tree
/
Copy path01_Server_Triggers.ps1
More file actions
153 lines (122 loc) · 3.33 KB
/
Copy path01_Server_Triggers.ps1
File metadata and controls
153 lines (122 loc) · 3.33 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<#
.SYNOPSIS
Gets the Server Triggers on the target server
.DESCRIPTION
Writes the Server Triggers out to the "01 - Server Triggers" folder
One file for all Triggers
.EXAMPLE
01_Server_Triggers.ps1 localhost
.EXAMPLE
01_Server_Triggers.ps1 server01 sa password
.Inputs
ServerName, [SQLUser], [SQLPassword]
.Outputs
.NOTES
.LINK
https://github.com/gwalkey
#>
[CmdletBinding()]
Param(
[string]$SQLInstance='localhost',
[string]$myuser,
[string]$mypass
)
# Load Common Modules and .NET Assemblies
try
{
Import-Module ".\SQLTranscriptase.psm1" -ErrorAction Stop
}
catch
{
Throw('SQLTranscriptase.psm1 not found')
}
LoadSQLSMO
# Init
Set-StrictMode -Version latest;
[string]$BaseFolder = (Get-Item -Path ".\" -Verbose).FullName
Write-Host -f Yellow -b Black "01 - Server Triggers"
Write-Output("Server: [{0}]" -f $SQLInstance)
# Server connection check
$SQLCMD1 = "select serverproperty('productversion') as 'Version'"
try
{
if ($mypass.Length -ge 1 -and $myuser.Length -ge 1)
{
Write-Output "Testing SQL Auth"
$myver = ConnectSQLAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $SQLCMD1 -User $myuser -Password $mypass -ErrorAction Stop| select -ExpandProperty Version
$serverauth="sql"
}
else
{
Write-Output "Testing Windows Auth"
$myver = ConnectWinAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $SQLCMD1 -ErrorAction Stop | select -ExpandProperty Version
$serverauth = "win"
}
if($myver -ne $null)
{
Write-Output ("SQL Version: {0}" -f $myver)
}
}
catch
{
Write-Host -f red "$SQLInstance appears offline."
Set-Location $BaseFolder
exit
}
[int]$ver = GetSQLNumericalVersion $myver
$SQLCMD2 =
"
SELECT
ssmod.definition AS [Definition],
'ENABLE TRIGGER ' + name +' ON ALL SERVER' as enablecmd
FROM
master.sys.server_triggers AS tr
LEFT OUTER JOIN master.sys.server_assembly_modules AS mod ON mod.object_id = tr.object_id
LEFT OUTER JOIN sys.server_sql_modules AS ssmod ON ssmod.object_id = tr.object_id
WHERE (tr.parent_class = 100)
"
# Run Query
if ($serverauth -eq "win")
{
try
{
$sqlresults2 = ConnectWinAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $SQLCMD2 -ErrorAction Stop
}
catch
{
Throw("Error Connecting to SQL: {0}" -f $error[0])
}
}
else
{
try
{
$sqlresults2 = ConnectSQLAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $SQLCMD2 -User $myuser -Password $mypass -ErrorAction Stop
}
catch
{
Throw("Error Connecting to SQL: {0}" -f $error[0])
}
}
# If No Results, write status file
if ($sqlresults2 -eq $null)
{
Write-Output "No Server Triggers Found on $SQLInstance"
echo null > "$BaseFolder\$SQLInstance\01 - No Server Triggers Found.txt"
Set-Location $BaseFolder
exit
}
# Create Output Folder
$fullfolderPath = "$BaseFolder\$sqlinstance\01 - Server Triggers"
if(!(test-path -path $fullfolderPath))
{
mkdir $fullfolderPath | Out-Null
}
# Script Out
Foreach ($row in $sqlresults2)
{
$row.Definition+"`r`nGO`r`n`r`n",$row.enableCMD+"`r`nGO`r`n" | out-file "$fullfolderPath\Server_Triggers.sql" -Encoding ascii -Append
Add-Content -Value "`r`n" -Path "$fullfolderPath\Server_Triggers.sql" -Encoding Ascii
}
# Return To Base
set-location $BaseFolder