This repository was archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 517
Expand file tree
/
Copy pathdeploy-ftp.ps1
More file actions
59 lines (47 loc) · 1.87 KB
/
deploy-ftp.ps1
File metadata and controls
59 lines (47 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
$filePath="<Replace with full file path>"
$webappname="mywebapp$(Get-Random)"
$location="West Europe"
# Create a resource group.
New-AzResourceGroup -Name myResourceGroup -Location $location
# Create an App Service plan in `Free` tier.
New-AzAppServicePlan -Name $webappname -Location $location `
-ResourceGroupName myResourceGroup -Tier Free
# Create a web app.
New-AzWebApp -Name $webappname -Location $location -AppServicePlan $webappname `
-ResourceGroupName myResourceGroup
# Get publishing profile for the web app
$xml = [xml](Get-AzWebAppPublishingProfile -Name $webappname `
-ResourceGroupName myResourceGroup `
-OutputFile null)
# Extract 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
# Upload file
$file = Get-Item -Path $filePath
$uri = New-Object System.Uri("$url/$($file.Name)")
$request = [System.Net.FtpWebRequest]([System.net.WebRequest]::Create($uri))
$request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$request.Credentials = New-Object System.Net.NetworkCredential($username,$password)
# Enable SSL for FTPS. Should be $false if FTP.
$request.EnableSsl = $true;
# Write the file to the request object.
$fileBytes = [System.IO.File]::ReadAllBytes($filePath)
$request.ContentLength = $fileBytes.Length;
$requestStream = $request.GetRequestStream()
try {
$requestStream.Write($fileBytes, 0, $fileBytes.Length)
}
finally {
$requestStream.Dispose()
}
Write-Host "Uploading to $($uri.AbsoluteUri)"
try {
$response = [System.Net.FtpWebResponse]($request.GetResponse())
Write-Host "Status: $($response.StatusDescription)"
}
finally {
if ($null -ne $response) {
$response.Close()
}
}