powershell - 如何使用 powershell 轮询 kudu api 响应以获取部署状态

标签 powershell azure azure-functions kudu

使用 KUDU api 部署 Azure 函数

这是我的网址:

$apiUrl = 'https://myapp.scm.azurewebsites.net/api/zipdeploy?isAsync=true'

调用剩余方法脚本:

Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method POST -InFile $zipFile -ContentType "multipart/form-data" -OutFile sample.txt -Verbose 

我得到的输出:

VERBOSE: POST https:// myapp.scm.azurewebsites.net /api/zipdeploy?isAsync=true with -1-byte payload
VERBOSE: received 0-byte response of content type  

这里我的问题是我没有收到任何响应,并且sample.txt 是一个空文件。 在使用不带 isAsync 的 url 时,我在 teamcity 中遇到超时异常,这就是我选择异步部署的原因

我可以验证门户以查看部署是否成功,但是如何通过powershell脚本来验证部署状态。

在以下站点中是这样提到的:

Async support: By adding ?isAsync=true to the URL, the deployment will run asynchronously. The Location header of the response will contain the deployment URL that can be polled for deployment status. Webhooks can be used for fully asynchronous notification of completion.

https://github.com/projectkudu/kudu/wiki/Deploying-from-a-zip-file

在此document是这样提到的 -

The Location header of the response will contain a link to a pollable deployment status.

但我不知道如何获取位置 header 。请帮忙。

最佳答案

太疯狂了,Invoke-RestMethod 似乎没有返回 headers 对象,但是 Invoke-WebRequest 却返回了。您应该能够以最小的努力重写您的请求以使用后者。

然后您可以按如下方式访问 header :

PS> $headers = Invoke-WebRequest -Uri https://jsonplaceholder.typicode.com/posts/1 | 
                   select -expand Headers

PS> $headers

Key                              Value
---                              -----
Connection                       keep-alive
Vary                             Origin, Accept-Encoding
...
Server                           cloudflare
...

PS> $headers.Server
cloudflare

回到Invoke-RestMethodthis PR已合并到 PowerShell 中,但我猜它只是在 Core 中,而不是在 Windows PowerShell(完整的框架)中。

您可以安装 PowerShell Core 6.0(与 Windows PowerShell 并行工作)来使用新的 -ResposneHeadersVariable 参数,切换到 Invoke-WebRequest< 可能更明智。/.

这是您对 PowerShell Core 6.1.0-preview2 的请求(也应该适用于稳定版本,2017 年 9 月 25 日之后的任何版本):

PS C:\Program Files\PowerShell\6-preview> Invoke-RestMethod `
        https://jsonplaceholder.typicode.com/posts/1 -ResponseHeadersVariable headers

PS C:\Program Files\PowerShell\6-preview> $headers

Key                              Value
---                              -----
Cache-Control                    {public, max-age=14400}
Connection                       {keep-alive}
Date                             {Fri, 11 May 2018 07:11:05 GMT}
Pragma                           {no-cache}
...

根据this blog post Windows PowerShell 没有 future ,所有努力现在都集中在 PowerShell Core 上。

关于powershell - 如何使用 powershell 轮询 kudu api 响应以获取部署状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50285633/

相关文章:

sql-server - Azure SQL 管理实例 - 恢复数据库不同版本

c# - 使用 ODBC 驱动程序从 ASP.NET 连接到 Azure Databricks

node.js - Azure 函数在 Node 启动时运行代码

azure - 如何从配置文件中获取 blob 路径名容器名称

collections - 是否可以在 powershell 中向集合添加属性?

powershell - 从批处理重定向到Powershell

azure - 使用自定义域和 SSL 配置 2 个 Web 应用程序上的 Azure 流量管理器

Azure Functions 作为 Web 应用程序指南

powershell - 为什么 -replace 没有按预期工作?

batch-file - 如何使用qpdf解密多个pdf文件?