4

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?

1 Answer 1

4

This is the output for $regkeynode

keyName                    owner                      registryValue             
-------                    -----                      -------------            
HKEY_CLASSES_ROOT\CLSID... false                      {TestValueName1, TestV...
HKEY_CLASSES_ROOT\CLSID... false                      {TestValueName3, TestV...

We can see here that RegistryValue is an array and can hold multiple values so when you call $registrykey.registryvalue.name you are getting every name at the same time, if there are 10 names you are getting 10 names at once not each one individually.

One more ForEach should fix this though

First we go through each $registrykey to get the $keyname
Then each $registryvalue getting the $registryvalue.name and $registryvalue.value

Sample code for you to play with

foreach ($registryKey in $regKeyNode)
{
    $keyname = $registrykey.keyName
    Write-Host "Write-Host Line: $keyname"

    foreach ($registryValue in $registryKey.registryValue)
    {
        $name = $registryValue.Name
        $value = $registryValue.value
        $namevalue =  $name+"="+$value
        Write-Host "Write-Host Line: $namevalue"
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.