xmlpowershellpowershell-remotingpowershell-corexmlupdate

How to invoke PowerShell script from command prompt that updates XML file?


Invoke PowerShell script from command prompt to update XML file nodes.

Sample XML File

<Job>
  <ProcFile name="jobName" />
  <Category name="Indicative">
    <Item name="rbfVersion">versionname</Item>
    <Item name="rbfRelease">releaseNumber</Item>
  </Category>
  <Category name="Parameters">
    <Item name="MmsPzapAppId">Value1</Item>
  </Category>
</Job>

PowerShell Script prepared to update nodes of xml file.

param ($workingDir, $fileName, $arrayOfObject)

if (!$workingDir) {
    throw "Working Directory Parameter is missing"
}elseif(!$fileName){
    throw "File name or Runbook name Parameter is missing"
}elseif(!$arrayOfObject){
    throw "Input parameters are missing"
}
$keyVauleHash = $arrayOfObject

#concatenating two parameters to get full path 
$filePath = "$workingDir\$fileName"
 
#Reading Values 
Write-Host "Working Directory: $workingDir"
Write-Host "File Name : $fileName"
Write-Host "Full Path : $filePath"
Write-Host "Number of Nodes to be updated:"$keyVauleHash.Count

#Get Content of XML file and store it in $xml file
[xml]$xml = Get-Content $filePath

#Looping through each key:value pair from array of object and updating respective nodes
foreach($item in $keyVauleHash.GetEnumerator()){
    $key = $item.key
    $value = $item.value
    $parametersNode = $xml.Job.Category | where {$_.Name -eq 'Parameters'}
    $foundNode = $parametersNode.Item | where {$_.Name -eq $key} 
    Write-Output $foundNode
    $foundNode.'#text' = $value
    Write-Output $foundNode
}

#Saving the changes
$xml.Save($filePath)

Script usage or invoke command

pwsh -command C:\Path\To\PowerShellScript\UpdateXML.ps1 -workingDir 'C:\Path\To\xmlFile' -fileName 'xmlFileName.xml' -arrayOfObject '@{InputFile="Value"}'

Above script should be invoked from command prompt.

Tried different method also, Tried PowerShell.exe instead of pwsh then also didn't work.

alternate invoke command

PowerShell.exe C:\Path\To\PowerShellScript\UpdateXML.ps1 -workingDir 'C:\Path\To\xmlFile' -fileName 'xmlFileName.xml' -arrayOfObject '@{InputFile="Value"}'

Both of the invoke methods are not working actually. I am getting this below error.

The property '#text' cannot be found on this object. Verify that the property exists and can be set.

Solution

  • Following code uses XML and a dictionary to change values of nodes

    using assembly System.Xml.Linq
    
    $inputFilename = "c:\temp\test.xml"
    $outputFilename = "c:\temp\test1.xml"
    
    $doc = [System.Xml.Linq.XDocument]::Load($inputFilename)
    
    $items = $doc.Descendants("Item")
    
    $dict = [System.Collections.Generic.Dictionary[string, [System.Xml.Linq.XElement]]]::new()
    
    $items | foreach { $name = $_.Attribute("name").Value; $element = $_; $dict.Add($name, $element) }
    
    $newValues = @{
       rbfVersion = 'version1'
       rbfRelease = '2.0'
       MmsPzapAppId = '123'
    }
    foreach($newValue in $newValues.Keys)
    {
       $key = $newValue 
       $value = $newValues[$newValue]
       $element = $dict[$key]
       $element.SetValue($value)
    }
    $doc.Save($outputFilename)