1
+ <#
2
+ . SYNOPSIS
3
+ Gets the Policy Based Mgmt Objects on the target server
4
+
5
+ . DESCRIPTION
6
+ Writes the Policies and Facets out to the "22 - PBM" folder
7
+
8
+ . EXAMPLE
9
+ 22_Policy_Based_Mgmt.ps1 localhost
10
+
11
+ . EXAMPLE
12
+ 22_Policy_Based_Mgmt.ps1 server01 sa password
13
+
14
+ . Inputs
15
+ ServerName\instance, [SQLUser], [SQLPassword]
16
+
17
+ . Outputs
18
+
19
+
20
+ . NOTES
21
+ https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.dmf.aspx
22
+ https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.facets.aspx
23
+
24
+ Feb 1, 2021 - Switched to building SQL statements with SQL as the SMO libraries are trash
25
+
26
+ . LINK
27
+ https://github.com/gwalkey
28
+
29
+ #>
30
+
31
+ [CmdletBinding ()]
32
+ Param (
33
+ [string ]$SQLInstance = " localhost" ,
34
+ [string ]$myuser ,
35
+ [string ]$mypass
36
+ )
37
+
38
+ # Load Common Modules and .NET Assemblies
39
+ try
40
+ {
41
+ Import-Module " .\SQLTranscriptase.psm1" - ErrorAction Stop
42
+ }
43
+ catch
44
+ {
45
+ Throw (' SQLTranscriptase.psm1 not found' )
46
+ }
47
+
48
+ # Init
49
+ Set-StrictMode - Version latest;
50
+ [string ]$BaseFolder = (Get-Item - Path " .\" - Verbose).FullName
51
+ Write-Host -f Yellow - b Black " 22 - Policy Based Mgmt Objects"
52
+ Write-Output (" Server: [{0}]" -f $SQLInstance )
53
+
54
+ # Server connection check
55
+ $SQLCMD1 = " select serverproperty('productversion') as 'Version'"
56
+ try
57
+ {
58
+ if ($mypass.Length -ge 1 -and $myuser.Length -ge 1 )
59
+ {
60
+ Write-Output " Testing SQL Auth"
61
+ $myver = ConnectSQLAuth - SQLInstance $SQLInstance - Database " master" - SQLExec $SQLCMD1 - User $myuser - Password $mypass - ErrorAction Stop| Select-Object - ExpandProperty Version
62
+ $serverauth = " sql"
63
+ }
64
+ else
65
+ {
66
+ Write-Output " Testing Windows Auth"
67
+ $myver = ConnectWinAuth - SQLInstance $SQLInstance - Database " master" - SQLExec $SQLCMD1 - ErrorAction Stop | Select-Object - ExpandProperty Version
68
+ $serverauth = " win"
69
+ }
70
+
71
+ if ($null -eq $myver )
72
+ {
73
+ Write-Output (" SQL Version: {0}" -f $myver )
74
+ }
75
+
76
+ }
77
+ catch
78
+ {
79
+ Write-Host -f red " $SQLInstance appears offline."
80
+ Set-Location $BaseFolder
81
+ exit
82
+ }
83
+
84
+ # Get Conditions
85
+ $SQLCMD1 = " SELECT * FROM [msdb].[dbo].[syspolicy_conditions] WHERE is_system=0"
86
+ if ($serverauth -eq ' win' )
87
+ {
88
+ $Conditions = Connect-InternalSQLServer - SQLInstance $SQLInstance - Database ' msdb' - SQLExec $SQLCMD1 - ErrorAction Stop
89
+ }
90
+ else {
91
+ $Conditions = Connect-ExternalSQLServer - SQLInstance $SQLInstance - Database ' msdb' - SQLExec $SQLCMD1 - User $myuser - Password $mypass - ErrorAction Stop
92
+ }
93
+
94
+
95
+ # Get Policies
96
+ $SQLCMD2 = "
97
+ SELECT
98
+ c.name AS 'condition_name',
99
+ a.name AS 'policy_category',
100
+ p.*
101
+ FROM
102
+ msdb.dbo.syspolicy_policies P
103
+ LEFT JOIN
104
+ dbo.syspolicy_conditions C
105
+ ON
106
+ c.condition_id = p.condition_id
107
+ LEFT JOIN
108
+ [syspolicy_policy_categories] A
109
+ ON
110
+ A.policy_category_id = P.policy_category_id
111
+ WHERE
112
+ c.is_system=0
113
+ "
114
+
115
+ if ($serverauth -eq ' win' )
116
+ {
117
+ $Policies = Connect-InternalSQLServer - SQLInstance $SQLInstance - Database ' msdb' - SQLExec $SQLCMD2 - ErrorAction Stop
118
+ }
119
+ else {
120
+ $Policies = Connect-ExternalSQLServer - SQLInstance $SQLInstance - Database ' msdb' - SQLExec $SQLCMD2 - User $myuser - Password $mypass - ErrorAction Stop
121
+ }
122
+
123
+
124
+ # Prep Output Folders
125
+ Write-Output " $SQLInstance - PBM"
126
+ $Output_path = " $BaseFolder \$SQLInstance \22 - PBM\"
127
+ if (! (test-path - path $Output_path ))
128
+ {
129
+ mkdir $Output_path | Out-Null
130
+ }
131
+
132
+ # Policies
133
+ $POutput_path = " $BaseFolder \$SQLInstance \22 - PBM\Policies\"
134
+ if (! (test-path - path $POutput_path ))
135
+ {
136
+ mkdir $POutput_path | Out-Null
137
+ }
138
+
139
+ # Conditions
140
+ $COutput_path = " $BaseFolder \$SQLInstance \22 - PBM\Conditions\"
141
+ if (! (test-path - path $COutput_path ))
142
+ {
143
+ mkdir $COutput_path | Out-Null
144
+ }
145
+
146
+ Write-Output " Writing Out..."
147
+
148
+ # Script Out
149
+ if ($null -ne $Conditions )
150
+ {
151
+ Write-Output " Exporting PBM Conditions..."
152
+
153
+ foreach ($Condition in $Conditions )
154
+ {
155
+ $myCName = $Condition.Name
156
+ $myfixedName = $myCName.replace (' \' , ' _' )
157
+ $myfixedName = $myfixedName.replace (' !' , ' _' )
158
+ $myfixedName = $myfixedName.replace (' /' , ' _' )
159
+ $myfixedName = $myfixedName.replace (' %' , ' _' )
160
+ $Outfilename = $COutput_path + " $myfixedName .sql"
161
+ " " | out-file - filepath $Outfilename - force - Encoding default
162
+
163
+ # Build SQL Statement
164
+ " EXEC msdb.dbo.sp_syspolicy_add_condition" | Out-File - FilePath $Outfilename - Append - Encoding default
165
+ " @name=N'" + $condition.name + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
166
+ " @description=N'" + $condition.description + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
167
+ " @facet=N'" + $condition.facet + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
168
+ " @expression=N'" + $condition.expression + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
169
+ " @is_name_condition=" + $condition.is_name_condition + " ," | Out-File - FilePath $Outfilename - Append - Encoding default
170
+ " @obj_name=N'" + $condition.is_name_condition + " '`r`n " | Out-File - FilePath $Outfilename - Append - Encoding default
171
+
172
+
173
+ }
174
+ }
175
+
176
+ if ($null -ne $Policies )
177
+ {
178
+ Write-Output " Exporting PBM Policies..."
179
+
180
+ foreach ($Policy in $Policies )
181
+ {
182
+ $myPName = $Policy.Name
183
+ $myfixedName = $myPName.replace (' \' , ' _' )
184
+ $myfixedName = $myfixedName.replace (' !' , ' _' )
185
+ $myfixedName = $myfixedName.replace (' /' , ' _' )
186
+ $myfixedName = $myfixedName.replace (' %' , ' _' )
187
+ $Outfilename = $POutput_path + " $myfixedName .sql"
188
+ " " | out-file - filepath $Outfilename - force - Encoding default
189
+
190
+ # Build SQL Statement
191
+ $Policy_id = $policy.policy_id
192
+
193
+ # sp_syspolicy_add_object_set
194
+ $SQLCMD3 = ' SELECT * FROM [msdb].[dbo].[syspolicy_object_sets] WHERE object_set_id=' + $Policy_ID
195
+ if ($serverauth -eq ' win' )
196
+ {
197
+ $Syspolicy_object_sets = Connect-InternalSQLServer - SQLInstance $SQLInstance - Database ' msdb' - SQLExec $SQLCMD3
198
+ }
199
+ else {
200
+ $Syspolicy_object_sets = Connect-ExternalSQLServer - SQLInstance $SQLInstance - Database ' msdb' - SQLExec $SQLCMD3 - User $myuser - Password $mypass - ErrorAction Stop
201
+ }
202
+ if ($null -ne $Syspolicy_object_sets )
203
+ {
204
+ $object_set_name = $Syspolicy_object_sets.object_set_name
205
+ $facet_name = $Syspolicy_object_sets.facet_name
206
+
207
+ " DECLARE @object_set_id INT;" | Out-File - FilePath $Outfilename - Append - Encoding default
208
+ " EXEC msdb.dbo.sp_syspolicy_add_object_set @object_set_name = N'" + $object_set_name + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
209
+ " @facet = N'" + $facet_name + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
210
+ " @object_set_id = @object_set_id OUTPUT;" | Out-File - FilePath $Outfilename - Append - Encoding default
211
+ " SELECT @object_set_id;`r`n " | Out-File - FilePath $Outfilename - Append - Encoding default
212
+ }
213
+
214
+
215
+ # sp_syspolicy_add_target_set
216
+ $SQLCMD4 = ' SELECT * FROM [msdb].[dbo].[syspolicy_target_sets] WHERE object_set_id=' + $Policy_ID
217
+ if ($serverauth -eq ' win' )
218
+ {
219
+ $Syspolicy_target_sets = Connect-InternalSQLServer - SQLInstance $SQLInstance - Database ' msdb' - SQLExec $SQLCMD4
220
+ }
221
+ else {
222
+ $Syspolicy_target_sets = Connect-ExternalSQLServer - SQLInstance $SQLInstance - Database ' msdb' - SQLExec $SQLCMD4 - User $myuser - Password $mypass - ErrorAction Stop
223
+ }
224
+ if ($null -ne $Syspolicy_target_sets )
225
+ {
226
+ $target_set_id = $Syspolicy_target_sets.target_set_id
227
+ $target_type_skeleton = $Syspolicy_target_sets.type_skeleton
228
+ $target_set_type = $Syspolicy_target_sets.type
229
+ $target_set_enabled = $Syspolicy_target_sets.enabled
230
+
231
+ " DECLARE @target_set_id INT;" | Out-File - FilePath $Outfilename - Append - Encoding default
232
+ " EXEC msdb.dbo.sp_syspolicy_add_target_set @object_set_name = N'" + $object_set_name + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
233
+ " @type_skeleton = N'" + $target_type_skeleton + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
234
+ " @type = N'" + $target_set_type + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
235
+ " @enabled = " + $target_set_enabled + " ," | Out-File - FilePath $Outfilename - Append - Encoding default
236
+ " @target_set_id = @target_set_id OUTPUT;" | Out-File - FilePath $Outfilename - Append - Encoding default
237
+ " SELECT @target_set_id;`r`n " | Out-File - FilePath $Outfilename - Append - Encoding default
238
+ }
239
+
240
+
241
+ # sp_syspolicy_add_target_set_level
242
+ $SQLCMD5 = ' SELECT * FROM [msdb].[dbo].[syspolicy_target_set_levels] WHERE target_set_id=' + $target_set_id
243
+ if ($serverauth -eq ' win' )
244
+ {
245
+ $Syspolicy_target_set_levels = Connect-InternalSQLServer - SQLInstance $SQLInstance - Database ' msdb' - SQLExec $SQLCMD5
246
+ }
247
+ else {
248
+ $Syspolicy_target_set_levels = Connect-ExternalSQLServer - SQLInstance $SQLInstance - Database ' msdb' - SQLExec $SQLCMD5 - User $myuser - Password $mypass - ErrorAction Stop
249
+ }
250
+ if ($null -ne $Syspolicy_target_set_levels )
251
+ {
252
+ $target_set_level_type_skeleton = $Syspolicy_target_set_levels.type_skeleton
253
+ $target_set_level_level_name = $Syspolicy_target_set_levels.level_name
254
+ $target_set_level_condition_id = $Syspolicy_target_set_levels.condition_id
255
+
256
+ " EXEC msdb.dbo.sp_syspolicy_add_target_set_level @target_set_id = @target_set_id," | Out-File - FilePath $Outfilename - Append - Encoding default
257
+ " @type_skeleton = N'" + $target_set_level_type_skeleton + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
258
+ " @level_name = N'" + $target_set_level_level_name + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
259
+ " @condition_name = N'" + $target_set_level_condition_id + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
260
+ " @target_set_level_id = 0;`r`n " | Out-File - FilePath $Outfilename - Append - Encoding default
261
+
262
+ }
263
+
264
+ " GO`r`n " | Out-File - FilePath $Outfilename - Append - Encoding default
265
+
266
+ # sp_syspolicy_add_policy
267
+ $policy_enabled = $policy.is_enabled
268
+
269
+ " DECLARE @policy_id INT;" | Out-File - FilePath $Outfilename - Append - Encoding default
270
+ " EXEC msdb.dbo.sp_syspolicy_add_policy @name = N'" + $policy.Name + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
271
+ " @condition_name = N'" + $policy.condition_name + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
272
+ " @policy_category = N'" + $policy.policy_category + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
273
+ " @description = N'" + $policy.description + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
274
+ " @help_text = N'" + $policy.help_text + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
275
+ " @help_link = N'" + $policy.help_link + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
276
+ " @schedule_uid = N'" + $policy.schedule_uid + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
277
+ " @execution_mode = " + $policy.execution_mode + " ," | Out-File - FilePath $Outfilename - Append - Encoding default
278
+ " @is_enabled = " + $policy_enabled + " ," | Out-File - FilePath $Outfilename - Append - Encoding default
279
+ " @policy_id = @policy_id OUTPUT," | Out-File - FilePath $Outfilename - Append - Encoding default
280
+ " @root_condition_name = N''," | Out-File - FilePath $Outfilename - Append - Encoding default
281
+ " @object_set = N'" + $object_set_name + " '," | Out-File - FilePath $Outfilename - Append - Encoding default
282
+ " SELECT @policy_id;" | Out-File - FilePath $Outfilename - Append - Encoding default
283
+ " GO`r`n " | Out-File - FilePath $Outfilename - Append - Encoding default
284
+ }
285
+ }
286
+
287
+ # Return to Base
288
+ set-location $BaseFolder
0 commit comments