Skip to content

Instantly share code, notes, and snippets.

@jslyonnais
Last active July 12, 2023 16:59
Show Gist options
  • Save jslyonnais/90f9489f176398dcebce98f9cb1e4ec2 to your computer and use it in GitHub Desktop.
Save jslyonnais/90f9489f176398dcebce98f9cb1e4ec2 to your computer and use it in GitHub Desktop.
Upload to Azure via FTP using Powershell Script
# Declare and initialize script variables
$appdirectory = "."
$webappname = "WEB_APP_NAME"
$location = "Central US"
$ressourceGroup = "RESSOURCE_GROUP_NAME"
# Create a resource group, if it does not exist
# Use the -Force flag to avoid getting a prompt asking for confirmation
New-AzureRmResourceGroup -Name $ressourceGroup -Location $location -Force
# Create an App Service plan in 'Free' tier, if it does not exist
# The -ErrorAction SilentlyContinue flag will ensure the script continues even if the service plan already exists
New-AzureRmAppServicePlan -Name $webappname -Location $location -ResourceGroupName $ressourceGroup -Tier Free -ErrorAction SilentlyContinue
# Create a web app, if it does not exist
New-AzureRmWebApp -Name $webappname -Location $location -AppServicePlan $webappname -ResourceGroupName $ressourceGroup -ErrorAction SilentlyContinue
# Get publishing profile for the web app
[xml]$xml = (Get-AzureRmWebAppPublishingProfile -Name $webappname -ResourceGroupName $ressourceGroup -OutputFile null)
# Extract FTP connection information from publishing profile
$username = $xml.SelectNodes("//publishProfile[@publishMethod='FTP']//@userName").value
$password = $xml.SelectNodes("//publishProfile[@publishMethod='FTP']//@userPWD").value
$url = $xml.SelectNodes("//publishProfile[@publishMethod='FTP']//@publishUrl").value
# Create a WebClient object with the required FTP credentials
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($username,$password)
# Get all the files from the specified directory recursively
$files = Get-ChildItem -Path $appdirectory -Recurse | Where-Object{!($_.PSIsContainer)}
# Upload each file to the FTP site
foreach ($file in $files)
{
$relativepath = (Resolve-Path -Path $file.FullName -Relative).Replace(".\", "").Replace('\', '/')
$uri = New-Object System.Uri("$url/$relativepath")
Write-Output "Uploading to " + $uri.AbsoluteUri
$webclient.UploadFile($uri, $file.FullName)
}
# Dispose the WebClient object to free up resources
$webclient.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment