-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add-AzWebAppFile.ps1
78 lines (65 loc) · 2.44 KB
/
Add-AzWebAppFile.ps1
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env pwsh
function Add-AzWebAppFile {
<#
.Description
Uploads files to Azure App Service application file system.
#>
[CmdletBinding()]
param (
# The name of the resource group containing the app service.
[Parameter(Mandatory=$true, Position=0)]
[string]
$ResourceGroupName,
# The name of the webapp
[Parameter(Mandatory=$true, Position=1)]
[string]
$WebAppName,
# The name of the webapp slot
[Parameter(Mandatory=$false, Position=2)]
[string]
$SlotName,
# Target Directory, where "/" is "/home" on the App Service file system.
[Parameter(Mandatory=$true, Position=3)]
[string]
$TargetDirectory,
# Files to upload
[Parameter(Mandatory=$true,
Position=4,
ValueFromPipeline=$true,
HelpMessage="Path to one or more locations.")]
[Alias("PSPath")]
[ValidateNotNullOrEmpty()]
[string]
$File,
# Overwrite if a file already existss
[Parameter()]
[switch]
$Force
)
begin {
if ($SlotName){
$target = Get-AzWebAppSlotPublishingProfile -ResourceGroupName $ResourceGroupName -Name $WebAppName -Slot $SlotName -Format ftp
} else {
$target = Get-AzWebAppPublishingProfile -ResourceGroupName $ResourceGroupName -Name $WebAppName -Format ftp
}
if (!$target) {
throw "Unable to obtain publishing profile."
}
$publishProfile=[xml]$target | Select-Object -ExpandProperty publishData | Select-Object -ExpandProperty publishProfile | Where-Object publishMethod -eq 'MSDeploy'
$credential=[PSCredential]::new("$($publishProfile.userName)", (ConvertTo-SecureString -AsPlainText $publishProfile.userPWD -Force))
$apiLocation="https://$($publishProfile.publishUrl)/api/vfs"
}
process {
$item = Get-Item $File
if ($item -is [System.IO.FileInfo]) {
Write-Host "Uploading $($item.Name)"
$headers=@{}
if ($Force) {
$headers['If-Match'] ='*'
}
Invoke-RestMethod -Credential $credential `
-Authentication Basic -Method PUT -InFile $File `
-Uri "${apiLocation}/$($TargetDirectory.TrimStart('/'))/$($item.Name)" -Headers $headers > $null
}
}
}