-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiis-create-http-binding.ps1
87 lines (68 loc) · 3.03 KB
/
iis-create-http-binding.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
79
80
81
82
83
84
# --------------------------------------------------------------------------------------
## Input
## --------------------------------------------------------------------------------------
param(
[Parameter(Mandatory=$True)][string]$WebSiteName,
[Parameter(Mandatory=$True)][string]$IPAddress,
[Parameter(Mandatory=$True)][string]$port,
[Parameter(Mandatory=$True)][string]$HostName
)
# Installation
#---------------
Import-Module WebAdministration
$Name = $WebSiteName
$Protocol = "http"
$SSL_Certificae = ""
## --------------------------------------------------------------------------------------
## Helpers
## --------------------------------------------------------------------------------------
# Helper for validating input parameters
function Validate-Parameter([string]$foo, [string[]]$validInput, $parameterName) {
echo "${parameterName}: $foo"
if (! $foo) {
throw "No value was set for $parameterName, and it cannot be empty"
}
if ($validInput) {
if (! $validInput -contains $foo) {
throw "'$input' is not a valid input for '$parameterName'"
}
}
}
## --------------------------------------------------------------------------------------
## Configuration
## --------------------------------------------------------------------------------------
Validate-Parameter $Name -parameterName "Name"
Validate-Parameter $Protocol -parameterName "Protocol"
Validate-Parameter $Port -parameterName "Port"
Validate-Parameter $HostName -parameterName "HostName"
Add-PSSnapin WebAdministration -ErrorAction SilentlyContinue
Import-Module WebAdministration -ErrorAction SilentlyContinue
## --------------------------------------------------------------------------------------
## Run
## --------------------------------------------------------------------------------------
$BindingString = ("*:" + $Port + ":" + $HostName)
echo($BindingString);
if ( (get-WebBinding -Name $Name | Where-Object {$_.Protocol -eq $Protocol -and $_.bindingInformation -like $BindingString} ).Count -eq 1 ) {
echo "Removing WebBinding '$BindingString'"
Get-WebBinding -Name $Name | Where-Object {$_.Protocol -eq $Protocol -and $_.bindingInformation -like $BindingString} | Remove-WebBinding
echo "Removed WebBinding '$BindingString'"
}
else {
echo "Binding does not exist. Creating"
}
#Create Web Bindin
if ($Protocol -eq "http" -or $Protocol -eq "https") {
New-WebBinding -Name $Name -Protocol $Protocol -IPAddress $IPAddress -Port $Port -HostHeader $HostName
if ($Protocol -eq "https") {
#Binding Certificate to https
echo ("Creating HTTPS Binding")
Get-ChildItem 'IIS:\SslBindings\' | Where-Object {$_.Sites -eq $Name } | Remove-Item
echo("Binding to certificate " + $SSL_Certificae)
$Path = "IIS:\SslBindings\0.0.0.0!" + $Port
Get-ChildItem -Path Cert:\LocalMachine\My | where-Object {$_.Thumbprint -eq $SSL_Certificae} | new-item -path $Path -Force
}
}
else {
echo "Protocol is not http or https"
exit -5
}