functions/Get-DSCEAreport.ps1

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
function Get-DSCEAreport {
<#
.SYNOPSIS
Generates a HTML based report after Start-DSCEAscan has been run

.DESCRIPTION
Used to create viewable reports after Start-DSCEAscan has created a results.xml file which will be parsed.

.PARAMETER ItemName
Switch parameter - This is the item name from the configuration file, used to generate a report of every machine's compliance against that item

.PARAMETER ComputerName
Switch parameter - Supplying a computer name will cause the report to display all items (true/false) pertaining to ComputerName

.PARAMETER Overall
Switch parameter - Indicating that the report generated will display all up true/false per computer in regards to compliance against the entire configuration file

.PARAMETER Detailed
Switch parameter - Indicating that the report generated will display all non-compliant configuration file items detected, grouped by computer

.PARAMETER InFile
The file name (full file path) of the XML file you would like to convert. If one is not provided, Get-DSCEAreport looks to the current directory for the most recently created XML file.

.PARAMETER OutPath
The full file path to use as a location to store HTML reports. The file path must be a pre-existing folder. If one is not provided, Get-DSCEAreport will create the HTML file in the current directory.

.LINK
https://microsoft.github.io/DSCEA

.EXAMPLE
Get-DSCEAreport -ItemName MicrosoftAntimalwareService

Description
-----------
This command returns all systems that were scanned and their compliance in regards to the Microsoft AntiMalware Service

.EXAMPLE
Get-DSCEAreport -ComputerName 'dc1'

Description
-----------
This command returns all configuration items for computer 'dc1'

.EXAMPLE
Get-DSCEAreport -Overall

Description
-----------
This command returns true/false per machine regarding whether they comply with the entire configuration file

.EXAMPLE
Get-DSCEAreport -Detailed

Description
-----------
This command returns non-compliant configuration file items detected, grouped by computer
#>

    [CmdLetBinding()]
    param
    (
        [parameter(Mandatory=$true,ParameterSetName='ItemName')]
        [String]$ItemName,

        [parameter(Mandatory=$true,ParameterSetName='ComputerName')]
        [String]$ComputerName,

        [parameter(Mandatory=$true,ParameterSetName='Overall')]
        [switch]$Overall,

        [parameter(Mandatory=$true,ParameterSetName='Detailed')]
        [switch]$Detailed,

        [String]$InFile = (Get-ChildItem .\results*.xml | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1).FullName,

        [String]$OutPath = '.'
    )
    $env:PSModulePath -split ';' | ForEach-Object {
        if(Test-Path (Join-Path -Path $_ -ChildPath 'DSCEA')) {
            if (!(Test-Path -Path 'C:\ProgramData\DSCEA')) {
                New-Item 'C:\ProgramData\DSCEA' -Type Directory
            }
            Copy-Item -Path (Get-ChildItem -Path $_ -Recurse -Filter 'logo.png').FullName -Destination 'C:\ProgramData\DSCEA\logo.png' -Force
        }
    }
    $results = Import-Clixml $InFile
    $date = (Get-ChildItem $InFile).LastWriteTime
    if($Overall) {
        $results | 
        select-object -ExpandProperty Compliance | Where-Object {$_.PSComputerName -ne $null} |
        select-object @{Name="Computer";Expression={$_.PSComputerName}}, @{Name="Compliant";Expression={$_.InDesiredState}} |
        ConvertTo-HTML -Head $webstyle -body "<img src='C:\ProgramData\DSCEA\logo.png'/><br>","<titlesection>DSC Configuration Report</titlesection><br>","<datesection>Report last run on",$date,"</datesection><p>" | 
        Out-File (Join-Path -Path $OutPath -ChildPath 'OverallComplianceReport.html')
        Get-ItemProperty (Join-Path -Path $OutPath -ChildPath 'OverallComplianceReport.html')
    }
    if($Detailed) {
        $results | ForEach-Object {
            $_.Compliance | ForEach-Object {
                $_.ResourcesNotInDesiredState | 
                Select-Object @{Name="Computer";Expression={$_.PSComputerName}}, ResourceName, InstanceName, InDesiredState
            }
        } | ConvertTo-HTML -Head $webstyle -body "<img src='C:\ProgramData\DSCEA\logo.png'/><br>","<titlesection>DSC Configuration Report</titlesection><br>","<datesection>Report last run on",$date,"</datesection><p>" | 
        Out-File (Join-Path -Path $OutPath -ChildPath 'DetailedComplianceReport.html')
        Get-ItemProperty (Join-Path -Path $OutPath -ChildPath 'DetailedComplianceReport.html')
    }
    if($ItemName) {
        $results | ForEach-Object {
            $_.Compliance | ForEach-Object {
                $_.ResourcesInDesiredState | ForEach-Object {$_ | Select-Object @{Name="Computer";Expression={$_.PSComputerName}}, ResourceName, InstanceName, InDesiredState}
                $_.ResourcesNotInDesiredState | ForEach-Object {$_ | Select-Object @{Name="Computer";Expression={$_.PSComputerName}}, ResourceName, InstanceName, InDesiredState}
            }
        } | Where-object {$_.InstanceName -ieq $ItemName} | 
        ConvertTo-HTML -Head $webstyle -body "<img src='C:\ProgramData\DSCEA\logo.png'/><br>","<titlesection>DSC Configuration Report</titlesection><br>","<datesection>Report last run on",$date,"</datesection><p>" | 
        Out-File (Join-Path -Path $OutPath -ChildPath "ItemComplianceReport-$ItemName.html")
        Get-ItemProperty (Join-Path -Path $OutPath -ChildPath "ItemComplianceReport-$ItemName.html")
    }
    if($ComputerName) {
        $results | where-object {$_.Computer -ieq $ComputerName} | ForEach-Object {
            $_.Compliance | ForEach-Object {
                $_.ResourcesNotInDesiredState | Select-Object @{Name="Computer";Expression={$_.PSComputerName}}, ResourceName, InstanceName, InDesiredState
                $_.ResourcesInDesiredState | Select-Object @{Name="Computer";Expression={$_.PSComputerName}}, ResourceName, InstanceName, InDesiredState
            }
        } | ConvertTo-HTML -Head $webstyle -body "<img src='C:\ProgramData\DSCEA\logo.png'/><br>","<titlesection>DSC Configuration Report</titlesection><br>","<datesection>Report last run on",$date,"</datesection><p>" | 
        Out-File (Join-Path -Path $OutPath -ChildPath "ComputerComplianceReport-$ComputerName.html")
        Get-ItemProperty (Join-Path -Path $OutPath -ChildPath "ComputerComplianceReport-$ComputerName.html")
    }
}