【发布时间】:2021-03-25 22:36:32
【问题描述】:
我正在尝试使用 PowerShell 命令“Get-GPOReport”以 XML 字符串格式获取 GPO 信息,这样我就可以在其中搜索具有未知和不同元素标记名称的子元素值(我不认为 XML 对象格式对我有用,所以我没有使用“[xml]”执行强制转换),但我无法解析 XML 输出,以便我可以在所需的“名称”元素之后获取一两行与我正在搜索的文本匹配的行。
之后,我一直在尝试使用带有 XPath 的“Select-String”或“Select-XML”(格式不清楚,我不知道是否可以为各种策略记录位置使用格式)来匹配文本和抓住一个价值,但我没有运气。
另外,如果有人知道如何搜索 GPMC GUI 名称(即“强制密码历史记录”)而不是需要先找到后端等效名称来搜索(即“PasswordHistorySize”),那也会更有帮助.
以下初始代码是起作用的部分:
$String = "PasswordHistorySize" # This is an example string, as I will search for various strings eventually from a file, but I'm not sure if I could search for equivalent Group Policy GUI text "Enforce password history", if anyone knows how to do that.
$CurrentGPOReport = Get-GPOReport -Guid $GPO.Id -ReportType Xml -Domain $Domain -Server $NearestDC
If ($CurrentGPOReport -match $String)
{
Write-Host "Policy Found: ""$($String)""" -Foregroundcolor Green
#
#
# The following code is what I've tried to use to get value data, without any luck:
#
$ValueLine1 = $($CurrentGPOReport | Select-String -Pattern $String -Context 0,2)
$Value = $($Pattern = ">(.*?)</" ; [regex]::match($ValueLine1, $Pattern).Groups[1].Value)
}
【问题讨论】:
标签: powershell parsing search match group-policy