-
Notifications
You must be signed in to change notification settings - Fork 0
/
reroute_traffic_to_slot.ps1
73 lines (56 loc) · 1.52 KB
/
reroute_traffic_to_slot.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
param (
[Parameter(Mandatory=$true)]
[string]$TenantId,
[Parameter(Mandatory=$true)]
[string]$AppClientId,
[Parameter(Mandatory=$true)]
[string]$AppPassword,
[Parameter(Mandatory = $true)]
[string]$ResourceGroup,
[Parameter(Mandatory = $true)]
[string]$Appservice
)
# ===== Create needed Variables from script Parameters
$securePassword = ConvertTo-SecureString $AppPassword -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($AppClientId, $securePassword)
# ===== Connect to Azure
Connect-AzAccount -ServicePrincipal -Credential $credential -Tenant $TenantId
# ===== Utils
function Is-TrafficRuleSet
{
Get-AzWebAppTrafficRouting -ResourceGroupName $ResourceGroup -WebAppName $AppService -RuleName $slotBaseName -erroraction 'silentlycontinue'
}
# ===== Operations
try
{
$slots = Get-AzWebAppSlot -ResourceGroupName $ResourceGroup -Name $AppService -erroraction 'Stop'
}
catch
{
Write-Error '$ResourceGroup or $Appservice does not exist'
exit
}
try
{
$slot = $slots[0]
}
catch
{
Write-Error 'No slot were found on $AppService'
exit
}
$slotBaseName = ($slot.Name -split '/')[1]
if (-not(Is-TrafficRuleSet))
{
echo "traffic rule not set. Setting now..."
Add-AzWebAppTrafficRouting -ResourceGroupName $ResourceGroup -WebAppName $AppService -RoutingRule @{ActionHostName=$slot.DefaultHostName ; ReroutePercentage='100' ; Name=$slotBaseName}
}
if (Is-TrafficRuleSet)
{
echo "Traffic Rule OK."
}
else
{
echo "error setting traffic rule!"
exit
}