Powershell script to update XML file content

前端 未结 3 1568
天涯浪人
天涯浪人 2020-12-15 16:25

Please help me create a Powershell script that will go through an XML file and update content. In the example below, I want to use the script to pull out and change the fil

3条回答
  •  离开以前
    2020-12-15 17:06

    Try this:

    $xmlFileName = "c:\so.xml"
    $match = "C:\\Prog\\Laun\.jar"
    $replace = "C:\Prog32\folder\test.jar"
    
    
    # Create a XML document
    [xml]$xmlDoc = New-Object system.Xml.XmlDocument
    
    # Read the existing file
    [xml]$xmlDoc = Get-Content $xmlFileName
    
    $buttons = $xmlDoc.config.button
    $buttons | % { 
        "Processing: " + $_.name + " : " + $_.command
        $_.command = $_.command -Replace $match, $replace
        "Now: " + $_.command
        }
    
    "Complete, saving"
    $xmlDoc.Save($xmlFileName)
    

提交回复
热议问题