-
Notifications
You must be signed in to change notification settings - Fork 1
/
iis-web-app.ps1
175 lines (156 loc) · 3.52 KB
/
iis-web-app.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#
# Add/Remove a Web Application in IIS with PowerShell
# This needs to be ran by a user with Admin credentials
#
#requires -version 4.0
#requires -runasadministrator
# Set script params
param([switch]$install, [switch]$uninstall)
<#
# Imports
#>
Import-Module WebAdministration
<#
# Global Vars
#>
$ErrorActionPreference = "Stop"
$pkg = $PWD
$site = "SiteName"
$siteName = "site.mysite.com"
$sitePaths = "E:\Inetpub\sitename", "E:\Logs\sitename"
$siteAppPool = "SiteName"
$siteAppNetCDL = "" #.Net Version leave blank for non managed code
$siteApp32Bit = "False"
$siteAppIdentity = "Local System"
$siteDefaultFile = "login.htm"
$siteVirtualDirs = "vDir1"
$siteVirtualDirPaths = "C:\Path\To\vDir\vDir1"
$siteZip = "$pkg\SiteName.zip"
<#
# Functions
#>
Function expandZipFile($file, $dest)
{
Write-Host("Extracting $file to $dest...")
Add-Type -AssemblyName System.IO.Compression.FileSystem
try
{
[System.IO.Compression.Zipfile]::ExtractToDirectory($file, $dest)
}
catch
{
$_
}
}
Function createFolders($folderPaths)
{
foreach($folder in $folderPaths)
{
try
{
New-Item -Force -Type container $folder
Write-Host("Created: $folder")
}
catch
{
$_
}
}
}
Function removeFolders($folderPaths)
{
foreach($folder in $folderPaths)
{
try
{
Remove-Item -Force $sitePath -Recurse
Write-Host("Removed: $sitePath")
}
catch
{
$_
}
}
}
Function addAppPool($appPool, $appNetCDL, $appPool32Bit, $appPoolIdentity)
{
Write-Host("Created AppPool: $appPool")
try
{
$ap = New-Item IIS:\AppPools\$appPool
$ap | Set-ItemProperty -Name "managedRuntimeVersion" -Value $appNetCDL
$ap | Set-ItemProperty -Name "enable32BitAppOnWin64" -Value $appPool32Bit
$ap | Set-ItemProperty -Name "processModel.identityType" -Value $appPoolIdentity
}
catch
{
$_
}
}
Function removeAppPool($appPool)
{
try
{
Remove-WebAppPool $appPool
Write-Host("Removed AppPool: $appPool")
}
catch
{
$_
}
}
Function addWebSite($site, $path, $name, $appPool, $logPath, $defaultFile)
{
try
{
$ws = New-Item IIS:\Sites\$site -PhysicalPath $path -bindings @{protocol="http";bindingInformation=":80:$name"}
$ws | Set-ItemProperty -name applicationPool -value $appPool
$ws | Set-ItemProperty -name logFile -value @{directory=$logPath}
Add-WebConfiguration //defaultDocument/files "IIS:\sites\$site" -atIndex 0 -Value @{value=$defaultFile}
Write-Host("Added Website: $site")
}
catch
{
$_
}
}
Function removeWebSite($site)
{
try
{
Remove-Item IIS:\Sites\$site -Recurse
Write-Host("Removed Website: $site")
}
catch
{
$_
}
}
Function addVirtDir($site, $vDir, $path)
{
Try
{
New-Item IIS:\Sites\$site\$vdir -physicalPath $path -type VirtualDirectory
}
catch
{
$_
}
}
<#
# Main
#>
if ($install)
{
createFolders $sitePaths
expandZipFile $siteZip $sitePaths[0]
addAppPool $siteAppPool $siteAppNetCDL $siteApp32Bit $siteAppIdentity $siteAppIdentityPass
addWebSite $site $sitePath[0] $siteName $siteAppPool $siteLogPath $siteDefaultFile
addVirtDir $site $siteVirtualDirs $siteVirtualDirPaths
}
elseif ($uninstall)
{
removeAppPool $siteAppPool
removeWebSite $site
removeFolders $sitePaths
}