Skip to content

Commit 18b3f24

Browse files
authored
Add files via upload
1 parent ef97d56 commit 18b3f24

File tree

1 file changed

+264
-0
lines changed

1 file changed

+264
-0
lines changed

Scripts/22_Policy_Based_Mgmt.ps1

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
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+
.LINK
25+
https://github.com/gwalkey
26+
27+
#>
28+
29+
[CmdletBinding()]
30+
Param(
31+
[string]$SQLInstance="localhost",
32+
[string]$myuser,
33+
[string]$mypass
34+
)
35+
36+
# Load Common Modules and .NET Assemblies
37+
try
38+
{
39+
Import-Module ".\SQLTranscriptase.psm1" -ErrorAction Stop
40+
}
41+
catch
42+
{
43+
Throw('SQLTranscriptase.psm1 not found')
44+
}
45+
46+
try
47+
{
48+
Import-Module ".\LoadSQLSmo.psm1"
49+
}
50+
catch
51+
{
52+
Throw('LoadSQLSmo.psm1 not found')
53+
}
54+
55+
LoadSQLSMO
56+
57+
# Init
58+
Set-StrictMode -Version latest;
59+
[string]$BaseFolder = (Get-Item -Path ".\" -Verbose).FullName
60+
Write-Host -f Yellow -b Black "22 - Policy Based Mgmt Objects"
61+
Write-Output("Server: [{0}]" -f $SQLInstance)
62+
63+
# Load DMF Assemblies
64+
[Reflection.Assembly]::LoadWithPartialName(Microsoft.SqlServer.Dmf)
65+
[Reflection.Assembly]::LoadWithPartialName(Microsoft.SqlServer.Management.Sdk.Sfc)
66+
67+
<#
68+
$dmfver = $null;
69+
70+
71+
try
72+
{
73+
# 2019
74+
$dmfver = 15
75+
Add-Type -AssemblyName 'Microsoft.SqlServer.Dmf, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ErrorAction Stop
76+
Add-Type -AssemblyName 'Microsoft.SqlServer.Management.Sdk.Sfc, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ErrorAction Stop
77+
Write-Output "Using DMF Library v15 (2019)"
78+
}
79+
catch
80+
{
81+
}
82+
83+
try
84+
{
85+
# 2017
86+
$dmfver = 14
87+
Add-Type -AssemblyName 'Microsoft.SqlServer.Dmf, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ErrorAction Stop
88+
Add-Type -AssemblyName 'Microsoft.SqlServer.Management.Sdk.Sfc, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ErrorAction Stop
89+
Write-Output "Using DMF Library v14 (2017)"
90+
}
91+
catch
92+
{
93+
}
94+
95+
try
96+
{
97+
# 2016
98+
$dmfver = 13
99+
Add-Type -AssemblyName 'Microsoft.SqlServer.Dmf, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ErrorAction Stop
100+
Add-Type -AssemblyName 'Microsoft.SqlServer.Management.Sdk.Sfc, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ErrorAction Stop
101+
Write-Output "Using DMF Library v13 (2016)"
102+
}
103+
catch
104+
{
105+
}
106+
try
107+
{
108+
# 2014
109+
$dmfver = 12
110+
Add-Type -AssemblyName 'Microsoft.SqlServer.Dmf, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ErrorAction Stop
111+
Add-Type -AssemblyName 'Microsoft.SqlServer.Management.Sdk.Sfc, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ErrorAction Stop
112+
Write-Output "Using DMF Library v12 (2014)"
113+
}
114+
catch
115+
{
116+
}
117+
try
118+
{
119+
# 2012
120+
$dmfver = 11
121+
Add-Type -AssemblyName 'Microsoft.SqlServer.Dmf, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ErrorAction Stop
122+
Add-Type -AssemblyName 'Microsoft.SqlServer.Management.Sdk.Sfc, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ErrorAction Stop
123+
Write-Output "Using DMF Library v11 (2012)"
124+
}
125+
catch
126+
{
127+
}
128+
try
129+
{
130+
# 2008
131+
$dmfver = 10
132+
Add-Type -AssemblyName 'Microsoft.SqlServer.Dmf, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ErrorAction Stop
133+
Add-Type -AssemblyName 'Microsoft.SqlServer.Management.Sdk.Sfc, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ErrorAction Stop
134+
Write-Output "Using DMF Library v10 (2008)"
135+
}
136+
catch
137+
{
138+
}
139+
140+
If ($dmfver -eq $null)
141+
{
142+
Write-Output "'Microsoft.SqlServer.Dmf.dll' or 'Microsoft.SqlServer.Management.Sdk.Sfc.dll' not found, exiting"
143+
exit
144+
}
145+
146+
#>
147+
# Server connection check
148+
$SQLCMD1 = "select serverproperty('productversion') as 'Version'"
149+
try
150+
{
151+
if ($mypass.Length -ge 1 -and $myuser.Length -ge 1)
152+
{
153+
Write-Output "Testing SQL Auth"
154+
$myver = ConnectSQLAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $SQLCMD1 -User $myuser -Password $mypass -ErrorAction Stop| select -ExpandProperty Version
155+
$serverauth="sql"
156+
}
157+
else
158+
{
159+
Write-Output "Testing Windows Auth"
160+
$myver = ConnectWinAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $SQLCMD1 -ErrorAction Stop | select -ExpandProperty Version
161+
$serverauth = "win"
162+
}
163+
164+
if($myver -ne $null)
165+
{
166+
Write-Output ("SQL Version: {0}" -f $myver)
167+
}
168+
169+
}
170+
catch
171+
{
172+
Write-Host -f red "$SQLInstance appears offline."
173+
Set-Location $BaseFolder
174+
exit
175+
}
176+
177+
178+
# New UP SFC Object
179+
if ($serverauth -eq "win")
180+
{
181+
$conn = New-Object Microsoft.SqlServer.Management.Sdk.Sfc.SqlStoreConnection("server='$SQLInstance';Trusted_Connection=true")
182+
$PolicyStore = New-Object Microsoft.SqlServer.Management.DMF.PolicyStore($conn)
183+
}
184+
else
185+
{
186+
$conn = New-Object Microsoft.SqlServer.Management.Sdk.Sfc.SqlStoreConnection("server='$SQLInstance';Trusted_Connection=false; User Id=$myuser; Password=$mypass")
187+
$PolicyStore = New-Object Microsoft.SqlServer.Management.DMF.PolicyStore($conn)
188+
}
189+
190+
191+
# Prep Output Folders
192+
Write-Output "$SQLInstance - PBM"
193+
$Output_path = "$BaseFolder\$SQLInstance\22 - PBM\"
194+
if(!(test-path -path $Output_path))
195+
{
196+
mkdir $Output_path | Out-Null
197+
}
198+
199+
# Policies
200+
$POutput_path = "$BaseFolder\$SQLInstance\22 - PBM\Policies\"
201+
if(!(test-path -path $POutput_path))
202+
{
203+
mkdir $POutput_path | Out-Null
204+
}
205+
206+
# Conditions
207+
$COutput_path = "$BaseFolder\$SQLInstance\22 - PBM\Conditions\"
208+
if(!(test-path -path $COutput_path))
209+
{
210+
mkdir $COutput_path | Out-Null
211+
}
212+
213+
Write-Output "Writing Out..."
214+
215+
# Script Out
216+
if ($PolicyStore -ne $null)
217+
{
218+
# New UP Policy Store Object
219+
Write-Output "Exporting PBM Policies and Conditions..."
220+
$PolicyStore = New-Object Microsoft.SqlServer.Management.DMF.PolicyStore($conn)
221+
222+
# Script out Policies
223+
$Policies = $PolicyStore.Policies | Where-Object { -not $_.IsSystemObject }
224+
foreach($policy in $Policies)
225+
{
226+
$myPName = $Policy.Name
227+
$myfixedName = $myPName.replace('\','_')
228+
$myfixedName = $myfixedName.replace('!','_')
229+
$myfixedName = $myfixedName.replace('/','_')
230+
$myfixedName = $myfixedName.replace('%','_')
231+
$Outfilename = $POutput_path+"$myfixedName.xml"
232+
233+
Write-Output("Policy: {0}" -f $myfixedName)
234+
$xmlWriter = [System.Xml.XmlWriter]::Create($Outfilename)
235+
$policy.Serialize($xmlWriter)
236+
$xmlWriter.Close()
237+
}
238+
239+
240+
# Script out Conditions
241+
$myConditions = $PolicyStore.Conditions | Where-Object { -not $_.IsSystemObject }
242+
foreach($Condition in $myConditions)
243+
{
244+
$myCName = $Condition.Name
245+
$myfixedName = $myCName.replace('\','_')
246+
$myfixedName = $myfixedName.replace('!','_')
247+
$myfixedName = $myfixedName.replace('/','_')
248+
$myfixedName = $myfixedName.replace('%','_')
249+
$Outfilename = $COutput_path+"$myfixedName.xml"
250+
251+
Write-Output("Condition: {0}" -f $myfixedName)
252+
$xmlWriter = [System.Xml.XmlWriter]::Create($Outfilename)
253+
$Condition.Serialize($xmlWriter)
254+
$xmlWriter.Close()
255+
}
256+
}
257+
else
258+
{
259+
Write-Output "Could Not Connect to PolicyStore"
260+
}
261+
262+
263+
# Return to Base
264+
set-location $BaseFolder

0 commit comments

Comments
 (0)