This repository has been archived by the owner on Aug 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Provision.ps1
68 lines (51 loc) · 1.74 KB
/
Provision.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
#Requires -RunAsAdministrator
$siteName = "Speedy"
$dnsname = "speedy.local"
$cert = Get-ChildItem cert:\LocalMachine\Root -Recurse | Where { $_.FriendlyName -eq $dnsname }
if ($cert -eq $null -or $cert.NotAfter -le [DateTime]::UtcNow)
{
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store "root", "LocalMachine"
$store.Open("ReadWrite")
if ($cert -ne $null)
{
Write-Host "Removing old certificate to root"
$store.Remove($cert)
}
Write-Host "Adding new self signed certificate to root"
$cert = New-SelfSignedCertificate -FriendlyName $dnsname -KeyFriendlyName $dnsname -Subject $dnsname -DnsName $dnsname
$store.Add($cert)
$store.Close()
$cert.GetCertHashString()
}
#
# Provision the website
#
Import-Module WebAdministration
$site = Get-Website $siteName
$sitePath = "C:\inetpub\$siteName"
if ($site -eq $null)
{
New-Item $sitePath -ItemType Directory -ErrorAction Ignore | Out-Null
New-Website -Name $siteName -PhysicalPath $sitePath
$site = Get-Website $siteName
$webPath = "IIS:\Sites\$siteName"
$bindings = @()
$bindings += @{ protocol="http"; bindingInformation="*:80:$dnsname"}
$bindings += @{ protocol="https"; bindingInformation="*:443:$dnsname"; sslFlags=1 }
Set-ItemProperty -Path $webPath -Name Bindings -Value $bindings
$binding = Get-WebBinding -Name $siteName -Protocol "https"
$binding.AddSslCertificate($cert.GetCertHashString(), "root")
$pool = Get-Item "IIS:\AppPools\$siteName" -ErrorAction Ignore
if ($pool -eq $null)
{
New-WebAppPool -Name $siteName
$pool = Get-Item IIS:\AppPools\$siteName
}
Set-ItemProperty IIS:\Sites\$siteName -Name applicationPool -Value $siteName
Start-WebAppPool -Name $siteName
$site.Start()
}
else
{
Write-Host "Website already created"
}