11

我正在尝试使用 PowerShell 通过 REST API 将更新的内容文件放到 Azure 网站上。但是,在向我提供凭据时,Invoke-RestMethod -Credentials我会返回标准 Azure 登录页面的 HTML。

如何从 PowerShell 使用 Kudu 进行身份验证?谢谢。

4

2 回答 2

15

您可以先通过 Powershell 获取网站,然后使用网站的发布凭据调用 Kudu REST API。下面的示例将获得 Kudu 版本。

$website = Get-AzureWebsite -Name "WebsiteName"

$username = $website.PublishingUsername
$password = $website.PublishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$apiBaseUrl = "https://$($website.Name).scm.azurewebsites.net/api"

$kuduVersion = Invoke-RestMethod -Uri "$apiBaseUrl/environment" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET
于 2014-12-27T04:29:44.787 回答
10

在新的 ARM 世界和最新的 PowerShell 中,您需要对 @Seth 的回答进行一些调整。

具体来说,你获取发布信用的方式不同,就是前3行。其余的我无耻地从@Seth 复制以完成片段。

确保根据需要替换 YourResourceGroup/YourWebApp:

$creds = Invoke-AzureRmResourceAction -ResourceGroupName YourResourceGroup -ResourceType Microsoft.Web/sites/config -ResourceName YourWebApp/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force

$username = $creds.Properties.PublishingUserName
$password = $creds.Properties.PublishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$apiBaseUrl = "https://$($website.Name).scm.azurewebsites.net/api"

$kuduVersion = Invoke-RestMethod -Uri "$apiBaseUrl/environment" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET
于 2015-12-30T00:48:27.073 回答