Home > Back-end >  How to upload files to Azure DevOps via Power Shell script and Rest API
How to upload files to Azure DevOps via Power Shell script and Rest API

Time:11-29

I need to upload file to Azure DevOps repository using rest api and power shell scripts. I did this:

$token = "token"
$authHeader = @{Authorization = 'Basic '   [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($token)")) }

$folderPath = "C:/"
$fileName = "TestTXT.txt"
$body = [System.IO.File]::ReadAllBytes("$folderPath/$fileName")

$org = "orgName"
$project = "projectName"

$createAttachmetUrlTemplate = "https://dev.azure.com/$org/$project/_apis/wit/attachments?fileName={fileName}&api-version=5.0"
$postUrl = $createAttachmetUrlTemplate -replace "{filename}", $fileName

$result = Invoke-RestMethod -Uri $postUrl -Method Post -ContentType "application/json" -Headers $authHeader -Body $body

I have got as result id and url accordingly enter image description here

But there isn't any new file in my repo. Where is it? What is wrong? How can I store files in my repo?

enter image description here

CodePudding user response:

The REST API RESULT

You will see the upload.txt in your repo. file in repo

  • Related