Common/helpers.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# =================================================================================================================
# Purpose:
# Revisions:
# 06282018 - Matt Preston, Microsoft - Release 1
# =================================================================================================================
# -----------------------------------------------------------------------------
#
# Copyright (C) 2018 Microsoft Corporation
#
# Disclaimer:
# This is SAMPLE code that is NOT production ready. It is the sole intention of this code to provide a proof of concept as a
# learning tool for Microsoft Customers. Microsoft does not provide warranty for or guarantee any portion of this code
# and is NOT responsible for any affects it may have on any system it is executed on or environment it resides within.
# Please use this code at your own discretion!
# Additional legalese:
# This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment.
# THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
# We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute
# the object code form of the Sample Code, provided that You agree:
# (i) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded;
# (ii) to include a valid copyright notice on Your software product in which the Sample Code is embedded; and
# (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorneys' fees,
# that arise or result from the use or distribution of the Sample Code.
# -----------------------------------------------------------------------------

<#
Functions:
    Private:
        H01 - Convert-PowerStigRoleToSql
        H02 - Import-PowerStigConfig
        H03 - Invoke-PowerStigSqlCommand
#>

#H01
function Convert-PowerStigRoleToSql
{
    [cmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [String]$Role
    )


    switch($Role)
    {
        "DotNetFramework"                       {Return $Role}
        "FireFox"                               {Return $Role}
        "IISServer"                             {Return $Role}
        "IISSite"                               {Return $Role}
        "InternetExplorer"                      {Return $Role}
        "Excel2013"                             {Return $Role}
        "Outlook2013"                           {Return $Role}
        "PowerPoint2013"                        {Return $Role}
        "Word2013"                              {Return $Role}
        "OracleJRE"                             {Return $Role}
        "SqlServer-2012-Database"               {Return "SqlServer2012Database"}
        "SqlServer-2012-Instance"               {Return "SqlServer2012Instance"}
        "SqlServer-2016-Instance"               {Return "SqlServer2016Instance"}
        "WindowsClient"                         {Return $Role}
        "WindowsDefender"                       {Return $Role}
        "WindowsDNSServer"                      {Return $Role}
        "WindowsFirewall"                       {Return $Role}
        "WindowsServer-DC"                      {Return "WindowsServerDC"}
        "WindowsServer-MS"                      {Return "WindowsServerMS"}
        "GoogleChrome"                          {Return $Role}
        "AdobeAcrobatReaderDCCont"              {Return $Role}
        "AdobeAcrobatReaderDCClassic"           {Return $Role}
    }
    
}


#H02
<#
.SYNOPSIS
Retrieves configuration data from a standard .ini file and returns it as a hashtable

.DESCRIPTION
Will cycle through each line of a standard .ini and store each configuration pair as a value/key pair in a hashtable

.PARAMETER configFilePath
Path to the .ini file to be put to a variable

.EXAMPLE
Import-PowerStigConfig -configFilePath C:\users\test.user\documents\config.ini
#>

function Import-PowerStigConfig 
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)]
        [ValidateNotNullorEmpty()]
        [String]$configFilePath
    )

    $configDataText = Get-Content $configFilePath
    $variables = @{}

    # Cycle through each part of the config.ini file
    foreach($c in $configDataText)
    {
        # Split String at the "=", Left is config name, right is config setting, ignore lines with "[" and ";"
        $splitVar = [regex]::split($c,'=')
        if(($splitVar[0].CompareTo("") -ne 0) -and ($splitVar[0].StartsWith("[") -ne $True) -and ($splitVar[0].StartsWith(";") -ne $True))
        { 
            $variables.Add($splitVar[0].trim(), $splitVar[1].trim()) | out-null
        } # End if
    } # End foreach

    # Return hashtable of config data
    Return $variables
} # End Import-PowerStigConfig

#H03
function Invoke-PowerStigSqlCommand
{
    [cmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [ValidateNotNullorEmpty()]
        [String]$Query,

        [Parameter(Mandatory=$false)]
        [String]$SqlInstance,

        [Parameter(Mandatory=$false)]
        [String]$DatabaseName
    )

    $workingPath = Split-Path $PsCommandPath
    $iniVar = Import-PowerStigConfig -configFilePath $workingPath\Config.ini

    if($SqlInstance -eq $null -or $SqlInstance -eq '')
    {
        $SqlInstance = $iniVar.SqlInstanceName
    }
    if($DatabaseName -eq $null -or $DatabaseName -eq '')
    {
        $DatabaseName = $iniVar.DatabaseName
    }

    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = "Server=$SqlInstance;Database=$DatabaseName;Integrated Security=True"
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.CommandText = $Query
    $SqlCmd.Connection = $SqlConnection
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet) | Out-Null
    $SqlConnection.Close()
    
    return $DataSet.Tables[0]
}