m.l.i
m.l.i

Reputation: 11

How can i update a current value in a XML file using powershell?

Hello everyone i dont know if this question is asked before but i need to update couple of values in a xml file using powershell i have the following code it works local but on tfs it doesn't work i get the following error System.Management.Automation.MethodInvocationException: Exception calling "ReadAllText" with "1" argument(s): "Value cannot be null.

And i have the following code

$EmployeeData= "C:\files\Employees.xml"
$oldEmployeeID= "23"
$NewEmployeeID= "25"

$content = [System.IO.File]::ReadAllText($EmployeeData).Replace($oldEmployeeID, $NewEmployeeID)
[System.IO.File]::WriteAllText($EmployeeData, $content)

localy it works but on tfs its crashes does anybody know why with the following error code

System.Management.Automation.MethodInvocationException: Exception calling "ReadAllText" with "1" argument(s): "Value cannot be null.

Upvotes: 0

Views: 207

Answers (2)

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31023

If you want to update values/tokens in a XML file during TFS build, a simple way is to use Replace Tokens task or Tokenization Task in your build definition.

Upvotes: 0

Ranadip Dutta
Ranadip Dutta

Reputation: 9133

You do not need to use this. You can directly parse XML in powershell by type casting:

$EmployeeData= "C:\files\Employees.xml"
$oldEmployeeID= "23"
$NewEmployeeID= "25"

[XML]$content = Get-Content $EmployeeData

Then you can access $content.elementId or $content.nodename.

Follow THIS for your reference.

Hope it helps.

Upvotes: 1

Related Questions