<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1678611822423757&amp;ev=PageView&amp;noscript=1">
Defrag This

| Read. Reflect. Reboot.

How to Copy Files From an Azure App Service with PowerShell

Adam Bertram| March 09 2018

| IT insights

copy-files-from-an-azure-app.jpg

copy-files-from-an-azure-app.jpg

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 that content 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 transfer all of this content locally without jumping through several hoops.

Prerequisites

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 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.scm.azurewebsites.net:443/msdeploy.axd?site=$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!

Topics: IT insights

Leave a Reply

Your email address will not be published. Required fields are marked *

THIS POST WAS WRITTEN BY Adam Bertram

Adam Bertram is a 20-year veteran of IT. He’s currently an automation engineer, blogger, independent consultant, freelance writer, author, and trainer. Adam focuses on DevOps, system management, and automation technologies as well as various cloud platforms. He is a Microsoft Cloud and Datacenter Management MVP and efficiency nerd that enjoys teaching others a better way to leverage automation.

Free Trials

Getting started has never been easier. Download a trial today.

Download Free Trials

Contact Us

Let us know how we can help you. Focus on what matters. 

Send us a note

Subscribe to our Blog

Let’s stay in touch! Register to receive our blog updates.