I am working on a powershell script to edit a text file generated from registry.pol. The end goal is achieving CIS L1 benchmark. In order for CIS L1 to work in AWS EC2-image-builder as described in this link the following sections need removed:
Computer
Software\Policies\Microsoft\Windows NT\Terminal Services
fPromptForPassword
DWORD:1
Computer
Software\Policies\Microsoft\WindowsFirewall\PublicProfile
AllowLocalPolicyMerge
DWORD:0
Computer
Software\Policies\Microsoft\WindowsFirewall\PublicProfile
AllowLocalIPsecPolicyMerge
DWORD:0
Here is my code so far to pipe everything but these 3 sections to output file:
Get-Content -Path C:\MS-L1.txt -Raw | where -notmatch {
if( $_.StartsWith("Computer") ) {
$section = select-string -pattern Computer -context 0,3
if( ($section -like "fPromptForPassword") and ($section -like "AllowLocalPolicyMerge") and ($section -like "AllowLocalIPsecPolicyMerge") ){
}
} | Out-File c:\MS-L1.txt
I am trying to figure out how to select certain sections whose first line is "Computer" but not all the sections whose first line is "Computer", only if the lines following "computer" also match what needs removed:
check for "computer" in a line
store current line and next 3 lines as a section
check the section for the unique registry setting (i.e. fPromptForPassword)
repeat for section 2 (starting at "computer")
repeat for section 3 " "
Pipe the remaining(unselected) output to MS-L1.txt
I believe using -context to examine the 3 lines following "computer" is feasible but am not confident in the formatting of my nested if statement, thank you for any guidance !