【发布时间】:2021-11-01 19:10:52
【问题描述】:
我正在编写一个 powershell 脚本来编辑从 registry.pol 生成的文本文件。最终目标是达到 CIS L1 基准。为了使 CIS L1 能够如 this link 中所述在 AWS EC2-image-builder 中工作,需要删除以下部分:
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
到目前为止,这是我将所有内容但通过管道传输到输出文件的代码:
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
我试图弄清楚如何选择第一行是“计算机”的某些部分,但不是所有第一行是“计算机”的部分,前提是“计算机”后面的行也匹配需要删除的内容:
-
在一行中检查“计算机”
-
将当前行和接下来的 3 行存储为一个部分
-
检查该部分的唯一注册表设置(即 fPromptForPassword)
-
重复第 2 部分(从“计算机”开始)
-
重复第 3 节“”
-
将剩余(未选择的)输出通过管道传输到 MS-L1.txt
我相信使用 -context 检查“计算机”后面的 3 行是可行的,但我对嵌套 if 语句的格式没有信心,谢谢您的指导!
【问题讨论】:
标签: powershell windows-server-2019