Created
November 22, 2017 03:55
-
-
Save moo2u2/d0224ee54fc281b2fb1bb853f8fdf3e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Copies all of a web app's files to a given slot | |
.DESCRIPTION | |
Copies all of a web app's files to a given slot. Skips "newrelic" folder as the files are in use. | |
.PARAMETER SubscriptionId | |
The subscription id where the resources reside. | |
.PARAMETER ResourceGroupName | |
The resource group where the resources reside. | |
.PARAMETER WebAppName | |
Name of the web app containing files for the slot. | |
.PARAMETER SlotName | |
Name of the slot to fill with files from web app. | |
#> | |
param( | |
[string] | |
$SubscriptionId, | |
[Parameter(Mandatory = $True)] | |
[string] | |
$ResourceGroupName, | |
[Parameter(Mandatory = $false)] | |
[string] | |
$WebAppName, | |
[Parameter(Mandatory = $True)] | |
[string] | |
$SlotName | |
) | |
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Deployment") | |
function Get-AzureRmWebAppPublishingCredentials($ResourceGroupName, $WebAppName, $SlotName = $null){ | |
if ([string]::IsNullOrWhiteSpace($SlotName)) { | |
$resourceType = "Microsoft.Web/sites/config"; | |
$resourceName = "$WebAppName/publishingcredentials"; | |
} | |
else { | |
$resourceType = "Microsoft.Web/sites/slots/config"; | |
$resourceName = "$WebAppName/$SlotName/publishingcredentials"; | |
} | |
$publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $ResourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2016-08-01 -Force; | |
return $publishingCredentials; | |
} | |
function GetScmUrl($ResourceGroupName, $WebAppName, $SlotName) { | |
# revert to this when MS fixes https://social.msdn.microsoft.com/Forums/expression/en-US/938e59f6-6a83-4640-a423-26fe91d66cf3/scm-uri-for-web-app-deployment-slots | |
#$scmUrl = $publishingCredentials.properties.scmUri | |
#$scmUrlNoCreds = $scmUrl.Replace($scmUrl.Substring($scmUrl.IndexOf('$'), ($scmUrl.IndexOf('@')-$scmUrl.IndexOf('$')+1)), '') | |
#$apiUrl = "$scmUrl/api/command" | |
# revert below | |
if($SlotName) { | |
$slot = Get-AzureRmWebAppSlot -ResourceGroupname $ResourceGroupName -Name $WebAppName -Slot $SlotName; | |
$scmUrl = $slot.EnabledHostNames | where { $_.Contains('.scm.') }; | |
} else { | |
$scmUrl = "$WebAppName.scm.azurewebsites.net"; | |
} | |
# revert above | |
return "https://$scmUrl"; | |
} | |
function SyncWebApps($srcUrl, $srcCredentials, $destUrl, $destCredentials) { | |
$syncOptions = New-Object Microsoft.Web.Deployment.DeploymentSyncOptions; | |
#$syncOptions.DoNotDelete = $true; | |
$appOfflineRule = $null; | |
$availableRules = [Microsoft.Web.Deployment.DeploymentSyncOptions]::GetAvailableRules(); | |
if (!$availableRules.TryGetValue('AppOffline', [ref]$appOfflineRule)) { | |
Write-Host "Failed to find AppOffline Rule"; | |
} else { | |
$syncOptions.Rules.Add($appOfflineRule); | |
Write-Host "Enabled AppOffline Rule"; | |
} | |
$skipNewRelic = New-Object Microsoft.Web.Deployment.DeploymentSkipDirective -ArgumentList @("skipNewRelic", 'objectName=dirPath,absolutePath=.*\\newrelic', $true); | |
$sourceBaseOptions = New-Object Microsoft.Web.Deployment.DeploymentBaseOptions; | |
$sourceBaseOptions.ComputerName = $srcUrl + "/msdeploy.axd"; | |
$sourceBaseOptions.UserName = $srcCredentials.properties.PublishingUserName; | |
$sourceBaseOptions.Password = $srcCredentials.properties.PublishingPassword; | |
$sourceBaseOptions.AuthenticationType = "basic"; | |
$sourceBaseOptions.SkipDirectives.Add($skipNewRelic); | |
$destBaseOptions = New-Object Microsoft.Web.Deployment.DeploymentBaseOptions; | |
$destBaseOptions.ComputerName = $destUrl + "/msdeploy.axd"; | |
$destBaseOptions.UserName = $destCredentials.properties.PublishingUserName; | |
$destBaseOptions.Password = $destCredentials.properties.PublishingPassword; | |
$destBaseOptions.AuthenticationType = "basic"; | |
$destBaseOptions.SkipDirectives.Add($skipNewRelic); | |
$destProviderOptions = New-Object Microsoft.Web.Deployment.DeploymentProviderOptions -ArgumentList @("contentPath"); | |
$destProviderOptions.Path = "/site"; | |
$sourceObj = [Microsoft.Web.Deployment.DeploymentManager]::CreateObject("contentPath", "/site", $sourceBaseOptions); | |
$sourceObj.SyncTo($destProviderOptions, $destBaseOptions, $syncOptions); | |
} | |
if($SubscriptionId) { | |
try { | |
Set-AzureRmContext -SubscriptionID $SubscriptionId; | |
} catch { | |
Login-AzureRmAccount; | |
Set-AzureRmContext -SubscriptionID $SubscriptionId; | |
} | |
} | |
$srcCreds = Get-AzureRmWebAppPublishingCredentials $ResourceGroupName $WebAppName; | |
$srcUrl = GetScmUrl $ResourceGroupName $WebAppName; | |
$destCreds = Get-AzureRmWebAppPublishingCredentials $ResourceGroupName $WebAppName $SlotName; | |
$destUrl = GetScmUrl $ResourceGroupName $WebAppName $SlotName; | |
SyncWebApps $srcUrl $srcCreds $destUrl $destCreds; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment