【发布时间】:2018-07-31 19:14:29
【问题描述】:
抱歉,帖子太长了。想详细解释一下。 我正在努力实现三件事,而且非常接近。可能是小学生的错误。尝试了嵌套循环等,但无法正常工作。
看来我需要拆分$resultszone 数组。
- 在文件中搜索特定区域。在下面的示例中,它是
\zones\、test1.in-addr.arpa、test2.in-addr.arpa 等之后的部分。 - 在找到区域后复制和修剪内容。在第一个示例中,只需 test1.in-addr.arpa(删除开头的“\”和结尾的“]”
- 在包含“类型”的行下方添加一行,包括找到的区域(例如 test1.in-addr.arpa)。
示例源文件:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DNS 服务器\Zones\test1.in-addr.arpa] “类型”=dword:00000001 "SecureSecondaries"=dword:00000002 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DNS 服务器\Zones\test2.in-addr.arpa] “类型”=dword:00000001 "SecureSecondaries"=dword:00000002预期结果
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DNS 服务器\Zones\test1.in-addr.arpa] “类型”=dword:00000001 "DatabaseFile"="test1.in-addr.arpa.dns" "SecureSecondaries"=dword:00000002 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DNS 服务器\Zones\test2.in-addr.arpa] “类型”=dword:00000001 "DatabaseFile"="test2.in-addr.arpa.dns" "SecureSecondaries"=dword:00000002我已经设法使用下面的代码实现了所有目标,除了它为每个部分添加了一行,包括来自找到的区域的所有结果。
例如:
"DatabaseFile"="test1.in-addr.arpa test2.in-addr.arpa#Get FileName Path
$FileName = "C:\temp\test.conf"
#Search for pattern in file and trim to desired format.
#Store array in $resultsZone
$resultszone = Select-String -Path "c:\temp\test.conf" -Pattern '(?<=Zones)(.*)' |
select -expa matches |
select -expa value |
% { $_.Trim("\]") }
# Get contents of file
(Get-Content $FileName) | ForEach-Object {
#Start Loop to find area of File to insert line
$_ # send the current line to output
if ($_ -match "type") {
#Add Line after the selected pattern (type) including area trimmed
"""DatabaseFile" + """=""" + $resultszone + ".dns" + """"
}
} | Set-Content C:\temp\elctest.conf
【问题讨论】:
-
注册表文件使用 INI 格式,所以我建议这样解析。请使用页面顶部的搜索框。以前也有人问过类似的问题。
-
仅用作第 3 方解决方案的输入。感谢您的意见。
标签: powershell