Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ samples/applications/iot-smart-grid/ConsoleClient/obj/Debug/TemporaryGeneratedFi
samples/applications/iot-smart-grid/WinFormsClient/bin/Release/Client.exe.config
samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/bin/Release/MultithreadedOrderInsert.exe.config
samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/obj/Debug/MultithreadedInMemoryTableInsert.MultithreadedOrderInsertMain.resources
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyEvaluationErrors.rdl.data
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyEvaluationErrorDetails.rdl.data
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyEvaluationDetails.rdl.data
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDetails.rdl.data
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDashboard.rdl.data
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/bin/Debug
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDashboard - Backup.rdl
/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDashboardFiltered.rdl.data

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Evaluate specific Policies against a Server List
# Uses the Invoke-PolicyEvaluation Cmdlet

#SAMPLE: #.\EPM_EnterpriseEvaluation_5.ps1 -ConfigurationGroup "DEV" -PolicyCategoryFilter "Name Pattern" �EvalMode �Check�

<#
Run Powershell ISE as Admin

https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-ps-module

#https://www.powershellgallery.com/packages/PowerShellGet/
Install-Module -Name PowerShellGet -Force

#https://www.powershellgallery.com/packages/SqlServer/
Install-Module -Name SqlServer -Force -AllowClobber

#>

