Hello dear Powershell experts,
I need some advice. I try to use Powershell variables (which i get from parameters) to address XML paths/nodes.
My Scenario
The code below shows a short cutout of my code. With this cutout i will try to explain my issue as detailed as possible.
foreach ($Gpo in $AllGpos) {
[xml]$selected_Gpo = Get-GPOReport -ReportType Xml -Guid $Gpo.Id
$policy_object = $selected_Gpo.GPO.Computer.ExtensionData.Extension
if ($policy_object.$Policy_Identity_XMLPath_Name -eq $Policy_Identity_Name) {
if ($policy_object.$Setting_XMLPath_Name -eq $Policy_Identity_Setting_Name -and $policy_object.$Setting_XMLPath_Value -eq $Policy_Identity_Setting_Value) {
}
...
I iterate trough all Group Policy Objects (in an Active Directory Environment), I generate an xml Report of the GPO and then search for specific Group Policy settings in this xml file. This method is working perfectly fine as long as i use hardcoded xml node paths. For example: Instead of using "$policy_object.$Policy_Identity_XMLPath_Name -eq $Policy_Identity_Name" I could use "$policy_object.Policy.DropDownList.Name -eq "Example Setting". But I am unable to use these hardcoded xml node paths and wanted to use variables to address these paths in the xml file.
Short cutout of an example XML
<q1:Policy>
<q1:Name>TestName</q1:Name>
<q1:State>Enabled</q1:State>
<q1:Explain>Test Explaination </q1:Explain>
<q1:Supported>Testsupported version</q1:Supported>
<q1:Category>Test Category</q1:Category>
<q1:DropDownList>
<q1:Name>Test Name DropDownList</q1:Name>
<q1:State>Enabled</q1:State>
<q1:Value>
<q1:Name>TestName Value</q1:Name>
</q1:Value>
</q1:DropDownList>
</q1:Policy>
My Problem
The problem is, that I am unable to address the xml paths with powershell variables. When i use variables like in the code sample above ($policy_object.$Policy_Identity_XMLPath_Name) I get null back, for which reason my if statements do not work anymore.
What i did so far
Of course I did a lot trial and error things, to get powershell address these xml node paths with variables. I tried to put ($policy_object.$Policy_Identity_XMLPath_Name) in different types of quotes. I also already have seen the stack overflow post with a pretty similar problem (Parsing an XML file with PowerShell with node from variable) But those solutions are basically not that bad, but aren't useful in my concrete scenario. Let me explain:
$Policy_Identity_Setting_XMLPath_Name = 'GPO/Computer/ExtensionData/Extension/SecurityOptions/Display/DisplayFields/Field/Name'
$test = policy_object.SelectNodes($Policy_Identity_Setting_XMLPath_Name)
=> results in null. Don't know why honestly
$ExecutionContext.InvokeCommand.ExpandString("`$(`policy_object.$Policy_Identity_Setting_XMLPath_Name)")
This would result in an "acceptable" outcome. But the outcome is mostly multiple values (GPO Settings) in my scenario and it stores them all in one giant string. I am unable to access a specific value in this string afterwards. So this method is also not ideal, because I am unable to process the outcome further.
I hope you fellows have some good ideas. Maybe I'm making it more complicated than necessary in my brain logic. If you need any more information, let me know.