How to Copy Files From an Azure App Service with PowerShell

How to Copy Files From an Azure App Service with PowerShell


Creating your own tools through PowerShell makes transferring content locally exponentially simpler. Here's how to do it yourself. 

Once an Azure app service has been created and all content uploaded, we sometimes need a way to download those files locally. Typically, we do this by either modifying the content in some way using our internal tools or by taking a backup. Unfortunately, though, Microsoft does not provide an easy way to download files locally without jumping through several hoops.


To create a tool like this, we will need a few things to begin. First, we'll obviously need PowerShell. I'll be using PowerShell v5, but v4 will work as well. We will also need a couple of "helper" PowerShell modules to assist us. To do the actual transfer, we'll technically be syncing data from Azure to on-premises using the MSDeploy tool. Although possible without using a separate PowerShell module, the PSWebDeploy module will prevent us from having to wade through all of the complicated MSDeploy parameters and switches. One of the prereqs to the PSWebDeploy module is Web Deploy, so we'll need to get that installed as well.


Building the File Download Tool

Once you've downloaded all of the prerequisites, believe it or not, the majority of the work is already done! Because an Azure app service is just another IIS web site, we'll be using the MSDeploy tool (through the PSWebDeploy PowerShell module) to do all of the heavy liftings.

To download all content from an Azure app service, we'll use the Sync-Website function that's in the PSWebDeploy module. This function acts as a wrapper to msdeploy. It automatically sends msdeploy all of the required parameters and switches its needs to download the contents of an IIS websites that's available inside of an Azure app service.

The Sync-Website command has four different parameters we'll have to use. Those parameters are SourcePath, TargetPath, ComputerName and Credential.

  • SourcePath - This will be wwwRoot to download all of the contents of the entire site
  • TargetPath - This is any folder on a file system you'd like.
  • ComputerName - This is the URI to the msdeploy endpoint on the Azure app service
  • Credential - This will capture the Azure username and password to authenticate to the app service.

Below is an example of what invoking Sync-Website with these parameters may look like:

$appServiceName = 'myappservice'
$credential = Get-Credential

$syncParams = @{
    SourcePath = 'wwwroot'
    TargetPath = 'C:\MyLocalFolder'
    ComputerName = "https://$$appServiceName"
    Credential = $credential

Sync-Website @syncParams

Read: Leveraging PowerShell Automation In The Cloud

Creating a Simpler Function

When the above command is run, you will find all of the contents of the Azure app service in the C:\MyLocalFolder location! You could be done here, but I've chosen to make this task even simpler by creating a Save-AzrWebApp function. This function simplifies this process even more and allows me to do something like this instead:

PS> Save-AzrWebApp -Name 'myappservice' -TargetPath 'C:\MyLocalFolder' -Credential (Get-Credential)

Wrap Up

If you'd look under the covers at what exactly happens when Save-AzrWebApp runs, you'd be surprised. This is what's great about using PowerShell. We, as a community, can build tools like this to not only help ourselves but to help others as well! Copying files from an Azure app service is not a trivial one-liner as you've seen here. However, once the tools are built around that problem all of the msdeploy finagling can soon be turned into a simple Save-AzrWebApp function!


Related Posts

Comments are disabled in preview mode.
Loading animation