I am trying to perform a search in the XML file in order to check if a specific group policy is linked to a few OUs.
The format of the OU is "OU=XXXXX-Name,OU=DEMO OU,dc=domain,dc=local"
I managed to get the full distinguishedname property of each OU and i kept only the first part of it
OU=XXXXX-Name,
discarding the rest and the "OU=" part so i am left with the display name which i need
I am a bit confused while struggling to create an "If condition" where i am using the
GPO-Report -XML
output to search and check the value in section and see if it matches with the target OU name so i can determine if the GPO is already linked to the specific OU
The XML file has the section below
<LinksTo>
<SOMName>XXXXX-Name</SOMName>
<SOMPath>domain.local/DEMO OUs</SOMPath>
<Enabled>true</Enabled>
<NoOverride>false</NoOverride>
</LinksTo>
Any help would be much appreciated cause i ve spend a good amount of hours circling around this issue, trying to figure out how regular expressions will help me achieve that.
PS I am not an expert in code but i try my best to get into it.
I have tried a few regular expression examples without any luck.
UPDATE
Apologies for the incomplete post (i am still new in here)
I think i managed to make it work by adding 2 lines of code. My code as follows:
Clear-Host
$gpoName = "TestGPO"
$oulist=(Get-Content C:\temp\ou.txt|foreach {
Get-ADOrganizationalUnit -Filter "name -like `"*$_*`"" -Properties distinguishedname|`
select -ExpandProperty distinguishedname}) -replace '^OU=|,.*$'
$xmlgpo=Get-GPO $gpoName |Get-GPOReport -ReportType XML
foreach ($item in $oulist){
if ($xmlgpo -match $item){
Write-Warning "GPO '$gponame' has a link already to '$item'"
}
else{
Write-Warning "No link to OU '$item' found"
}
}
CodePudding user response:
I would write code like this to create a Powershell Table.
using assembly System
using assembly System.Xml.Linq
$filename = "c:\temp\test.xml"
$xDoc = [System.Xml.Linq.XDocument]::Load($filename)
$links = $xDoc.Descendants("LinksTo").Foreach([System.Xml.Linq.XElement])
$pattern = '^(?<domain>[^.] )\.(?<host>[^/] )/(?<value>.*)'
$table = [System.Collections.ArrayList]::new()
foreach($link in $links)
{
$newRow = New-Object -TypeName psobject
$SOMName = $link.Element("SOMName").Value
$newRow | Add-Member -NotePropertyName SOMName -NotePropertyValue $SOMName
$SOMPath = $link.Element("SOMPath").Value
$matches = $SOMPath | Select-String -Pattern $pattern
$domain = $matches.Matches.groups[1].value
$newRow | Add-Member -NotePropertyName domain -NotePropertyValue $domain
$_host = $matches.Matches.groups[2].value
$newRow | Add-Member -NotePropertyName host -NotePropertyValue $_host
$value = $matches.Matches.groups[3].value
$newRow | Add-Member -NotePropertyName value -NotePropertyValue $value
$Enabled = $link.Element("Enabled").Value
$newRow | Add-Member -NotePropertyName Enabled -NotePropertyValue $Enabled
$NoOverride = $link.Element("NoOverride").Value
$newRow | Add-Member -NotePropertyName NoOverride -NotePropertyValue $NoOverride
$table.Add($newRow) | Out-Null
}
$table | Format-Table
CodePudding user response:
I decided to turn my question into an answer since i managed to ma ke the code work Thank you all for your time
Clear-Host
$gpoName = "TestGPO"
$oulist=(Get-Content C:\temp\ou.txt|foreach {
Get-ADOrganizationalUnit -Filter "name -like `"*$_*`"" -Properties distinguishedname|`
select -ExpandProperty distinguishedname}) -replace '^OU=|,.*$'
$xmlgpo=Get-GPO $gpoName |Get-GPOReport -ReportType XML
foreach ($item in $oulist){
if ($xmlgpo -match $item){
Write-Warning "GPO '$gponame' has a link already to '$item'"
}
else{
Write-Warning "No link to OU '$item' found"
}
}