Skip to content

Commit 50ecc8e

Browse files
authored
Merge pull request #292 from FonsecaSergio/master
Add EPM version 5.0 that works with SQL 2016, and new SQLServer Powershell module
2 parents bf2f22d + 93b3359 commit 50ecc8e

19 files changed

Lines changed: 12699 additions & 0 deletions

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,11 @@ samples/applications/iot-smart-grid/ConsoleClient/obj/Debug/TemporaryGeneratedFi
427427
samples/applications/iot-smart-grid/WinFormsClient/bin/Release/Client.exe.config
428428
samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/bin/Release/MultithreadedOrderInsert.exe.config
429429
samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/obj/Debug/MultithreadedInMemoryTableInsert.MultithreadedOrderInsertMain.resources
430+
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyEvaluationErrors.rdl.data
431+
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyEvaluationErrorDetails.rdl.data
432+
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyEvaluationDetails.rdl.data
433+
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDetails.rdl.data
434+
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDashboard.rdl.data
435+
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/bin/Debug
436+
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDashboard - Backup.rdl
437+
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDashboardFiltered.rdl.data

samples/features/epm-framework/5.0/0Setup Scripts/EPM_Create_Database_Objects.sql

Lines changed: 803 additions & 0 deletions
Large diffs are not rendered by default.

samples/features/epm-framework/5.0/0Setup Scripts/EPM_Upgrade_Database_Objects.sql

Lines changed: 679 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Evaluate specific Policies against a Server List
2+
# Uses the Invoke-PolicyEvaluation Cmdlet
3+
4+
#SAMPLE: #.\EPM_EnterpriseEvaluation_5.ps1 -ConfigurationGroup "DEV" -PolicyCategoryFilter "Name Pattern" –EvalMode “Check”
5+
6+
<#
7+
Run Powershell ISE as Admin
8+
9+
https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-ps-module
10+
11+
#https://www.powershellgallery.com/packages/PowerShellGet/
12+
Install-Module -Name PowerShellGet -Force
13+
14+
#https://www.powershellgallery.com/packages/SqlServer/
15+
Install-Module -Name SqlServer -Force -AllowClobber
16+
17+
#>
18+
19+
param([string]$ConfigurationGroup=$(Throw `
20+
"Parameter missing: -ConfigurationGroup ConfigGroup"),`
21+
[string]$PolicyCategoryFilter=$(Throw "Parameter missing: `
22+
-PolicyCategoryFilter Category"), `
23+
[string]$EvalMode=$(Throw "Parameter missing: -EvalMode EvalMode"))
24+
25+
Remove-Module SQLPS -Force -ErrorAction SilentlyContinue
26+
Import-Module SqlServer -DisableNameChecking -MinimumVersion "21.0.171.78"
27+
28+
# Parameter -ConfigurationGroup specifies the
29+
# Central Management Server group to evaluate
30+
# Parameter -PolicyCategoryFilter specifies the
31+
# category of policies to evaluate
32+
# Parameter -EvalMode accepts "Check" to report policy
33+
# results, "Configure" to reconfigure any violations
34+
35+
# Declare variables to define the central warehouse
36+
# in which to write the output, store the policies
37+
$CentralManagementServer = "Win2012"
38+
$HistoryDatabase = "MDW"
39+
# Define the location to write the results of the policy evaluation
40+
$ResultDir = "E:\Results\"
41+
# End of variables
42+
43+
#Function to insert policy evaluation results into SQL Server - table policy.PolicyHistory
44+
function PolicyHistoryInsert($sqlServerVariable, $sqlDatabaseVariable, $EvaluatedServer, $EvaluatedPolicy, $EvaluationResults)
45+
{
46+
&{
47+
$sqlQueryText = "INSERT INTO policy.PolicyHistory (EvaluatedServer, EvaluatedPolicy, EvaluationResults) VALUES(N'$EvaluatedServer', N'$EvaluatedPolicy', N'$EvaluationResults')"
48+
Invoke-Sqlcmd -ServerInstance $sqlServerVariable -Database $sqlDatabaseVariable -Query $sqlQueryText -ErrorAction Stop
49+
}
50+
trap
51+
{
52+
$ExceptionText = $_.Exception.Message -replace "'", ""
53+
}
54+
}
55+
56+
#Function to insert policy evaluation errors into SQL Server - table policy.EvaluationErrorHistory
57+
function PolicyErrorInsert($sqlServerVariable, $sqlDatabaseVariable, $EvaluatedServer, $EvaluatedPolicy, $EvaluationResultsEscape)
58+
{
59+
&{
60+
$sqlQueryText = "INSERT INTO policy.EvaluationErrorHistory (EvaluatedServer, EvaluatedPolicy, EvaluationResults) VALUES(N'$EvaluatedServer', N'$EvaluatedPolicy', N'$EvaluationResultsEscape')"
61+
Invoke-Sqlcmd -ServerInstance $sqlServerVariable -Database $sqlDatabaseVariable -Query $sqlQueryText -ErrorAction Stop
62+
}
63+
trap
64+
{
65+
$ExceptionText = $_.Exception.Message -replace "'", ""
66+
}
67+
}
68+
69+
#Function to delete files from this policy only
70+
function PolicyFileDelete($File)
71+
{
72+
# Delete evaluation files in the directory.
73+
Remove-Item -Path $File
74+
# ugly but moves on...
75+
trap
76+
{
77+
continue;
78+
}
79+
}
80+
81+
# Connection to the policy store
82+
$conn = new-object Microsoft.SQlServer.Management.Sdk.Sfc.SqlStoreConnection("server=$CentralManagementServer;Trusted_Connection=true");
83+
$PolicyStore = new-object Microsoft.SqlServer.Management.DMF.PolicyStore($conn);
84+
85+
# Create recordset of servers to evaluate
86+
$sconn = new-object System.Data.SqlClient.SqlConnection("server=$CentralManagementServer;Trusted_Connection=true");
87+
$q = "SELECT DISTINCT server_name FROM $HistoryDatabase.[policy].[pfn_ServerGroupInstances]('$ConfigurationGroup');"
88+
89+
$sconn.Open()
90+
$cmd = new-object System.Data.SqlClient.SqlCommand ($q, $sconn);
91+
$cmd.CommandTimeout = 0;
92+
$dr = $cmd.ExecuteReader();
93+
94+
# Loop through the servers and then loop through
95+
# the policies. For each server and policy,
96+
# call cmdlet to evaluate policy on server and delete xml file afterwards
97+
98+
while ($dr.Read()) {
99+
$ServerName = $dr.GetValue(0);
100+
foreach ($Policy in $PolicyStore.Policies)
101+
{
102+
if (($Policy.PolicyCategory -eq $PolicyCategoryFilter)-or ($PolicyCategoryFilter -eq ""))
103+
{
104+
&{
105+
$OutputFile = $ResultDir + ("{0}_{1}.xml" -f (Encode-SqlName $ServerName ), ($Policy.Name));
106+
Invoke-PolicyEvaluation -Policy $Policy -TargetServerName $ServerName -AdHocPolicyEvaluationMode $EvalMode -OutputXML > $OutputFile;
107+
$PolicyResult = Get-Content $OutputFile -encoding UTF8;
108+
$PolicyResult = $PolicyResult -replace "'", ""
109+
PolicyHistoryInsert $CentralManagementServer $HistoryDatabase $ServerName $Policy.Name $PolicyResult;
110+
$File = $ResultDir + ("*_{0}.xml" -f ($Policy.Name));
111+
PolicyFileDelete $File;
112+
}
113+
trap [Exception]
114+
{
115+
$File = $ResultDir + ("*_{0}.xml" -f ($Policy.Name));
116+
PolicyFileDelete $File;
117+
$ExceptionText = $_.Exception.Message -replace "'", ""
118+
$ExceptionMessage = $_.Exception.GetType().FullName + ", " + $ExceptionText
119+
PolicyErrorInsert $CentralManagementServer $HistoryDatabase $ServerName $Policy.Name $ExceptionMessage;
120+
continue;
121+
}
122+
}
123+
}
124+
}
125+
126+
$dr.Close()
127+
$sconn.Close()
128+
129+
#Shred the XML results to PolicyHistoryDetails
130+
Invoke-Sqlcmd -ServerInstance $CentralManagementServer -Database $HistoryDatabase -Query "EXEC policy.epm_LoadPolicyHistoryDetail `$(PolicyCategory)" -Variable "PolicyCategory='${PolicyCategoryFilter}'" -QueryTimeout 65535 -Verbose -ErrorAction Stop
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 10.00
3+
# Visual Studio 2008
4+
Project("{F14B399A-7131-4C87-9E4B-1186C45EF12D}") = "PolicyReports", "PolicyReports.rptproj", "{E0F65769-8BEA-4BFD-B715-44519AF1CF80}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Default = Debug|Default
9+
DebugLocal|Default = DebugLocal|Default
10+
Release|Default = Release|Default
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.Debug|Default.ActiveCfg = Debug
14+
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.Debug|Default.Build.0 = DebugLocal
15+
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.Debug|Default.Deploy.0 = DebugLocal
16+
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.DebugLocal|Default.ActiveCfg = DebugLocal
17+
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.DebugLocal|Default.Build.0 = DebugLocal
18+
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.DebugLocal|Default.Deploy.0 = DebugLocal
19+
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.Release|Default.ActiveCfg = Release
20+
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.Release|Default.Build.0 = Release
21+
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.Release|Default.Deploy.0 = Release
22+
EndGlobalSection
23+
GlobalSection(SolutionProperties) = preSolution
24+
HideSolutionNode = FALSE
25+
EndGlobalSection
26+
EndGlobal
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 11.00
3+
# Visual Studio 2010
4+
Project("{F14B399A-7131-4C87-9E4B-1186C45EF12D}") = "PolicyReports", "PolicyReports\PolicyReports.rptproj", "{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Default = Debug|Default
9+
DebugLocal|Default = DebugLocal|Default
10+
Release|Default = Release|Default
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.Debug|Default.ActiveCfg = Debug
14+
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.Debug|Default.Build.0 = DebugLocal
15+
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.Debug|Default.Deploy.0 = DebugLocal
16+
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.DebugLocal|Default.ActiveCfg = DebugLocal
17+
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.DebugLocal|Default.Build.0 = DebugLocal
18+
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.Release|Default.ActiveCfg = Release
19+
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.Release|Default.Build.0 = Release
20+
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.Release|Default.Deploy.0 = Release
21+
EndGlobalSection
22+
GlobalSection(SolutionProperties) = preSolution
23+
HideSolutionNode = FALSE
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RptDataSource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="PolicyDW">
3+
<ConnectionProperties>
4+
<Extension>SQL</Extension>
5+
<ConnectString>Data Source=localhost;Initial Catalog=EPM</ConnectString>
6+
<IntegratedSecurity>true</IntegratedSecurity>
7+
</ConnectionProperties>
8+
<DataSourceID>be53e7b6-9fc8-452b-ac5e-151bec780bf7</DataSourceID>
9+
</RptDataSource>

0 commit comments

Comments
 (0)