-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetupWebapplication.ps1
144 lines (118 loc) · 5.94 KB
/
SetupWebapplication.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
$CustomServerName = Read-Host -Prompt "Use sql express? [y/n]"
if ($CustomServerName -eq "n") {
$TargetserverName = Read-Host -Prompt "Input database server name (eg. localhost\sqlexpress)"
}
else {
$TargetserverName = "localhost\sqlexpress"
}
######################################################################
####################### GET SITE CONFIG ##############################
######################################################################
Write-Host "Get config"
$iisconfig = Get-Content "$PSScriptRoot\iis-config.json" | Out-String | ConvertFrom-Json
$IISWebsiteName = $iisconfig."iisWebsiteName"
$ApppoolNetVersion = $iisconfig."apppoolNetVersion"
$DatabaseName = $IISWebsiteName
$WebsiteUrl = "$IISWebsiteName".ToLower() + ".local"
######################################################################
####################### ADD IIS BINDING ##############################
######################################################################
Import-Module WebAdministration
Write-Host "Create Apppool"
if (!(Test-Path IIS:\AppPools\$IISWebsiteName -pathType container)) {
$AppPool = New-WebAppPool $IISWebsiteName
$AppPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $ApppoolNetVersion
}
Write-Host "Create Website"
if (!(Get-Website -Name "$IISWebsiteName")) {
New-Website -Name $IISWebsiteName -PhysicalPath $PSScriptRoot -ApplicationPool $IISWebsiteName -HostHeader $WebsiteUrl
if (!(Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.Subject -eq "CN=$WebsiteUrl" })) {
New-SelfSignedCertificate -DnsName "$WebsiteUrl" -CertStoreLocation "cert:\LocalMachine\My"
}
New-WebBinding -Name $IISWebsiteName -Protocol "https" -Port 443 -IPAddress * -HostHeader $WebsiteUrl -SslFlags 1
$Thumbprint = (Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.Subject -eq "CN=$WebsiteUrl" }).Thumbprint;
$Cert = (Get-ChildItem -Path "cert:\LocalMachine\My\$Thumbprint")
if (!(Get-ChildItem -Path Cert:\LocalMachine\Root | Where-Object { $_.Thumbprint -eq $Thumbprint })) {
$DestStore = new-object System.Security.Cryptography.X509Certificates.X509Store([System.Security.Cryptography.X509Certificates.StoreName]::Root, "localmachine")
$DestStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$DestStore.Add($Cert)
$DestStore.Close()
}
(Get-WebBinding -Name $IISWebsiteName -Port 443 -Protocol "https" -HostHeader $WebsiteUrl).AddSslCertificate($Thumbprint, "my")
}
######################################################################
####################### UPDATE HOSTS FILE ############################
######################################################################
Write-Host "Update hostsfile"
If ((Get-Content "$($env:windir)\system32\Drivers\etc\hosts" ) -notcontains "127.0.0.1 $WebsiteUrl")
{ Add-Content "$($env:windir)\system32\Drivers\etc\hosts" "`n127.0.0.1 $WebsiteUrl" }
######################################################################
####################### IMPORT DATABASE ##############################
######################################################################
Write-Host "Import databse"
$RootDir = (Get-Item $PSScriptRoot).parent.parent.parent.FullName
$BackupDirectory = "$RootDir\bak"
$files = Get-ChildItem -Path $BackupDirectory\*.bacpac | Sort-Object LastAccessTime -Descending | Select-Object
$i = 0
Write-Host "Select database to import"
foreach ($f in $files) {
$i++
Write-Host "$i` : $f"
}
do {
$SelectedIndex = (Read-Host -Prompt "Pick a number").ToInt32($null)
}
while ($SelectedIndex -notin (1..$i))
$bacpacFile = $files[$SelectedIndex - 1]
$file = "$bacpacFile";
$DropDatabaseQuery =
@"
IF EXISTS (SELECT * FROM [sys].[databases] WHERE [name] = '$DatabaseName') DROP DATABASE [$DatabaseName]
"@
Write-Host "Drop existing"
SQLCMD -S $TargetserverName -E -Q $DropDatabaseQuery
Write-Host "Import bacpac"
SqlPackage.exe /a:IMPORT /sf:$file /tdn:$DatabaseName /tsn:$TargetServerName
Write-Host "Create user"
$CreateUserQuery =
@"
IF EXISTS (SELECT * FROM [sys].[databases] WHERE [name] = '$DatabaseName')
BEGIN
IF NOT EXISTS (SELECT * FROM [sys].[server_principals] WHERE [name] = 'IIS APPPOOL\$IISWebsiteName')
CREATE LOGIN [IIS APPPOOL\$IISWebsiteName] FROM WINDOWS WITH DEFAULT_DATABASE = [$DatabaseName]
IF NOT EXISTS (SELECT * FROM [$DatabaseName].[sys].[sysusers] WHERE [name] = 'IIS APPPOOL\$IISWebsiteName')
BEGIN
USE [$DatabaseName]
CREATE USER [IIS APPPOOL\$IISWebsiteName] FROM LOGIN [IIS APPPOOL\$IISWebsiteName]
WITH DEFAULT_SCHEMA = [dbo]
ALTER ROLE [db_owner] ADD MEMBER [IIS APPPOOL\$IISWebsiteName];
END
END
"@
Write-Host "Create user"
SQLCMD -S $TargetserverName -E -Q $CreateUserQuery
######################################################################
####################### SET FOLDER PERMISSIONS #######################
######################################################################
Write-Host "Set folder permissions"
$ACL = Get-Acl $PSScriptRoot
$Entry = "IIS APPPOOL\$IISWebsiteName", "Modify", "ContainerInherit,ObjectInherit", "None", "Allow"
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($Entry)
$ACL.SetAccessRule($AccessRule)
Set-Acl -Path $PSScriptRoot -AclObject $ACL
######################################################################
####################### IMPORT MEDIA #################################
######################################################################
$CustomMediaName = Read-Host -Prompt "Use default media folder? [y/n]"
if ($CustomMediaName -eq "n") {
$TargetMediaName = Read-Host -Prompt "Input media folder name"
}
else {
$TargetMediaName = "media"
}
$Context = New-AzStorageContext -Local
$Now = Get-Date
$StorageAccountUri = New-AzStorageContainerSASToken -Name $TargetMediaName -Permission rwdl -ExpiryTime $Now.AddDays(1.0) -Context $Context -FullUri
$MediaDirectory = "$RootDir/media/*"
AzCopy copy "$MediaDirectory" "$StorageAccountUri" --overwrite=prompt --from-to=LocalBlob --recursive