xmlpowershellloops

Updating value of XML element with Powershell


I have have to configure buttons for 200 games!! The button mapping is done via XML. It looks like this:

<GameProfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ProfileName>NAME</ProfileName>
<GameNameInternal>GAME NAME HERE</GameNameInternal>
<GameGenreInternal>Fighting</GameGenreInternal>
<GamePath>PATH GOES HERE</GamePath>
<TestMenuParameter/>
<TestMenuIsExecutable>false</TestMenuIsExecutable>
<ExtraParameters/>
<TestMenuExtraParameters/>
<IconName>Icons/TITLE.png</IconName>
<ResetHint>false</ResetHint>
<ConfigValues>
...
</ConfigValues>
<JoystickButtons>
<JoystickButtons>
<ButtonName>Test</ButtonName>
<XInputButton>
...
</XInputButton>
<InputMapping>Test</InputMapping>
<AnalogType>None</AnalogType>
<BindNameXi>Input Device 0 RightThumb</BindNameXi>
<HideWithDirectInput>false</HideWithDirectInput>
<HideWithXInput>false</HideWithXInput>
<HideWithRawInput>false</HideWithRawInput>
<HideWithKeyboardForAxis>false</HideWithKeyboardForAxis>
<HideWithoutKeyboardForAxis>false</HideWithoutKeyboardForAxis>
<HideWithRelativeAxis>false</HideWithRelativeAxis>
<HideWithoutRelativeAxis>false</HideWithoutRelativeAxis>
<HideWithUseDPadForGUN1Stick>false</HideWithUseDPadForGUN1Stick>
<HideWithoutUseDPadForGUN1Stick>false</HideWithoutUseDPadForGUN1Stick>
<HideWithUseDPadForGUN2Stick>false</HideWithUseDPadForGUN2Stick>
<HideWithoutUseDPadForGUN2Stick>false</HideWithoutUseDPadForGUN2Stick>
<HideWithUseAnalogAxisToAimGUN1>false</HideWithUseAnalogAxisToAimGUN1>
<HideWithoutUseAnalogAxisToAimGUN1>false</HideWithoutUseAnalogAxisToAimGUN1>
<HideWithUseAnalogAxisToAimGUN2>false</HideWithUseAnalogAxisToAimGUN2>
<HideWithoutUseAnalogAxisToAimGUN2>false</HideWithoutUseAnalogAxisToAimGUN2>
<HideWithoutProMode>false</HideWithoutProMode>
<HideWithProMode>false</HideWithProMode>
</JoystickButtons>

I made this script based on info I received in a previous post. It grabs ALL the game XML files, then cycles through each and prompts me to enter every button found. Everything works but the part where it takes my input "$Key" and changes the value of "" (if any).

    $games = Get-ChildItem C:\PATH\UserProfiles -Filter "*.xml"
$notMatch = "fly","drive","gun","shooter","Racing", "axis" #ignore these types of games

foreach($game in $games){
    [xml]$gameInfo = Get-Content $game.fullname
    if(($gameInfo.GameProfile.GameGenreInternal -notmatch ($notMatch -join '|')) -and ($gameInfo.GameProfile.GameNameInternal)){
        $xml = New-Object XML
        $xml.Load("C:\PATH\$game")
        $gameName = $xml.GameProfile.GameNameInternal
        $buttons = $xml.GameProfile.JoystickButtons.JoystickButtons
        Write-Host -ForegroundColor Yellow $gameName
        Write-Host -ForegroundColor Yellow "----------------------------------------"
        foreach($button in $buttons){
            $key = Read-Host "Map button" '"'$button.ButtonName'"'
            $key = $host.ui.rawui.readkey()
            $button.InputMapping = $key
        }
            write-host ""
    }
}

Everything works except updating the value of "InputMapping". This picture clearly shows the value of $Key and the error I am getting. enter image description here


Solution

  • You are trying to update the InputMapping attribute of each button, but the method you are using to set the attribute isn't quite right. Instead of using SetAttribute, you should directly set the value of the InputMapping element.

    $games = Get-ChildItem C:\PATH\UserProfiles -Filter "*.xml"
    $notMatch = "fly","drive","gun","shooter","Racing", "axis" #ignore these types of games
    
    foreach($game in $games){
        [xml]$gameInfo = Get-Content $game.fullname
        if(($gameInfo.GameProfile.GameGenreInternal -notmatch ($notMatch -join '|')) -and ($gameInfo.GameProfile.GameNameInternal)){
            $xml = New-Object XML
            $xml.Load("C:\PATH\$game")
            $gameName = $xml.GameProfile.GameNameInternal
            $buttons = $xml.GameProfile.JoystickButtons.JoystickButtons
            Write-Host -ForegroundColor Yellow $gameName
            Write-Host -ForegroundColor Yellow "----------------------------------------"
            foreach($button in $buttons){
                $key = Read-Host "Map button" '"'$button.ButtonName'"'
                $button.InputMapping = $key # Update the InputMapping element
            }
            $xml.Save("C:\PATH\$game") # Save the updated XML file
            write-host ""
        }
    }
    

    the InputMapping element is directly updated with the value of $key. Additionally, the updated XML file is saved back to its original location.

    This should work I believe.