-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateAppPool.ps1
More file actions
172 lines (164 loc) · 7.73 KB
/
Copy pathCreateAppPool.ps1
File metadata and controls
172 lines (164 loc) · 7.73 KB
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
##https://github.com/OctopusDeploy/Library/blob/master/step-templates/iis-apppool-create.json
function Validate-Parameter {
Param(
[Parameter(Position=0)][string]$Parameter,
[Parameter(Mandatory=$true, Position=1)][string]$ParameterName
)
if (!$ParameterName -contains 'Password') {
Write-Host ('{0}: {1}' -f ${ParameterName},$Parameter)
}
if (!$Parameter) {
Write-Error ('No value was set for {0}, and it cannot be empty' -f $ParameterName)
}
}
function Execute-Retry {
Param(
[Parameter(Mandatory=$true, Position=0)][ScriptBlock]$Command
)
$attemptCount = 0
$operationIncomplete = $true
$maxFailures = 5
$sleepBetweenFailures = Get-Random -minimum 1 -maximum 4
while ($operationIncomplete -and $attemptCount -lt $maxFailures) {
$attemptCount = ($attemptCount + 1)
if ($attemptCount -ge 2) {
Write-Output ('Waiting for {0} seconds before retrying ...' -f $sleepBetweenFailures)
Start-Sleep -s $sleepBetweenFailures
Write-Output 'Retrying ...'
}
try {
& $Command
$operationIncomplete = $false
} catch [System.Exception] {
if ($attemptCount -lt ($maxFailures)) {
Write-Output ('Attempt {0} of {1} failed: {2}' -f $attemptCount,$maxFailures,$_.Exception.Message)
}
else {
Write-Host 'Failed to execute command'
}
}
}
}
function Get-ScheduledTimes {
Param(
[Parameter(Position=0)][string]$Schedule
)
if (!$Schedule) {
return @()
}
$minutes = $Schedule.Split(',')
$minuteArrayList = New-Object System.Collections.ArrayList(,$minutes)
return $minuteArrayList
}
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.Web.Administration')
Add-PSSnapin WebAdministration -ErrorAction SilentlyContinue
Import-Module WebAdministration -ErrorAction SilentlyContinue
$appPoolName = $(AppPool.Name)
$appPoolIdentityType = $(AppPool.IdentityType)
if ($appPoolIdentityType -eq 3) {
$appPoolIdentityUser = $(AppPool.Identity.User)
$appPoolIdentityPassword = $(AppPool.Identity.Password)
}
$appPoolLoadUserProfile = [boolean]::Parse($(AppPool.LoadUserProfile))
$appPoolAutoStart = [boolean]::Parse($(AppPool.AutoStart))
$appPoolEnable32BitAppOnWin64 = [boolean]::Parse($(AppPool.Enable32BitAppOnWin64))
$appPoolManagedRuntimeVersion = $(AppPool.ManagedRuntimeVersion)
$appPoolManagedPipelineMode = $(AppPool.ManagedPipelineMode)
$appPoolIdleTimeout = [TimeSpan]::FromMinutes($(AppPool.IdleTimeoutMinutes))
$appPoolPeriodicRecycleTime = $(AppPool.PeriodicRecycleTime)
$appPoolMaxProcesses = [int]$(AppPool.MaxProcesses)
$appPoolRegularTimeInterval = [TimeSpan]::FromMinutes($(AppPool.RegularTimeInterval))
$appPoolQueueLength = [int]$(AppPool.QueueLength)
$appPoolStartMode = $(AppPool.StartMode)
$appPoolCpuAction = $(AppPool.CpuLimitAction)
$appPoolCpuLimit = [int]$(AppPool.CpuLimit)
Validate-Parameter -Parameter $appPoolName -ParameterName 'Application Pool Name'
Validate-Parameter -Parameter $appPoolIdentityType -ParameterName 'Identity Type'
if ($appPoolIdentityType -eq 3) {
Validate-Parameter -Parameter $appPoolIdentityUser -ParameterName 'Identity UserName'
# If using Group Managed Serice Accounts, the password should be allowed to be empty
}
Validate-Parameter -Parameter $appPoolLoadUserProfile -parameterName 'Load User Profile'
Validate-Parameter -Parameter $appPoolAutoStart -ParameterName 'AutoStart'
Validate-Parameter -Parameter $appPoolEnable32BitAppOnWin64 -ParameterName 'Enable 32-Bit Apps on 64-bit Windows'
Validate-Parameter -Parameter $appPoolManagedRuntimeVersion -ParameterName 'Managed Runtime Version'
Validate-Parameter -Parameter $appPoolManagedPipelineMode -ParameterName 'Managed Pipeline Mode'
Validate-Parameter -Parameter $appPoolIdleTimeout -ParameterName 'Process Idle Timeout'
Validate-Parameter -Parameter $appPoolMaxProcesses -ParameterName 'Maximum Worker Processes'
Validate-Parameter -Parameter $appPoolStartMode -parameterName 'Start Mode'
Validate-Parameter -Parameter $appPoolCpuAction -parameterName 'CPU Limit Action'
Validate-Parameter -Parameter $appPoolCpuLimit -parameterName 'CPU Limit (percent)'
$iis = (New-Object Microsoft.Web.Administration.ServerManager)
$pool = $iis.ApplicationPools | Where-Object {$_.Name -eq $appPoolName} | Select-Object -First 1
if ($pool -eq $null) {
Write-Output ('Creating Application Pool {0}' -f $appPoolName)
Execute-Retry {
$iis = (New-Object Microsoft.Web.Administration.ServerManager)
$iis.ApplicationPools.Add($appPoolName)
$iis.CommitChanges()
}
}
else {
Write-Output ('Application Pool {0} already exists, reconfiguring ...' -f $appPoolName)
}
$list = Get-ScheduledTimes -Schedule $appPoolPeriodicRecycleTime
Execute-Retry {
$iis = (New-Object Microsoft.Web.Administration.ServerManager)
$pool = $iis.ApplicationPools | Where-Object {$_.Name -eq $appPoolName} | Select-Object -First 1
Write-Output ('Setting: AutoStart = {0}' -f $appPoolAutoStart)
$pool.AutoStart = $appPoolAutoStart
Write-Output ('Setting: Enable32BitAppOnWin64 = {0}' -f $appPoolEnable32BitAppOnWin64)
$pool.Enable32BitAppOnWin64 = $appPoolEnable32BitAppOnWin64
Write-Output ('Setting: IdentityType = {0}' -f $appPoolIdentityType)
$pool.ProcessModel.IdentityType = $appPoolIdentityType
if ($appPoolIdentityType -eq 3) {
Write-Output ('Setting: UserName = {0}' -f $appPoolIdentityUser)
$pool.ProcessModel.UserName = $appPoolIdentityUser
if (!$appPoolIdentityPassword) {
Write-Output ('Setting: Password = [empty]')
}
else {
Write-Output ('Setting: Password = [Omitted For Security]')
}
$pool.ProcessModel.Password = $appPoolIdentityPassword
}
Write-Output ('Setting: LoadUserProfile = {0}' -f $appPoolLoadUserProfile)
$pool.ProcessModel.LoadUserProfile = $appPoolLoadUserProfile
Write-Output ('Setting: ManagedRuntimeVersion = {0}' -f $appPoolManagedRuntimeVersion)
if ($appPoolManagedRuntimeVersion -eq 'No Managed Code') {
$pool.ManagedRuntimeVersion = ''
}
else {
$pool.ManagedRuntimeVersion = $appPoolManagedRuntimeVersion
}
Write-Output ('Setting: ManagedPipelineMode = {0}' -f $appPoolManagedPipelineMode)
$pool.ManagedPipelineMode = $appPoolManagedPipelineMode
Write-Output ('Setting: IdleTimeout = {0}' -f $appPoolIdleTimeout)
$pool.ProcessModel.IdleTimeout = $appPoolIdleTimeout
Write-Output ('Setting: MaxProcesses = {0}' -f $appPoolMaxProcesses)
$pool.ProcessModel.MaxProcesses = $appPoolMaxProcesses
Write-Output ('Setting: RegularTimeInterval = {0}' -f $appPoolRegularTimeInterval)
$pool.Recycling.PeriodicRestart.Time = $appPoolRegularTimeInterval
Write-Output ('Setting: QueueLength = {0}' -f $appPoolQueueLength)
$pool.QueueLength = $appPoolQueueLength
Write-Output ('Setting: CPU Limit (percent) = {0}' -f $appPoolCpuLimit)
## Limit is stored in 1/1000s of one percent
$pool.Cpu.Limit = $appPoolCpuLimit * 1000
Write-Output ('Setting: CPU Limit Action = {0}' -f $appPoolCpuAction)
$pool.Cpu.Action = $appPoolCpuAction
Write-Output ('Setting: Schedule = {0}' -f $appPoolPeriodicRecycleTime)
$pool.Recycling.PeriodicRestart.Schedule.Clear()
foreach($timestamp in $list) {
$pool.Recycling.PeriodicRestart.Schedule.Add($timestamp)
}
if (Get-Member -InputObject $pool -Name StartMode -MemberType Properties)
{
Write-Output ('Setting: StartMode = {0}' -f $appPoolStartMode)
$pool.StartMode = $appPoolStartMode
}
else
{
Write-Output ('IIS does not support StartMode property, skipping this property...')
}
$iis.CommitChanges()
}