This article is contributed. See the original author and article here.

If you would like to download the azure app service contents to on-prem server and restore back to azure app service using powershell script, you can refer following steps.


 


First, we can use KUDU Zip  REST API download the folders as ZIP files.


GET /api/zip/{path}/


For more details, here is a document for your reference.


https://github.com/projectkudu/kudu/wiki/REST-API#zip


Since we need to use automated method way to do this, we can use service principal to connect azure account, then generate the token to call REST API.


For service principal, you can refer following document to create service principal.


https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal


 


In order to call REST API, powershell usually use the Invoke-RestMethod module to call API.


However, Invoke-RestMethod  may have some problem when the site contains a lot of files.


This was raised in github issue as following link.


https://github.com/projectkudu/kudu/issues/1448#issuecomment-253796347


There is a workaround posted in above link you can refer.


It looks like it’s tied to Invoke-RestMethod and Invoke-WebRequest though, it seems to work fine when using System.Net.WebClient instead, so that can be a workaround for this.


So the final script would be as following:


 


 

$resourceGroupName = "henryapprg"
$webAppName = "freewinhe" 
$slotName =  ""
$localPath = "c:testhenry.zip"
$ClientSecret = "serviceprincipalclientsecret"
$ApplicationID = "serviceApplicationID"
$TenantID = "tenantid"

#use service principal to connect Azure Account 
$passwd = ConvertTo-SecureString $ClientSecret -AsPlainText -Force
$pscredential = New-Object System.Management.Automation.PSCredential($ApplicationID, $passwd)
Connect-AzAccount -Credential $pscredential -Tenant $TenantID -ServicePrincipal

#generate token with Azure Account. 
$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$context =Get-AzContext
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azProfile)
Write-Debug ("Getting access token for tenant" + $currentAzureContext.Subscription.TenantId)
$token = $profileClient.AcquireAccessToken($context.Subscription.TenantId)
$token.AccessToken 
$kuduApiAuthorisationToken = 'Bearer {0}' -f $token.AccessToken


    if ($slotName -eq ""){
        $kuduApiUrl = "https://$webAppName.scm.azurewebsites.net/api/zip/site/wwwroot/"
    }
    else{
        $kuduApiUrl = "https://$webAppName`-$slotName.scm.azurewebsites.net/api/zip/site/wwwroot/"
    }
    $virtualPath = $kuduApiUrl.Replace(".scm.azurewebsites.", ".azurewebsites.").Replace("/api/zip/site/wwwroot", "")
    Write-Host " Downloading File from WebApp. Source: '$kuduApiUrl'. Target: '$localPath'..." -ForegroundColor DarkGray


#Call zip Rest API to download the Zip file from webapp. 
`
$WebClient = New-Object System.Net.WebClient
$WebClient.Headers.Add('Authorization', $kuduApiAuthorisationToken)
$WebClient.Headers.Add('ContentType', 'multipart/form-data')
$WebClient.DownloadFile($kuduApiUrl, $localPath)

 


 


 


Second, there is a document that introduce a powershell command to zip deploy.


https://docs.microsoft.com/en-us/azure/app-service/deploy-zip#with-powershell


Publish-AzWebapp -ResourceGroupName <group-name> -Name <app-name> -ArchivePath <zip-file-path>


 


So the restore script would be like this:


 

$localPath = "c:testhenry.zip"
$ClientSecret = "serviceprincipalclientsecret"
$ApplicationID = "serviceApplicationID"
$TenantID = "tenantid"

#use service principal to connect Azure Account
$passwd = ConvertTo-SecureString $ClientSecret -AsPlainText -Force
$pscredential = New-Object System.Management.Automation.PSCredential($ApplicationID, $passwd)
Connect-AzAccount -Credential $pscredential -Tenant $TenantID -ServicePrincipal


Publish-AzWebapp -ResourceGroupName "henryapprg" -Name "phpwinhenry" -ArchivePath $localPath -Force 

 


 


 

Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.