Outputs/output-csv.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
using module "..\ORCA.psm1"

class csv : ORCAOutput
{

    $OutputDirectory=$null

    csv()
    {
        $this.Name="CSV"
    }

    RunOutput($Checks,$Collection)
    {

        # Write to file

        if($null -eq $this.OutputDirectory)
        {
            $OutputDir = $this.DefaultOutputDirectory
        }
        else 
        {
            $OutputDir = $this.OutputDirectory
        }

        $ResultOverview = @()
        $ResultDetail = @()

        # Parse to flatten the output, for the overview some data is going to get lost because it's not a flat structure
        ForEach($c in $Checks)
        {

            $ResultOverview += New-Object -TypeName PSObject -Property @{
                Control=$c.Control
                Area=$c.Area
                Name=$c.Name
                Result=$($c.Result.ToString())
                ObjectsFailed=$c.FailCount
                ObjectsPassed=$c.PassCount
                ObjectsInfo=$c.InfoCount
            }

            ForEach($config in $c.Config)
            {
                $ResultDetail += New-Object -TypeName PSObject -Property @{
                    Control=$c.Control
                    Area=$c.Area
                    Name=$c.Name
                    ConfigObject=$config.Object
                    ConfigItem=$config.ConfigItem
                    ConfigData=$config.ConfigData
                    Level=$($config.Level.ToString())
                }
            }
        }

        $Tenant = $(($Collection["AcceptedDomains"] | Where-Object {$_.InitialDomain -eq $True}).DomainName -split '\.')[0]
        $ReportFileNameOverview = "ORCA-$($tenant)-$(Get-Date -Format 'yyyyMMddHHmm').csv"
        $ReportFileNameDetail = "ORCA-$($tenant)-$(Get-Date -Format 'yyyyMMddHHmm')-Detail.csv"

        $OverviewFile = "$OutputDir\$ReportFileNameOverview"
        $DetailFile = "$OutputDir\$ReportFileNameDetail"

        $ResultOverview | Select-Object Control,Area,Name,Result,ObjectsFailed,ObjectsPassed,ObjectsInfo,Text | Export-Csv $OverviewFile -NoTypeInformation
        $ResultDetail | Select-Object Control,Area,Name,ConfigObject,ConfigItem,ConfigData,Level | Export-Csv $DetailFile -NoTypeInformation

        $this.Completed = $True
        $this.Result = New-Object -TypeName PSObject -Property @{
            Overview=$OverviewFile
            Detail=$DetailFile
        }

    }

}