Édition des attributs XML à L'aide de Powershell

donc j'ai un .EXE.fichier de configuration que j'essaie de rechercher un attribut spécifique à l'intérieur, puis de l'éditer en utilisant la version 4.0 de Powershell dans Windows 7, et j'ai des problèmes. J'ai essayé plusieurs choses, et je n'ai aucun succès. Voici une version réduite du fichier de configuration que j'utilise.

<configuration>
  <Config1>
    <section name="text" type="text, text, text=text" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" requirePermission="true" />
  </Config1>
  <Config2>
    <module debugLogLevel="Debug" Version="1.0.0.0" />
    <Interested-Item attribute-1="text-text" attribute2="0">
    </Interested-Item>
    <modules>
      <add name="something1" />
      <add name="something2" />
      <add name="something3" />
      <add name="something4" />
      <add name="something5" />
      <add name="something6" />
      <add name="something7" />
    </modules>
  </Config2>        
</configuration>

Comment pourrais-je changer l'attribut-1 sous Interested-Item, en utilisant Powershell? Toute aide serait grandement appréciée.

Voici quelques exemples de choses que j'ai essayé sans succès.

$File = Get-Content $FileLocation
$XML = [XML]$File

foreach ($attribute in $XML.Config2.Interested-Item)
{
     $attribute = Interested-Item.attribute-1 = "Updated Texted"
}
XML.Save($FileLocation)

cela ne fait rien pour moi. Il ne modifie pas le fichier du tout.

$File = Get-Content $FileLocation
$node = $File.SelectSingleNode("/Config2/Interetested-Item[@attribute-1]")
$node.Value = "New-Value"
$File.Save($FileLocation)

renvoie l'erreur suivante.

The property 'Value' cannot be found on this object. Verify that the property exists and can be set.At line:5 char:1
+ $node.Value = "New-Value"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

j'ai essayé de l'implémenter en utilisant-Xpath de Get-Help Select-XML et j'ai également échoué avec cela.

la seule chose avec laquelle j'ai eu du succès, ce qui ne fonctionne pas dans la pratique est la suivante.

(Get-Content $FileLocation) | ForEach-Object{$_ -replace "text-*", "NewText"} | Set-Content $FileLocation

cela fonctionnera de force pour la première fois, et ensuite ne sera pas en mesure de mettre à jour le paramètre, en raison de la mise en place d'une nouvelle valeur. Mon intention est d'exécuter ce script plusieurs fois afin de mettre à jour un groupe de fichiers de configuration.

2
demandé sur micsea64 2014-07-21 17:29:22

1 réponses

il y a plusieurs façons. Par exemple, vous pouvez utiliser XPath:

$File = Get-Content $FileLocation
$XML = [XML]$File

$XPpath = "/configuration/Config2/Interested-Item[@attribute-1]"

# Selecting all nodes that match our $XPath (i.e. all
# '/configuration/Config2/Interested-Item' nodes that have attribute 
# 'attribute-1'.
$nodes = $XML.SelectNodes($XPpath)

# Updating the attribute value for all selected nodes.
$nodes | % { $_.SetAttribute("attribute-1", "foo") }

$XML.OuterXml | Out-File $FileLocation

plus d'info ici et plus généralement w3schools.com est votre ami lorsque vous avez affaire à HTML ou XML.

3
répondu Alexander Obersht 2014-07-21 15:10:30