Private/Invoke-HPSSecedit.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
Function Invoke-HPSSecedit {

    <#
        .SYNOPSIS
        n/a

        .DESCRIPTION
        n/a

        .PARAMETER Arguments
        n/a

        .PARAMETER SeceditPath
        n/a

        .EXAMPLE
        Invoke-HPSSecEdit -Arguments "/configure /db ${tempDB} /cfg ${FilePath}"

        .INPUTS
        System.String

        .OUTPUTS
        System.Object

        .LINK
        https://hardening.thomas-illiet.fr/Private/Invoke-HPSSecedit/

        .LINK
        https://github.com/thomas-illiet/Hardening/blob/stable/Hardening/Private/Invoke-HPSSecedit.ps1

        .LINK
        https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process

        .LINK
        https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo

        .NOTES
        - File Name : Invoke-HPSSecedit.ps1
        - Author : Thomas ILLIET
    #>


    [CmdletBinding( HelpUri = "https://hardening.thomas-illiet.fr/Private/Invoke-HPSSecedit/" )]
    [OutputType( [System.Object] )]
    Param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [System.String]
        $Arguments,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [System.String]
        $SeceditPath = "C:\Windows\System32\Secedit.exe"
    )

    begin {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
    }

    process {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"

        if ( Test-Path -Path $SeceditPath ) {

            $StandardOut = @()

            Write-Verbose "[$($MyInvocation.MyCommand.Name)] Configure secedit process"
            $StartInfo = New-Object System.Diagnostics.ProcessStartInfo
            $StartInfo.FileName = $SeceditPath
            $StartInfo.Arguments = $Arguments
            $StartInfo.RedirectStandardOutput = $True
            $StartInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8
            $StartInfo.UseShellExecute = $False
            $Startinfo.CreateNoWindow = $True

            Write-Verbose "[$($MyInvocation.MyCommand.Name)] Start secedit process"
            $Process = New-Object System.Diagnostics.Process
            $Process.StartInfo = $StartInfo
            $Process.Start() | Out-Null

            # Add standard output in variable $StandardOut
            while ( $process.StandardOutput.Peek() -gt -1 ) {
                Write-Verbose "[$($MyInvocation.MyCommand.Name)] $($process.StandardOutput.ReadLine())"
                $StandardOut += $process.StandardOutput.ReadLine()
            }

            # Instructs the Process component to wait indefinitely for the associated process to exit.
            $Process.WaitForExit()

            # Gets the exit code of the process.
            if ( $Process.ExitCode -eq 0 ) {
                Write-Verbose "[$($MyInvocation.MyCommand.Name)] The operation completed successfully."
            }
            else {
                Write-Error "Incorrect function : $StandardOut"
            }

            # Return Secedit Output
            Return $StandardOut
        }
        else {
            Write-Error "[$($MyInvocation.MyCommand.Name)] Could not find the secedit executable : $SecEditPath"
        }
    }

    end {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
    }
}