【发布时间】:2014-12-12 12:08:14
【问题描述】:
我正在尝试使用 PowerShell 通过 REST API 将更新的内容文件放到 Azure 网站上。但是,当将我的凭据提供给 Invoke-RestMethod -Credentials 时,我会返回标准 Azure 登录页面的 HTML。
如何通过 PowerShell 对 Kudu 进行身份验证?谢谢。
【问题讨论】:
标签: rest powershell azure-web-app-service kudu
我正在尝试使用 PowerShell 通过 REST API 将更新的内容文件放到 Azure 网站上。但是,当将我的凭据提供给 Invoke-RestMethod -Credentials 时,我会返回标准 Azure 登录页面的 HTML。
如何通过 PowerShell 对 Kudu 进行身份验证?谢谢。
【问题讨论】:
标签: rest powershell azure-web-app-service kudu
您可以先通过 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
【讨论】:
$matchedNames = $azureWebSite.EnabledHostNames -match 'scm' if($matchedNames -and $matchedNames.count -gt 0) { $WebSiteName = $matchedNames[0] }
在新的 ARM 世界和最新的 PowerShell 中,您需要对 @Seth 的回答进行一些调整。
具体来说,获取发布凭据的方式不一样,就是前3行。其余的我无耻地从@Seth 复制以完成sn-p。
确保根据需要替换 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
【讨论】: