Azure devops Azure DevOps服务器:以编程方式创建Wiki页面

Azure devops Azure DevOps服务器:以编程方式创建Wiki页面,azure-devops,azure-pipelines,azure-devops-rest-api,Azure Devops,Azure Pipelines,Azure Devops Rest Api,我尝试在Azure DevOps Server 2019.0.1中基于.net xml文档为Overview>Wiki生成降价Wiki页面 为了在每个构建中从.xml生成.md,我使用PowerShell脚本调用 当前要发布Wiki页面,我必须手动将.md文件上载到存储库,并通过将代码发布为Wiki选项将其链接 是否可以在构建过程中以编程方式将.md文件发布为wikipage,而不将其添加到主存储库中?我想让wikipages在每个构建过程中保持最新 结果如下所示: Add-Type -Ass

我尝试在Azure DevOps Server 2019.0.1中基于.net xml文档为
Overview>Wiki
生成降价Wiki页面

为了在每个构建中从
.xml
生成
.md
,我使用PowerShell脚本调用

当前要发布Wiki页面,我必须手动将
.md
文件上载到存储库,并通过将代码发布为Wiki选项将其链接

是否可以在构建过程中以编程方式将
.md
文件发布为wikipage,而不将其添加到主存储库中?我想让wikipages在每个构建过程中保持最新

结果如下所示:

Add-Type -AssemblyName System.Net.Http

#CREATE HTTP CLIENT
$client = New-Object -TypeName System.Net.Http.Httpclient
Write-Host "Request Azure Devops API:" -ForegroundColor Yellow

#PERSONAL ACCESS TOKEN
[string] $PersonalAccessToken = "uwadlzqp6j7i5n35dazsswmwp6gth2w2sxb55h3zrlc2bi7jn5ad";
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PersonalAccessToken)"))

#CONFIGURE CLIENT
$json = New-Object System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")
$client.DefaultRequestHeaders.Accept.Add($json)
$client.DefaultRequestHeaders.Authorization = New-Object System.Net.Http.Headers.AuthenticationHeaderValue("Basic", $token)

#REQUEST
$uri = "http://138.202.18.216:8070/Samples/Framework%20A/_apis/wiki/wikis/"
$task = $client.GetAsync($uri)
Write-Host "URI: $uri" -ForegroundColor Yellow

#WAIT FOR THE ASYNC CALL TO FINISH
$task.Wait()

#USE THE RESULT
if ($task.IsCompleted) {

    #DO YOUR THING HERE.
    [System.Net.Http.HttpResponseMessage] $msg = $task.Result
    $result = $msg.Content.ReadAsStringAsync()
    $converted = ConvertFrom-Json –InputObject $result.Result

    #LOOP RESULTS
    for ($i=0; $i -lt $converted.count; $i++) {
        $entry = $converted.value[$i]
        Write-Host "Name:`t $($entry.name)"
        Write-Host "Url:`t $($entry.url)"
        Write-Host

        #REQUEST WIKI
        $task = $client.GetAsync($entry.url + "/pages?")
        $task.Wait()
        [System.Net.Http.HttpResponseMessage] $msg = $task.Result
        $result = $msg.Content.ReadAsStringAsync()
        $converted = ConvertFrom-Json –InputObject $result.Result
        $converted.content = "Hallo"

        $wiki = $entry.name
        #$uri_new = $entry.url + "/pages?api-version=5.0"
        $uri_new = $entry.url + "/pages?pagePath=FrameworkA?api-version=5.0"
        Write-Host "Overwrite Url: $uri_new" -ForegroundColor Red

        $reconvert = ConvertTo-Json –InputObject $converted
        [System.Net.Http.HttpContent] $content = [System.Net.Http.StringContent]::new($reconvert)
        $client.PutAsync($uri_new, $content)


        Write-Host "SECOND TRY: Invoke-RestMethod" -ForegroundColor Green
        $content_type = $client.DefaultRequestHeaders.Accept.ToString()
        $workitem = Invoke-RestMethod -Method PUT -Uri $uri_new -UseDefaultCredentials -Body @{"content" = "hallo"}
    }
}
Request Azure Devops API:
URI: http://138.202.18.216:8070/Samples/Framework%20A/_apis/wiki/wikis/
Name:    FrameworkA.wiki
Url:     http://138.202.18.216:8070/Samples/4dfea275-73bb-497d-bf64-2fe87891390b/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3

