Manage Azure AppService Files using Powershell

For the last couple of weeks, I've been working on a project that is focused on Azure App Services and how certain applications can be hosted on them. Part of the project was to examine the performance of the storage space that each plan offers and whether it can accommodate the needs of each application.

If you deploy an App Service that is using code instead of containers, you'll have to upload the files to the App Service's storage yourself. This can be achieved in many ways, including DevOps pipelines, development tools such as Visual Studio and FTP. In my case, I was handed a testing version of the website code that was based on PHP and a large number of files used by the site, including CSS and JS and a lot of image files that I had to upload to the AppService.

In order to provide the required information regarding the endpoints to connect to and with what credentials, Azure provides a file called "Publish Profile". There are three profiles included in the publish file:

  • Web Deploy, used by Visual Studio to upload code
  • Zip Deploy, used when deploying the code using a zip file
  • FTP, where you can upload the files using an FTP client
I couldn't use Web Deploy and I did not want to get into zip files due to the size of the website, so I decided to use the FTP method. 

To get the details of each profile, you have to download the publish profile for the AppService from its Overview blade on the Azure Portal. It should be something like:

The publish profile file is a plain XML file that can be easily parsed with Powershell. Use the following scriptblock (update with the name of your file) to get the information for each profile:

[xml](Get-Content .\website.PublishSettings) |
    % publishData | % publishProfile |
    fl profilename, publishmethod, publishurl, username,userpwd

The above commands should result in something similar to:

The second profile contains all the information you'll need in order to connect using FTP. If you open up your preferred FTP client and fill in the information, you should have access to the remote file system. Below I'm using WinSCP:

Although this is a perfectly fine way to manage the files and directories on the AppService, I wanted something more Powershell friendly so that I would automate some tasks.

The Kudu advanced tools came to mind and sure enough, it provides a VFS REST API that allows some operations on files and folders. However, since this is an API I had to wrap all the calls in Powershell functions.

The result of my efforts is the CPolydorou.Azure.WebApp module that contains the below functions:

  • Get-WebAppItem
  • Get-WebAppChildItem
  • Upload-WebAppFile
  • Upload-WebAppDirectory
  • Remove-WebAppItem
  • New-WebAppDirectory

The first thing you need to do is to create three variables, holding the username, the password, and the deployment URL for your site. For this, we are going to use the output of the publish profile and more specifically the last profile:

Make sure you're using single quotes for the username in order to include the dollar sign and also add the https:// part to the deployment URL. Now that we have the variables configured we can start working on the App Service's storage space. Let's see some examples!

To get the items in your sites root directory use the Get-WebAppChildItem function as shown below:

Use Upload-WebAppFile to upload files, Upload-WebAppDirectory to upload the contents of a directory, and New-WebAppDirectory to create directories:

To remove items from the site use the Remove-WebAppItem function as shown below:

Be extra careful when removing items because not all files are part of the code and deployment and you may lose data!

The Powershell module for Azure WebApps is available on the PowerShell Gallery over here.

The entire Kudu documentation is available here.

Popular posts from this blog

Domain Controller Machine Password Reset

Configuring a Certificate on Exchange Receive Connector

Verbose Parameter Passing to cmdlet inside Function