Azure Websites Kudu REST API - Authentication
Asked Answered
B

2

11

I'm trying to use PowerShell to put an updated content file onto an Azure Website via the REST API. However, when supplying my credentials into Invoke-RestMethod -Credentials I am returned the HTML of the standard Azure login page.

How can I authenticate with Kudu from PowerShell? Thanks.

Belshin answered 12/12, 2014 at 12:8 Comment(0)
S
16

You can first get the website via Powershell and then use the publish credentials from the website to call the Kudu REST API. The example below will get the Kudu version.

$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
Schiff answered 27/12, 2014 at 4:29 Comment(3)
Excellent example and demo of basic auth.Belshin
I added a new answer to take @Seth's answer into the Azure ARM world.Rhodos
Careful, if you are using slot, $website.Name would be websitename(slot) in which case, Invoke-RestMethod fails. Instead do $matchedNames = $azureWebSite.EnabledHostNames -match 'scm' if($matchedNames -and $matchedNames.count -gt 0) { $WebSiteName = $matchedNames[0] }Caulicle
R
10

In the new ARM world and with the latest PowerShell, you'll need to make some adjustments to @Seth's answer.

Specifically, the way you obtain the publishing creds is different, which is the first 3 lines. The rest I shamelessly copied from @Seth to complete the snippet.

Make sure to replace YourResourceGroup/YourWebApp as appropriate:

$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
Rhodos answered 30/12, 2015 at 0:48 Comment(3)
I'm trying to get the credentials via your cmdlet above but using a ResourceId instead and I'm always getting an error. What I've done is run your command, get the right credentials and then use the Id as the -ResourceId to see if I can get them (I need to automate this for a series of websites in different resourcegroups). Any idea of why this could be not working?Bulbil
Sorry, I don't completely follow. It would be best to ask a new question so you can show in more details what you are trying. Post the new question link here and I'll try to take a look.Rhodos
The question is here, thanks!Bulbil

© 2022 - 2024 — McMap. All rights reserved.