Overwrite Url: http://138.202.18.216:8070/Samples/4dfea275-73bb-497d-bf64-2fe87891390b/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3/pages?pagePath=FrameworkA?api-version=5.0


Result                 : StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
                         {
                           Pragma: no-cache
                           X-TFS-ProcessId: c79f89a2-e776-41e0-8134-a18dfacad964
                           ActivityId: 0821277c-20a3-499a-824b-aaf60d65bd38
                           X-TFS-Session: 0821277c-20a3-499a-824b-aaf60d65bd38
                           X-VSS-E2EID: 0821277c-20a3-499a-824b-aaf60d65bd38
                           X-VSS-UserData: d82854f4-53e6-4455-a653-e0c8e665cef1:user
                           X-FRAME-OPTIONS: SAMEORIGIN
                           Lfs-Authenticate: NTLM
                           X-Content-Type-Options: nosniff
                           X-Cache: MISS from ptc-bld-squid
                           X-Cache-Lookup: MISS from ptc-bld-squid:3128
                           Connection: keep-alive
                           Cache-Control: no-cache
                           Date: Mon, 08 Jul 2019 10:53:01 GMT
                           P3P: CP="CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IVDo OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT"
                           Server: Microsoft-IIS/10.0
                           Via: 1.1 ptc-bld-squid (squid/3.5.23)
                           X-AspNet-Version: 4.0.30319
                           X-Powered-By: ASP.NET
                           Content-Length: 92
                           Allow: GET
                           Content-Type: application/json; charset=utf-8
                           Expires: -1
                         }
Id                     : 277695
Exception              : 
Status                 : RanToCompletion
IsCanceled             : False
IsCompleted            : True
CreationOptions        : None
AsyncState             : 
IsFaulted              : False
AsyncWaitHandle        : System.Threading.ManualResetEvent
CompletedSynchronously : False

SECOND TRY: Invoke-RestMethod
Invoke-RestMethod : {"count":1,"value":{"Message":"The requested resource does not support http method 'PUT'."}}
In C:\Users\user\Desktop\Azure DevOps Console\DevOps API Call.ps1:60 Zeichen:21
+ ... $workitem = Invoke-RestMethod -Method PUT -Uri $uri_new -UseDefaultCr ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

编辑:PowerShell脚本:

Add-Type -AssemblyName System.Net.Http

#CREATE HTTP CLIENT
$client = New-Object -TypeName System.Net.Http.Httpclient
Write-Host "Request Azure Devops API:" -ForegroundColor Yellow

#PERSONAL ACCESS TOKEN
[string] $PersonalAccessToken = "uwadlzqp6j7i5n35dazsswmwp6gth2w2sxb55h3zrlc2bi7jn5ad";
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PersonalAccessToken)"))

#CONFIGURE CLIENT
$json = New-Object System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")
$client.DefaultRequestHeaders.Accept.Add($json)
$client.DefaultRequestHeaders.Authorization = New-Object System.Net.Http.Headers.AuthenticationHeaderValue("Basic", $token)

#REQUEST
$uri = "http://138.202.18.216:8070/Samples/Framework%20A/_apis/wiki/wikis/"
$task = $client.GetAsync($uri)
Write-Host "URI: $uri" -ForegroundColor Yellow

#WAIT FOR THE ASYNC CALL TO FINISH
$task.Wait()

#USE THE RESULT
if ($task.IsCompleted) {

    #DO YOUR THING HERE.
    [System.Net.Http.HttpResponseMessage] $msg = $task.Result
    $result = $msg.Content.ReadAsStringAsync()
    $converted = ConvertFrom-Json –InputObject $result.Result

    #LOOP RESULTS
    for ($i=0; $i -lt $converted.count; $i++) {
        $entry = $converted.value[$i]
        Write-Host "Name:`t $($entry.name)"
        Write-Host "Url:`t $($entry.url)"
        Write-Host

        #REQUEST WIKI
        $task = $client.GetAsync($entry.url + "/pages?")
        $task.Wait()
        [System.Net.Http.HttpResponseMessage] $msg = $task.Result
        $result = $msg.Content.ReadAsStringAsync()
        $converted = ConvertFrom-Json –InputObject $result.Result
        $converted.content = "Hallo"

        $wiki = $entry.name
        #$uri_new = $entry.url + "/pages?api-version=5.0"
        $uri_new = $entry.url + "/pages?pagePath=FrameworkA?api-version=5.0"
        Write-Host "Overwrite Url: $uri_new" -ForegroundColor Red

        $reconvert = ConvertTo-Json –InputObject $converted
        [System.Net.Http.HttpContent] $content = [System.Net.Http.StringContent]::new($reconvert)
        $client.PutAsync($uri_new, $content)


        Write-Host "SECOND TRY: Invoke-RestMethod" -ForegroundColor Green
        $content_type = $client.DefaultRequestHeaders.Accept.ToString()
        $workitem = Invoke-RestMethod -Method PUT -Uri $uri_new -UseDefaultCredentials -Body @{"content" = "hallo"}
    }
}
Request Azure Devops API:
URI: http://138.202.18.216:8070/Samples/Framework%20A/_apis/wiki/wikis/
Name:    FrameworkA.wiki
Url:     http://138.202.18.216:8070/Samples/4dfea275-73bb-497d-bf64-2fe87891390b/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3

Overwrite Url: http://138.202.18.216:8070/Samples/4dfea275-73bb-497d-bf64-2fe87891390b/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3/pages?pagePath=FrameworkA?api-version=5.0


Result                 : StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
                         {
                           Pragma: no-cache
                           X-TFS-ProcessId: c79f89a2-e776-41e0-8134-a18dfacad964
                           ActivityId: 0821277c-20a3-499a-824b-aaf60d65bd38
                           X-TFS-Session: 0821277c-20a3-499a-824b-aaf60d65bd38
                           X-VSS-E2EID: 0821277c-20a3-499a-824b-aaf60d65bd38
                           X-VSS-UserData: d82854f4-53e6-4455-a653-e0c8e665cef1:user
                           X-FRAME-OPTIONS: SAMEORIGIN
                           Lfs-Authenticate: NTLM
                           X-Content-Type-Options: nosniff
                           X-Cache: MISS from ptc-bld-squid
                           X-Cache-Lookup: MISS from ptc-bld-squid:3128
                           Connection: keep-alive
                           Cache-Control: no-cache
                           Date: Mon, 08 Jul 2019 10:53:01 GMT
                           P3P: CP="CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IVDo OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT"
                           Server: Microsoft-IIS/10.0
                           Via: 1.1 ptc-bld-squid (squid/3.5.23)
                           X-AspNet-Version: 4.0.30319
                           X-Powered-By: ASP.NET
                           Content-Length: 92
                           Allow: GET
                           Content-Type: application/json; charset=utf-8
                           Expires: -1
                         }
Id                     : 277695
Exception              : 
Status                 : RanToCompletion
IsCanceled             : False
IsCompleted            : True
CreationOptions        : None
AsyncState             : 
IsFaulted              : False
AsyncWaitHandle        : System.Threading.ManualResetEvent
CompletedSynchronously : False

