lundi 16 mars 2015

PowerShell XML: Switch vs if

Doing a simple find/replace with an XML file via PowerShell. I am getting errors when attempting to replace text in a node but only when using a switch...using a general if statement works perfectly.


Code starts thusly:



$oldString = 'AAAA'
$newString = 'BBBB'
[xml]$xml = Get-Content $myXMLFile
$list = Select-Xml -Xml $xml -XPath '//add'
$list|Foreach-Object {
[string]$key = $_.node.key
[string]$value = $_.node.value


The plain old IF version works as I would expect...



if($value -eq $oldString)
{
$_.node.value = $newString
}
}|Set-Content $myXMLFile


But replacing the IF with a switch statement throws an error.



switch($value)
{
$oldString{$_.node.value = $newString}
}
}|Set-Content $myXMLFile

Property 'value' cannot be found on this object; make sure it exists and is settable.
+ $oldString{$_.node.value = $newString}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound


I have a lot of node data to replace and I would rather not write a bunch of IF's if I can get away with a switch.


Aucun commentaire:

Enregistrer un commentaire