-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-website-hostname.ps1
71 lines (58 loc) · 1.75 KB
/
add-website-hostname.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
<#
.SYNOPSIS
Add website hostname.
.DESCRIPTION
Add website hostname.
.EXAMPLE
.\add-website-hostname.ps1 -WebSiteName "myWebSiteName" -HostName "www.goto10.cz"
#>
param(
[CmdletBinding(SupportsShouldProcess=$true)]
# The webSite Name you want to create
[Parameter(Mandatory = $true)]
[string]$WebSiteName,
# Host name
[Parameter(Mandatory = $true)]
[string]$HostName
)
#$VerbosePreference = "Continue"
# Check if Windows Azure Powershell is avaiable
if ((Get-Module -ListAvailable Azure) -eq $null)
{
throw "Windows Azure Powershell not found! Please install from http://www.windowsazure.com/en-us/downloads/#cmd-line-tools"
}
# Check if there is already a login session in Azure Powershell
Try
{
$ctx = Get-AzureRmContext -ErrorAction Continue
}
Catch [System.Management.Automation.PSInvalidOperationException]
{
Login-AzureRmAccount
}
# Find the website
$website = Get-AzureWebsite | Where-Object {$_.Name -eq $WebSiteName }
if ($website -eq $null)
{
throw "Website '$WebSiteName' not found."
}
else
{
$newHosts = [System.Collections.ArrayList]@()
$hosts = $website.HostNames
Write-Host "Current hostnames for website '$($WebSiteName)':"
foreach($h in $hosts) {
# We need to remove "default" $WebSiteName.azurewebsites.net host or
# setting of HostNames will fail with no result!
if (!$h.EndsWith('.azurewebsites.net')) {
$n = $newHosts.Add($h)
}
$n++;
Write-Host "$n. $h"
}
$n = $newHosts.Add($HostName)
$n += 2;
Write-Host "Adding $n. hostname: $HostName"
Set-AzureWebsite -Name $WebSiteName -HostNames $newHosts
}
Write-Host "Complete!"