SECOND TRY: Invoke-RestMethod
Invoke-RestMethod : {"count":1,"value":{"Message":"The requested resource does not support http method 'PUT'."}}
In C:\Users\user\Desktop\Azure DevOps Console\DevOps API Call.ps1:60 Zeichen:21
+ ... $workitem = Invoke-RestMethod -Method PUT -Uri $uri_new -UseDefaultCr ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
该脚本尝试两种不同的方法将/发布到服务器:

  • System.Net.Http.Client

  • 调用RestMethod-方法PUT

  • 输出为:

    Add-Type -AssemblyName System.Net.Http
    
    #CREATE HTTP CLIENT
    $client = New-Object -TypeName System.Net.Http.Httpclient
    Write-Host "Request Azure Devops API:" -ForegroundColor Yellow
    
    #PERSONAL ACCESS TOKEN
    [string] $PersonalAccessToken = "uwadlzqp6j7i5n35dazsswmwp6gth2w2sxb55h3zrlc2bi7jn5ad";
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PersonalAccessToken)"))
    
    #CONFIGURE CLIENT
    $json = New-Object System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")
    $client.DefaultRequestHeaders.Accept.Add($json)
    $client.DefaultRequestHeaders.Authorization = New-Object System.Net.Http.Headers.AuthenticationHeaderValue("Basic", $token)
    
    #REQUEST
    $uri = "http://138.202.18.216:8070/Samples/Framework%20A/_apis/wiki/wikis/"
    $task = $client.GetAsync($uri)
    Write-Host "URI: $uri" -ForegroundColor Yellow
    
    #WAIT FOR THE ASYNC CALL TO FINISH
    $task.Wait()
    
    #USE THE RESULT
    if ($task.IsCompleted) {
    
        #DO YOUR THING HERE.
        [System.Net.Http.HttpResponseMessage] $msg = $task.Result
        $result = $msg.Content.ReadAsStringAsync()
        $converted = ConvertFrom-Json –InputObject $result.Result
    
        #LOOP RESULTS
        for ($i=0; $i -lt $converted.count; $i++) {
            $entry = $converted.value[$i]
            Write-Host "Name:`t $($entry.name)"
            Write-Host "Url:`t $($entry.url)"
            Write-Host
    
            #REQUEST WIKI
            $task = $client.GetAsync($entry.url + "/pages?")
            $task.Wait()
            [System.Net.Http.HttpResponseMessage] $msg = $task.Result
            $result = $msg.Content.ReadAsStringAsync()
            $converted = ConvertFrom-Json –InputObject $result.Result
            $converted.content = "Hallo"
    
            $wiki = $entry.name
            #$uri_new = $entry.url + "/pages?api-version=5.0"
            $uri_new = $entry.url + "/pages?pagePath=FrameworkA?api-version=5.0"
            Write-Host "Overwrite Url: $uri_new" -ForegroundColor Red
    
            $reconvert = ConvertTo-Json –InputObject $converted
            [System.Net.Http.HttpContent] $content = [System.Net.Http.StringContent]::new($reconvert)
            $client.PutAsync($uri_new, $content)
    
    
            Write-Host "SECOND TRY: Invoke-RestMethod" -ForegroundColor Green
            $content_type = $client.DefaultRequestHeaders.Accept.ToString()
            $workitem = Invoke-RestMethod -Method PUT -Uri $uri_new -UseDefaultCredentials -Body @{"content" = "hallo"}
        }
    }
    
    Request Azure Devops API:
    URI: http://138.202.18.216:8070/Samples/Framework%20A/_apis/wiki/wikis/
    Name:    FrameworkA.wiki
    Url:     http://138.202.18.216:8070/Samples/4dfea275-73bb-497d-bf64-2fe87891390b/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3
    
    Overwrite Url: http://138.202.18.216:8070/Samples/4dfea275-73bb-497d-bf64-2fe87891390b/_apis/wiki/wikis/2e887fde-ce76-4180-aa8d-f26ed4799eb3/pages?pagePath=FrameworkA?api-version=5.0
    
    
    Result                 : StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
                             {
                               Pragma: no-cache
                               X-TFS-ProcessId: c79f89a2-e776-41e0-8134-a18dfacad964
                               ActivityId: 0821277c-20a3-499a-824b-aaf60d65bd38
                               X-TFS-Session: 0821277c-20a3-499a-824b-aaf60d65bd38
                               X-VSS-E2EID: 0821277c-20a3-499a-824b-aaf60d65bd38
                               X-VSS-UserData: d82854f4-53e6-4455-a653-e0c8e665cef1:user
                               X-FRAME-OPTIONS: SAMEORIGIN
                               Lfs-Authenticate: NTLM
                               X-Content-Type-Options: nosniff
                               X-Cache: MISS from ptc-bld-squid
                               X-Cache-Lookup: MISS from ptc-bld-squid:3128
                               Connection: keep-alive
                               Cache-Control: no-cache
                               Date: Mon, 08 Jul 2019 10:53:01 GMT
                               P3P: CP="CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IVDo OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT"
                               Server: Microsoft-IIS/10.0
                               Via: 1.1 ptc-bld-squid (squid/3.5.23)
                               X-AspNet-Version: 4.0.30319
                               X-Powered-By: ASP.NET
                               Content-Length: 92
                               Allow: GET
                               Content-Type: application/json; charset=utf-8
                               Expires: -1
                             }
    Id                     : 277695
    Exception              : 
    Status                 : RanToCompletion
    IsCanceled             : False
    IsCompleted            : True
    CreationOptions        : None
    AsyncState             : 
    IsFaulted              : False
    AsyncWaitHandle        : System.Threading.ManualResetEvent
    CompletedSynchronously : False
    
    SECOND TRY: Invoke-RestMethod
    Invoke-RestMethod : {"count":1,"value":{"Message":"The requested resource does not support http method 'PUT'."}}
    In C:\Users\user\Desktop\Azure DevOps Console\DevOps API Call.ps1:60 Zeichen:21
    + ... $workitem = Invoke-RestMethod -Method PUT -Uri $uri_new -UseDefaultCr ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
        + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
    

    您可以使用以下语法创建或更新Wiki页面:

    正文:


    因此,在构建中,您可以添加使用上述API的PowerShell任务并创建wiki页面。

    感谢您的指导。我按照的步骤使用RESTAPI,生成并添加一个新的PAT,然后尝试他的演示脚本。但是身份验证响应是
    错误:无法检索请求的URL-访问被拒绝。
    。我知道这个错误有很多原因,但是设置
    $orgUrl=”正确吗https://132.200.14.216:8070“
    $personalToken=“uwawlzqp6j7i5n25dazspwmnp63th2w2axb5563hrlc2bi7yr5za”
    。我不认为我能在这里做错什么?是否还要配置Azure DevOps服务器?如果它是您的本地Azure DevOps服务器,请尝试使用powershell
    调用RestMethod
    ,并使用
    -UseDefaultCredentials
    (且不使用PAT)。更进一步,我获得了权限。但是现在API在
    PUT
    之后返回一个json,有一个异常:
    System.Net.WebException:远程服务器返回了一个错误:(403)不允许
    。我将http内容定义为:
    @{“content”=“FrameworkA”}
    。此方法允许我更新现有的wiki页面或?这意味着
    \u api/wiki/wiki/FrameworkX.wiki
    中给出的页面必须存在吗?另一个问题是,我想上传
    path=$($path)
    的本地路径。可能吗?我需要哪种格式?我想上传基于
    标记文件的wiki页面。@MarTin请用您尝试的脚本编辑问题(或打开一个新问题)。我的最终url是:
    ../4dfea275-73bb-497d-bf64-2fe87891390b/_api/wiki/2e887fde-ce76-4180-aa8d-f26ed4799eb3/pagePath=FrameworkA?api version=5.0
    ?在我的浏览器中,我得到了以下信息:
    {“path”:“/”,“order”:0,“isParentPage”:true,“gitItemPath”:“/”,“subpage”:[],“url”:“LONG url”,“remoteUrl”:“LONG url”,“content”:“}
    。我在PowerShell也有同样的感受。我认为下一步是覆盖您描述的
    内容
    变量,但我得到了错误:
    请求的资源不支持http方法“PUT”。
    来自
    调用RestMethod-方法PUT-UseDefaultCredentials