Skip to content

Instantly share code, notes, and snippets.

@RichieBzzzt
Last active September 1, 2017 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichieBzzzt/f27e7837f009676a89f896dbb7bc70df to your computer and use it in GitHub Desktop.
Save RichieBzzzt/f27e7837f009676a89f896dbb7bc70df to your computer and use it in GitHub Desktop.
[xml] $x = '<Lunch>
<Sandwiches>
<Sandwich Include="Mixte">
<Filling>Ham and Emmantal Cheese</Filling>
</Sandwich>
<Sandwich Include="RoteDeBoeuf">
<Filling>Beef and Mustard Mayonnaise</Filling>
</Sandwich>
</Sandwiches>
</Lunch>'
$Mixte = "Fromage au jambon et Emmantal"
$RoteDeBoeuf = "Mayonnaise au boeuf et à la moutarde"
$keys = $($x.Lunch.Sandwiches.Sandwich)
foreach ($var in $keys) {
$update = $var.Include
if (Test-Path variable:$update) {
[string]$value = Get-Variable $update -ValueOnly
Write-Verbose ('Setting variable: {0} = {1}' -f $update, $value) -Verbose
$element = $x.SelectNodes("/Lunch/Sandwiches/Sandwich") | Where-Object {$_.Include -eq $update}
$element.Filling = $value
}
else {
$missingVariables += $var
}
}
if ($missingVariables.Count -gt 0) {
throw ('The following variables are not defined in the current scope (but are defined in the xml): {0}' -f ($missingVariables -join " `n"))
}
$x.Lunch.Sandwiches.Sandwich
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment