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
[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:
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.