Repository navigation
Expand file tree
/
Copy path16_Audits.ps1
More file actions
186 lines (147 loc) · 4.89 KB
/
Copy path16_Audits.ps1
File metadata and controls
186 lines (147 loc) · 4.89 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<#
.SYNOPSIS
Gets the Audits and Audit Specifications on the target server
.DESCRIPTION
Writes the SQL Server Credentials out to the "16 - Audits" folder
Saves the Server-level audits and specs as "Servername_ServerAudit_Auditname.sql" and the "ServerName_ServerAuditSpec_SpecName.sql"
Saves the Database-level Audits and Specs into their own folder
.EXAMPLE
16_Audits.ps1 localhost
.EXAMPLE
16_Audits.ps1 server01 sa password
.Inputs
ServerName, [SQLUser], [SQLPassword]
.Outputs
.NOTES
MSDN References:
https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.audit.enumserverauditspecification.aspx
https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.audit.enumdatabaseauditspecification.aspx
.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 "16 - Audits"
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
# New UP SQL SMO Object
if ($serverauth -eq "win")
{
$srv = New-Object "Microsoft.SqlServer.Management.SMO.Server" $SQLInstance
}
else
{
$srv = New-Object "Microsoft.SqlServer.Management.SMO.Server" $SQLInstance
$srv.ConnectionContext.LoginSecure=$false
$srv.ConnectionContext.set_Login($myuser)
$srv.ConnectionContext.set_Password($mypass)
}
# Create Output folder
$fullfolderPath = "$BaseFolder\$sqlinstance\16 - Audits"
if(!(test-path -path $fullfolderPath))
{
mkdir $fullfolderPath | Out-Null
}
# -----------------
# 1) Server Audits
# -----------------
foreach($Audit in $srv.Audits)
{
# Script out objects for each DB
$strmyauditName = $SQLInstance+"_ServerAudit_"+$audit.name+".sql"
$strmyaudit = $Audit.script()
$output_path = "$BaseFolder\$SQLInstance\16 - Audits\$strmyAuditName"
Write-Output ("Server Audit: {0}" -f $audit.Name)
$strmyaudit | out-file $output_path -Force -encoding ascii
}
# -------------------------------
# 2) Server Audit Specifications
# -------------------------------
foreach($Audit in $srv.ServerAuditSpecifications)
{
# Script out objects for each DB
$strmyauditName = $SQLInstance+"_ServerAuditSpec_"+$audit.name+".sql"
$strmyaudit = $Audit.script()
$output_path = "$BaseFolder\$SQLInstance\16 - Audits\$strmyAuditName"
Write-Output ("Server Audit Spec: {0}" -f $audit.Name)
$strmyaudit | out-file $output_path -Force -encoding ascii
}
# -------------------
# 3) Database Audits
# -------------------
foreach($sqlDatabase in $srv.databases)
{
# Skip System Databases
# Comment out if you want to include System Databases
if ($sqlDatabase.Name -in ('Master','Model','MSDB','TempDB')) {continue}
# Script out Audits per DB
$db = $sqlDatabase
$fixedDBName = $db.name.replace('[','')
$fixedDBName = $fixedDBName.replace(']','')
$DB_Audit_output_path = "$BaseFolder\$SQLInstance\16 - Audits\$fixedDBName"
# Skip Offline Databases (SMO still enumerates them, but cant retrieve the objects)
if ($sqlDatabase.Status -ne 'Normal')
{
Write-Output ("Skipping Offline: {0}" -f $sqlDatabase.Name)
continue
}
foreach($DBAudit in $db.DatabaseAuditSpecifications)
{
# Script out objects for each DB
$strmyauditName = $fixedDBName+"_DBAuditSpec.sql"
$strmyaudit = $DBAudit.script()
$output_path = $DB_Audit_output_path+"\"+$strmyAuditName
if(!(test-path -path $DB_Audit_output_path))
{
mkdir $DB_Audit_output_path | Out-Null
}
Write-Output ("Database Audits for: {0}" -f $fixedDBName)
$strmyaudit | out-file $output_path -Force -encoding ascii
}
}
# Return To Base
set-location $BaseFolder