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.

Luke Puplett
  • 42,091
  • 47
  • 181
  • 266

2 Answers2

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
Seth
  • 772
  • 1
  • 6
  • 9
  • Excellent example and demo of basic auth. – Luke Puplett Dec 28 '14 at 11:34
  • I added a new answer to take @Seth's answer into the Azure ARM world. – David Ebbo Dec 30 '15 at 00:49
  • 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] }` – Yash Jan 22 '16 at 02:25
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
David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • 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? – Antón Molleda Apr 21 '16 at 16:58
  • 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. – David Ebbo Apr 22 '16 at 01:08
  • The question is [here](http://stackoverflow.com/questions/36801349/how-to-access-the-publishing-credentials-of-a-website-using-azure-rm-and-resourc), thanks! – Antón Molleda Apr 22 '16 at 18:53