param([string]$ConfigurationGroup=$(Throw `
"Parameter missing: -ConfigurationGroup ConfigGroup"),`
[string]$PolicyCategoryFilter=$(Throw "Parameter missing: `
-PolicyCategoryFilter Category"), `
[string]$EvalMode=$(Throw "Parameter missing: -EvalMode EvalMode"))

Remove-Module SQLPS -Force -ErrorAction SilentlyContinue
Import-Module SqlServer -DisableNameChecking -MinimumVersion "21.0.171.78"

# Parameter -ConfigurationGroup specifies the
# Central Management Server group to evaluate
# Parameter -PolicyCategoryFilter specifies the
# category of policies to evaluate
# Parameter -EvalMode accepts "Check" to report policy
# results, "Configure" to reconfigure any violations

# Declare variables to define the central warehouse
# in which to write the output, store the policies
$CentralManagementServer = "Win2012"
$HistoryDatabase = "MDW"
# Define the location to write the results of the policy evaluation
$ResultDir = "E:\Results\"
# End of variables

#Function to insert policy evaluation results into SQL Server - table policy.PolicyHistory
function PolicyHistoryInsert($sqlServerVariable, $sqlDatabaseVariable, $EvaluatedServer, $EvaluatedPolicy, $EvaluationResults)
{
&{
$sqlQueryText = "INSERT INTO policy.PolicyHistory (EvaluatedServer, EvaluatedPolicy, EvaluationResults) VALUES(N'$EvaluatedServer', N'$EvaluatedPolicy', N'$EvaluationResults')"
Invoke-Sqlcmd -ServerInstance $sqlServerVariable -Database $sqlDatabaseVariable -Query $sqlQueryText -ErrorAction Stop
}
trap
{
$ExceptionText = $_.Exception.Message -replace "'", ""
}
}

#Function to insert policy evaluation errors into SQL Server - table policy.EvaluationErrorHistory
function PolicyErrorInsert($sqlServerVariable, $sqlDatabaseVariable, $EvaluatedServer, $EvaluatedPolicy, $EvaluationResultsEscape)
{
&{
$sqlQueryText = "INSERT INTO policy.EvaluationErrorHistory (EvaluatedServer, EvaluatedPolicy, EvaluationResults) VALUES(N'$EvaluatedServer', N'$EvaluatedPolicy', N'$EvaluationResultsEscape')"
Invoke-Sqlcmd -ServerInstance $sqlServerVariable -Database $sqlDatabaseVariable -Query $sqlQueryText -ErrorAction Stop
}
trap
{
$ExceptionText = $_.Exception.Message -replace "'", ""
}
}

#Function to delete files from this policy only
function PolicyFileDelete($File)
{
# Delete evaluation files in the directory.
Remove-Item -Path $File
# ugly but moves on...
trap
{
continue;
}
}

# Connection to the policy store
$conn = new-object Microsoft.SQlServer.Management.Sdk.Sfc.SqlStoreConnection("server=$CentralManagementServer;Trusted_Connection=true");
$PolicyStore = new-object Microsoft.SqlServer.Management.DMF.PolicyStore($conn);

# Create recordset of servers to evaluate
$sconn = new-object System.Data.SqlClient.SqlConnection("server=$CentralManagementServer;Trusted_Connection=true");
$q = "SELECT DISTINCT server_name FROM $HistoryDatabase.[policy].[pfn_ServerGroupInstances]('$ConfigurationGroup');"

$sconn.Open()
$cmd = new-object System.Data.SqlClient.SqlCommand ($q, $sconn);
$cmd.CommandTimeout = 0;
$dr = $cmd.ExecuteReader();

# Loop through the servers and then loop through
# the policies. For each server and policy,
# call cmdlet to evaluate policy on server and delete xml file afterwards

while ($dr.Read()) {
$ServerName = $dr.GetValue(0);
foreach ($Policy in $PolicyStore.Policies)
{
if (($Policy.PolicyCategory -eq $PolicyCategoryFilter)-or ($PolicyCategoryFilter -eq ""))
{
&{
$OutputFile = $ResultDir + ("{0}_{1}.xml" -f (Encode-SqlName $ServerName ), ($Policy.Name));
Invoke-PolicyEvaluation -Policy $Policy -TargetServerName $ServerName -AdHocPolicyEvaluationMode $EvalMode -OutputXML > $OutputFile;
$PolicyResult = Get-Content $OutputFile -encoding UTF8;
$PolicyResult = $PolicyResult -replace "'", ""
PolicyHistoryInsert $CentralManagementServer $HistoryDatabase $ServerName $Policy.Name $PolicyResult;
$File = $ResultDir + ("*_{0}.xml" -f ($Policy.Name));
PolicyFileDelete $File;
}
trap [Exception]
{
$File = $ResultDir + ("*_{0}.xml" -f ($Policy.Name));
PolicyFileDelete $File;
$ExceptionText = $_.Exception.Message -replace "'", ""
$ExceptionMessage = $_.Exception.GetType().FullName + ", " + $ExceptionText
PolicyErrorInsert $CentralManagementServer $HistoryDatabase $ServerName $Policy.Name $ExceptionMessage;
continue;
}
}
}
}

$dr.Close()
$sconn.Close()

#Shred the XML results to PolicyHistoryDetails
Invoke-Sqlcmd -ServerInstance $CentralManagementServer -Database $HistoryDatabase -Query "EXEC policy.epm_LoadPolicyHistoryDetail `$(PolicyCategory)" -Variable "PolicyCategory='${PolicyCategoryFilter}'" -QueryTimeout 65535 -Verbose -ErrorAction Stop
26 changes: 26 additions & 0 deletions samples/features/epm-framework/5.0/2Reporting/PolicyReports.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{F14B399A-7131-4C87-9E4B-1186C45EF12D}") = "PolicyReports", "PolicyReports.rptproj", "{E0F65769-8BEA-4BFD-B715-44519AF1CF80}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Default = Debug|Default
DebugLocal|Default = DebugLocal|Default
Release|Default = Release|Default
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.Debug|Default.ActiveCfg = Debug
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.Debug|Default.Build.0 = DebugLocal
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.Debug|Default.Deploy.0 = DebugLocal
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.DebugLocal|Default.ActiveCfg = DebugLocal
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.DebugLocal|Default.Build.0 = DebugLocal
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.DebugLocal|Default.Deploy.0 = DebugLocal
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.Release|Default.ActiveCfg = Release
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.Release|Default.Build.0 = Release
{E0F65769-8BEA-4BFD-B715-44519AF1CF80}.Release|Default.Deploy.0 = Release
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{F14B399A-7131-4C87-9E4B-1186C45EF12D}") = "PolicyReports", "PolicyReports\PolicyReports.rptproj", "{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Default = Debug|Default
DebugLocal|Default = DebugLocal|Default
Release|Default = Release|Default
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.Debug|Default.ActiveCfg = Debug
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.Debug|Default.Build.0 = DebugLocal
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.Debug|Default.Deploy.0 = DebugLocal
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.DebugLocal|Default.ActiveCfg = DebugLocal
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.DebugLocal|Default.Build.0 = DebugLocal
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.Release|Default.ActiveCfg = Release
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.Release|Default.Build.0 = Release
{30FA2CD3-F4E3-4EA6-85A7-07AF4D922933}.Release|Default.Deploy.0 = Release
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RptDataSource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="PolicyDW">
<ConnectionProperties>
<Extension>SQL</Extension>
<ConnectString>Data Source=localhost;Initial Catalog=EPM</ConnectString>
<IntegratedSecurity>true</IntegratedSecurity>
</ConnectionProperties>
<DataSourceID>be53e7b6-9fc8-452b-ac5e-151bec780bf7</DataSourceID>
</RptDataSource>
Loading