Good evening! I'm attempting to use PowerShell to read in an XML file and then output the selected nodes in a specific way. I'm nearly there, but after much research, I am stumped.
Please consider the following:
$xml = [xml]@'
<?xml version="1.0" encoding="utf-8"?>
<registryKeys>
<registryKey keyName="HKEY_CLASSES_ROOT\CLSID\TestKey1" owner="false">
<registryValue name="TestValueName1" valueType="REG_EXPAND_SZ" value="TestValue1" operationHint="replace" owner="true" />
<registryValue name="TestValueName2" valueType="REG_SZ" value="TestValue2" operationHint="replace" owner="true" />
</registryKey>
<registryKey keyName="HKEY_CLASSES_ROOT\CLSID\TestKey2" owner="false">
<registryValue name="TestValueName3" valueType="REG_EXPAND_SZ" value="TestValue3" operationHint="replace" owner="true" />
<registryValue name="TestValueName4" valueType="REG_SZ" value="TestValue4" operationHint="replace" owner="true" />
</registryKey>
</registryKeys>
'@
$regKeyNode=$xml.SelectNodes("/registryKeys/registryKey")
foreach ($registryKey in $regKeyNode) {
$keyname = $registrykey.keyName
$name = $registryKey.registryValue.name
$value = $registryKey.registryValue.value
$namevalue = $name+"="+$value
Write-Host "Write-Host Line: $keyname"
Write-Host "Write-Host Line: $namevalue"
}
Upon running the above code, I receive the following output:
Write-Host Line: HKEY_CLASSES_ROOT\CLSID\TestKey1
Write-Host Line: TestValueName1 TestValueName2 = TestValue1 TestValue2
Write-Host Line: HKEY_CLASSES_ROOT\CLSID\TestKey2
Write-Host Line: TestValueName3 TestValueName4 = TestValue3 TestValue4
This is not how I want the returned output to be formatted. I need it to rather return like this:
Write-Host Line: HKEY_CLASSES_ROOT\CLSID\TestKey1
Write-Host Line: TestValueName1 = TestValue1
Write-Host Line: TestValueName2 = TestValue2
Write-Host Line: HKEY_CLASSES_ROOT\CLSID\TestKey1
Write-Host Line: TestValueName3 = TestValue3
Write-Host Line: TestValueName4 = TestValue4
I realize I am probably over thinking this, but for the life of me I can't figure out what I'm doing wrong. Anyone able to simplify this?