Home > Net >  Unable to Add Text File to Azure DevOps Repo using RestApi - File containing '\'
Unable to Add Text File to Azure DevOps Repo using RestApi - File containing '\'

Time:09-07

I am trying to add a file "AssemblyInfo.cs" which contain a '' as part of the text.

enter image description here When trying to use the enter image description here

After Removing the Char RestApi pass enter image description here

i am using the following code to convert the text to Json - Powershell based

function GetJSContentCS {
    param (
        $filepath
    )
    $read = (Get-Content -Encoding UTF8 -RAW -Path $filepath) -replace "(?s)`r`n\s*$"
    $jscontent=''
    foreach ($item in $read) {
        $jsitem = $item '\n'
        $jsitem = $jsitem.replace('"','\"')
        $jscontent  = $jsitem
    }
    return $jscontent
}
GetJSContentCS -filepath 'C:\temp\AssemblyInfo (2).cs'

Any Advise Would be great

CodePudding user response:

The problem is missing a \ character after c: in the converted Json.

By running the PowerShell the converted Json is :

[assembly: XmlConfigurator(ConfigFile = @\"c:\appsLogging.xml\", Watch = true)]\n

However, the correct Json should like this:

[assembly: XmlConfigurator(ConfigFile = @\"c:\\appsLogging.xml\", Watch = true)]

Please try the following script to convert the text to Json:

function GetJSContentCS {
    param (
        $filepath
    )
    $read = (Get-Content -Encoding UTF8 -RAW -Path $filepath) -replace "(?s)`r`n\s*$"
    cls
    Write-Host $read 
    $jscontent=''
    foreach ($item in $read) {
        $jsitem = $item.replace('"','\"')
        $jsitem = $jsitem.replace(':\',':\\')
        $jscontent  = $jsitem
    }
    return $jscontent
}
GetJSContentCS -filepath 'C:\temp\AssemblyInfo.cs'

UPDATE:

Sample PowerShell script for your reference to call the enter image description here

  • Related