I want to use Powershell to map buttons for my game. The game uses XML for these values. I need to change the value of <InputMapping>Service2</InputMapping>
The XML looks like this:
<JoystickButtons>
<ButtonName>Service 2</ButtonName>
<InputMapping>Service2</InputMapping>
</JoystickButtons>
Here is my script so far
[xml]$gameInfo = Get-Content $game.fullname
$xml = New-Object XML
$xml.Load("C:\PATH\$game")
$Buttons = $xml.SelectNodes("/JoystickButtons")
What should I do?
You're close, if you want to target the InputMapping
node then your XPath should be JoystickButtons/InputMapping
and from there you access the .InnerText
property and assign a new value to it. Lastly, .Save
using the path you used to load the XML or a new path if you decide so.
$xmlPath = "C:\PATH\$game"
$xml = [xml]::new()
$xml.PreserveWhitespace = $true
$xml.Load($xmlPath)
$xml.SelectSingleNode('JoystickButtons/InputMapping').InnerText = 'newValue'
$xml.Save($xmlPath)
If you want to target the InputMapping
node where specifically the text is equal to Service2
you can use the XPath:
$xml.SelectSingleNode("JoystickButtons/InputMapping[text()='Service2']")
And lastly, if there were multiple InputMapping
nodes and you want to update them all, you would use .SelectNodes
instead of .SelectSingleNode
and a loop would be required to update each node.