PowerShell xml.Save does not save the file on the same path in IntelliJ

56 views Asked by At

I have created a simple powershell script (replacer.ps1), which:

  1. Reads a xml file
  2. Changes one of the attributes
  3. Saves file

The code of replacer.ps1 is as follows:

function ReplaceElemInXml
{
    param ([string]$xmlFilePath)
    [xml]$xmlContent = Get-Content -Path $xmlFilePath
    $element = $xmlContent.SelectSingleNode("//aa")
    $element.SetAttribute("checksum", "newAttributeValue")
    $xmlContent.Save($xmlFilePath)
    Write-Output "Attribute updated successfully for file: $xmlFilePath "
}

ReplaceElemInXml -xmlFilePath "test.xml"

the structure of my folder is:

enter image description here

After I run the code from the commandline in IntelliJ:

> attributeReplacer\childFolder\onemoreChildFolder> .\replacer.ps1

The output says:

Attribute updated successfully for file: test.xml 

But the original test.xml file is not being updated, instead a new test.xml file is being created in the root folder of IntelliJ project:

enter image description here

How can I overwrite the file in the current folder?

0

There are 0 answers