forked from 15below/Ensconce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateiis6app.ps1
357 lines (302 loc) · 11.4 KB
/
createiis6app.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
$frameworkPath = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client").InstallPath
set-alias regIIS $frameworkPath\aspnet_regiis.exe
function TestInclude ([string]$name) {
(get-host).ui.rawui.foregroundcolor= "Magenta"
$name
(get-host).ui.rawui.foregroundcolor= "Yellow"
}
function CheckIfAppPoolExists ([string]$name)
{
$tempPool = gwmi -namespace "root\MicrosoftIISv2" -class "IISApplicationPoolSetting" -filter "Name like '%$name%'"
$tempPool -ne $NULL
}
function CheckIfWebSiteExists ([string]$name)
{
$tempWebsite = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '%$name%'"
$tempWebsite -ne $NULL
}
function CheckIfWebApplicationExists ([string]$webSite, [string]$appName)
{
$tempWebsite = (gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '%$webSite%'")
$tempApp = (gwmi -namespace "root\MicrosoftIISv2" -class "IISWebDirectory" | where {$_.name -like "$tempWebSite/*$appName" })
if ($tempApp -ne $NULL)
{
$tempApp.AppGetStatus().returnvalue -ne 2
}
else
{ $False }
}
function CheckIfVirtualDirectoryExists ([string]$webSite, [string]$virtualDir)
{
$iis = [ADSI]"IIS://localhost/W3SVC"
$webServer = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND $_.ServerComment -eq $website }
$webVirtualDir = ($webServer.children | where { $_.keyType -eq "IIsWebVirtualDir" }).children | where { $_.keyType -eq "IIsWebVirtualDir" -and $_.Name -eq $virtualDir}
($webVirtualDir -ne $null)
}
function CheckIfSslBindingExists ([string]$webSite, [string]$hostHeader)
{
$true
}
function CreateAppPool ([string]$name)
{
# check if pool exists and delete it - for testing purposes
" Creating ISS app pool for " + $name
$tempPool = gwmi -namespace "root\MicrosoftIISv2" -class "IISApplicationPoolSetting" -filter "Name like '%$name%'"
if (($tempPool -eq $NULL)) {
# create Application Pool
$appPoolSettings = [wmiclass] "root\MicrosoftIISv2:IISApplicationPoolSetting"
$newPool = $appPoolSettings.CreateInstance()
$newPool.Name = "W3SVC/AppPools/" + $name
$newPool.PeriodicRestartTime = 1740
$newPool.IdleTimeout = 20
$newPool.MaxProcesses = 1
$newPool.AppPoolIdentityType = 3
$newPool.Put()
}
}
function SetAppPoolIdentity([string]$name, [string]$user, [string]$password)
{
$appPool = gwmi -namespace "root\MicrosoftIISv2" -class "IISApplicationPoolSetting" -filter "Name like '%$name%'"
# LocalSystem = 0
# LocalService = 1
# NetworkService = 2
# SpecificUser = 3
# ApplicationPoolIdentity = 4
$identityType = 3
if($user -And $user.ToLower() -eq "networkservice")
{
$identityType = 2
}
else
{
$appPool.WAMUserName = $user
$appPool.WAMUserPass = $password
# Only add user to group if its a specific user, dont do it for networkservice
AddUserToGroup $user "IIS_WPG"
}
$appPool.AppPoolIdentityType = $identityType
$appPool.Put()
}
function StopAppPool([string]$name)
{
$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" | where-object {$_.Name -eq "W3SVC/AppPools/$name"}
if ($appPool)
{
"Stopping AppPool: " + $name | Write-Host
$appPool.Stop()
}
}
function StartAppPool([string]$name)
{
$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" | where-object {$_.Name -eq "W3SVC/AppPools/$name"}
if ($appPool)
{
"Starting AppPool: " + $name | Write-Host
$appPool.Start()
}
}
function RestartAppPool([string]$name)
{
$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" | where-object {$_.Name -eq "W3SVC/AppPools/$name"}
if ($appPool)
{
"Restarting AppPool: " + $name | Write-Host
$appPool.Stop()
$appPool.Start()
}
}
function StopWebSite([string]$name)
{
"Stopping WebSite: " + $name | Write-Host
$iis = [ADSI]"IIS://localhost/W3SVC"
$website = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND $_.ServerComment -eq $name }
if ($website -ne $NULL)
{
$website.serverState = 4
$website.setInfo()
}
}
function StartWebSite([string]$name)
{
"Starting WebSite: " + $name | Write-Host
$iis = [ADSI]"IIS://localhost/W3SVC"
$website = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND $_.ServerComment -eq $name }
if ($website -ne $NULL)
{
$website.serverState = 2
$website.setInfo()
}
}
function CreateWebSite ([string]$name, [string]$localPath, [string] $appPoolName, [string] $applicationName, [string] $hostName, [string] $logLocation)
{
# check if web site exists and delete it - for testing purposes
" Creating IIS website for " + $name
$tempWebsite = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '%$name%'"
if (($tempWebsite -eq $NULL)) {
$iisWebService = gwmi -namespace "root\MicrosoftIISv2" -class "IIsWebService"
$bindingClass = [wmiclass]'root\MicrosoftIISv2:ServerBinding'
$bindings = $bindingClass.CreateInstance()
$bindings.Port = "80"
$bindings.Hostname = $hostname
EnsurePath $localPath
$NewSite = $iisWebService.CreateNewSite($name, $bindings, $localPath)
$webServerSettings = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '%$name%'"
$iis = [ADSI]"IIS://localhost/W3SVC"
$webServer = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND $_.ServerComment -eq $name }
$webserver.AspEnableParentPaths = $True
$webserver.LogFileDirectory = $logLocation
$webServer.Properties["AccessFlags"].Value = 513
$webServer.Properties["AuthFlags"].Value = 1
$webServer.DefaultDoc = "index.asp," + $webServer.DefaultDoc
$webServer.AppPoolID = $appPoolName
$webserver.SetInfo()
$webVirtualDir = $webServer.children | where { $_.keyType -eq "IIsWebVirtualDir" }
# Set Application name
$webVirtualDir.AppFriendlyName = $applicationName
# Save changes
$webServer.CommitChanges()
$webVirtualDir.CommitChanges()
# Switch the Website to .NET 4.0
$webServerSettings.Name
regiis -s $webServerSettings.Name
# Start the newly created web site
if (!($webServer -eq $NULL)) {$webServer.start()}
}
}
function AddHostHeader([string]$siteName, [string] $hostHeader, [int] $port, [string] $protocol)
{
$iis = [ADSI]"IIS://localhost/W3SVC"
$site = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND $_.ServerComment -eq $siteName }
if($site -ne $null)
{
$webBinding = $site.ServerBindings | where { $_.Port -eq $port -AND $_.Hostname -eq $hostHeader -AND $_.IP -eq "*" }
if($webBinding -eq $null) {
if( $hostHeader -eq "" ) {
"Host-header is empty, cannot add" | Write-Host
}
else {
"Adding additional host-header binding of: $hostHeader, port: $port" | Write-Host
"Ignoring protocol of: '$protocol' - IIS6 does not have this concept "| Write-Host
$site.ServerBindings.Insert($site.ServerBindings.Count, ":$port:$hostHeader")
}
}
else {
"Http host header already exists - no need to add" | Write-Host
}
}
}
function CreateWebApplication([string]$webSite, [string]$appName, [string] $appPool, [string]$InstallDir ,[string]$subFolders)
{
EnsurePath $InstallDir
$webServerSettings = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '%$webSite%'"
if ($subFolders -eq $null -or $subFolders -eq "" )
{
$dirSettings = [wmiclass] "root\MicrosoftIISv2:IIsWebDirectory"
$newDir = $dirSettings.CreateInstance()
$newDir.Name = ($webServerSettings.Name + '/ROOT/' + $appName)
$newDir.Description = $appPool
$newDir.Put()
} else {
$virtualDirName = $subFolders +"\" + $appName
CreateVirtualDirectory $webSite $virtualDirName $installDir
$nvdir = ($webServerSettings.Name + '/ROOT/' + $virtualDirName)
$nvdir = $nvdir.Replace("\", "/")
$newDir = gwmi -namespace "root\microsoftiisv2" -Class "IIsWebVirtualDir" -filter "Name='$nvdir'"
}
$newDir.AppCreate3(2, $appPool, $True)
}
function CreateVirtualDirectory([string]$webSite, [string]$virtualDir, [string]$installDir)
{
"Creating $virtualDir pointing at $installDir" | Write-Host
$webServerSettings = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '%$webSite%'"
$virtualDirSettings = [wmiclass] "root\MicrosoftIISv2:IIsWebVirtualDirSetting"
$virtualDirName = $virtualDir
if ($virtualDirName.StartsWith("\")) {
$virtualDirName = $VirtualDirName.substring(1)
}
$newVDir = $virtualDirSettings.CreateInstance()
$newVDir.Name = ($webServerSettings.Name + '/ROOT/' + $virtualDirName)
$newVDir.Path = $installDir
$newVDir.Put();
}
function AddSslCertificate ([string] $websiteName, [string] $friendlyName, [string] $hostHeader)
{
# This method requires for you to have selfssl on your machine
$selfSslPath = "\program files\iis resources\selfssl"
if (Test-Path $selfSslPath)
{
$certificateCommonName = "/N:cn=" + $friendlyName
$certificateValidityDays = "/V:3650"
$websitePort = "/P:443"
$addToTrusted = "/T"
$quietMode = "/Q"
$webServerSetting = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '$websiteName'"
$websiteId ="/S:" + $webServerSetting.name.substring($webServerSetting.name.lastindexof('/')+1)
cd -path $selfSslPath
.\selfssl.exe $addToTrusted $friendlyName $certificateValidityDays $websitePort $websiteId $quietMode
}
else
{
"Didn't add SSL ass SelfSSL was not installed on this machine!" | Write-Host
}
}
function EnableWebDav ([string] $websiteName)
{
"EnableWebDav is not supported for IIS6" | Write-Host
}
function AddAuthoringRule ([string] $websiteName, [string] $userName, [string] $access)
{
"AddAutoringRole is not supported for IIS6" | Write-Host
}
function AddWildcardMap ([string] $websiteName, [string]$subFolders)
{
$iis = [ADSI]"IIS://localhost/W3SVC"
$webServer = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND $_.ServerComment -eq $websiteName }
$webVirtualDir = $webServer.children | where { $_.keyType -eq "IIsWebVirtualDir" }
if ($subFolders -ne $null -and $subFolders -ne "" )
{
$folders = $subFolders.split("\")
foreach ($folder in $Folders)
{
$webVirtualDir = ($webVirtualDir.Children | where {$_.name -like $Folder })
}
}
if ($webVirtualDir -ne $null)
{
$check = $webVirtualDir.ScriptMaps | where {$_.EndsWith(", All")}
if ($check -eq $null)
{
# Add wildcard map
$wildcardMap = "*,C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll, 0, All"
$webVirtualDir.ScriptMaps.Add($wildcardMap)
$webVirtualDir.CommitChanges()
}
}
}
function AddDefaultDocument ([string] $websiteName, [string] $defaultDocumentName)
{
$iis = [ADSI]"IIS://localhost/W3SVC"
$webServer = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND $_.ServerComment -eq $websiteName }
if ($webServer.DefaultDoc.Contains($defaultDocumentName) -eq $false)
{
"Adding $defaultDocumentName as a default document of $websiteName" | Write-Host
$webServer.DefaultDoc = "index.asp," + $webServer.DefaultDoc
$webServer.CommitChanges()
}
}
function EnableParentPaths ([string] $websiteName)
{
"EnableParentPaths is set during site creation for IIS6" | Write-Host
}
function Enable32BitApps ([string] $appPoolName)
{
"The Enable32BitApps function does nothing in IIS6" | Write-Host
}
function DefaultApplicationPoolGroup ()
{
"IIS_WPG"
}
function SetMaxRequestEntityAllowed([string] $websiteName, [int] $maxRequestEntityAllowedValue)
{
"The SetMaxRequestEntityAllowed function does nothing in IIS6" | Write-Host
}