| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2015, Henrik Wallström <[email protected]> |
| 4 |
# Copyright: (c) 2017, Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
#Requires -Module Ansible.ModuleUtils.Legacy |
| 8 |
|
| 9 |
$ErrorActionPreference = 'Stop' |
| 10 |
|
| 11 |
$params = Parse-Args -arguments $args -supports_check_mode $true |
| 12 |
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false |
| 13 |
|
| 14 |
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true |
| 15 |
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateSet "started", "restarted", "stopped", "absent", "present" |
| 16 |
$result = @{ |
| 17 |
changed = $false |
| 18 |
attributes = @{} |
| 19 |
info = @{ |
| 20 |
name = $name |
| 21 |
state = $state |
| 22 |
attributes = @{} |
| 23 |
cpu = @{} |
| 24 |
failure = @{} |
| 25 |
processModel = @{} |
| 26 |
recycling = @{ |
| 27 |
periodicRestart = @{} |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
# Stores the free form attributes for the module |
| 33 |
$attributes = @{} |
| 34 |
$input_attributes = Get-AnsibleParam -obj $params -name "attributes" |
| 35 |
if ($input_attributes) { |
| 36 |
if ($input_attributes -is [System.Collections.Hashtable]) { |
| 37 |
# Uses dict style parameters, newer and recommended style |
| 38 |
$attributes = $input_attributes |
| 39 |
} |
| 40 |
else { |
| 41 |
Fail-Json -obj $result -message "Using a string for the attributes parameter is not longer supported, please use a dict instead" |
| 42 |
} |
| 43 |
} |
| 44 |
$result.attributes = $attributes |
| 45 |
|
| 46 |
Function Get-DotNetClassForAttribute($attribute_parent) { |
| 47 |
switch ($attribute_parent) { |
| 48 |
"attributes" { [Microsoft.Web.Administration.ApplicationPool] } |
| 49 |
"cpu" { [Microsoft.Web.Administration.ApplicationPoolCpu] } |
| 50 |
"failure" { [Microsoft.Web.Administration.ApplicationPoolFailure] } |
| 51 |
"processModel" { [Microsoft.Web.Administration.ApplicationPoolProcessModel] } |
| 52 |
"recycling" { [Microsoft.Web.Administration.ApplicationPoolRecycling] } |
| 53 |
default { [Microsoft.Web.Administration.ApplicationPool] } |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
Function Convert-CollectionToList($collection) { |
| 58 |
$list = @() |
| 59 |
|
| 60 |
if ($collection -is [String]) { |
| 61 |
$raw_list = $collection -split "," |
| 62 |
foreach ($entry in $raw_list) { |
| 63 |
$list += $entry.Trim() |
| 64 |
} |
| 65 |
} |
| 66 |
elseif ($collection -is [Microsoft.IIs.PowerShell.Framework.ConfigurationElement]) { |
| 67 |
# the collection is the value from IIS itself, we need to conver accordingly |
| 68 |
foreach ($entry in $collection.Collection) { |
| 69 |
$list += $entry.Value.ToString() |
| 70 |
} |
| 71 |
} |
| 72 |
elseif ($collection -isnot [Array]) { |
| 73 |
$list += $collection |
| 74 |
} |
| 75 |
else { |
| 76 |
$list = $collection |
| 77 |
} |
| 78 |
|
| 79 |
return , $list |
| 80 |
} |
| 81 |
|
| 82 |
Function Compare-Value($current, $new) { |
| 83 |
if ($null -eq $current) { |
| 84 |
return $true |
| 85 |
} |
| 86 |
|
| 87 |
if ($current -is [Array]) { |
| 88 |
if ($new -isnot [Array]) { |
| 89 |
return $true |
| 90 |
} |
| 91 |
|
| 92 |
if ($current.Count -ne $new.Count) { |
| 93 |
return $true |
| 94 |
} |
| 95 |
for ($i = 0; $i -lt $current.Count; $i++) { |
| 96 |
if ($current[$i] -ne $new[$i]) { |
| 97 |
return $true |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
else { |
| 102 |
if ($current -ne $new) { |
| 103 |
return $true |
| 104 |
} |
| 105 |
} |
| 106 |
return $false |
| 107 |
} |
| 108 |
|
| 109 |
Function Convert-ToPropertyValue($pool, $attribute_key, $attribute_value) { |
| 110 |
# Will convert the new value to the enum value expected and cast accordingly to the type |
| 111 |
if ([bool]($attribute_value.PSobject.Properties.Name -match "Value")) { |
| 112 |
$attribute_value = $attribute_value.Value |
| 113 |
} |
| 114 |
$attribute_key_split = $attribute_key -split "\." |
| 115 |
if ($attribute_key_split.Length -eq 1) { |
| 116 |
$attribute_parent = "attributes" |
| 117 |
$attribute_child = $attribute_key |
| 118 |
$attribute_meta = $pool.Attributes | Where-Object { $_.Name -eq $attribute_child } |
| 119 |
} |
| 120 |
elseif ($attribute_key_split.Length -gt 1) { |
| 121 |
$attribute_parent = $attribute_key_split[0] |
| 122 |
$attribute_key_split = $attribute_key_split[1..$($attribute_key_split.Length - 1)] |
| 123 |
$parent = $pool.$attribute_parent |
| 124 |
|
| 125 |
foreach ($key in $attribute_key_split) { |
| 126 |
$attribute_meta = $parent.Attributes | Where-Object { $_.Name -eq $key } |
| 127 |
$parent = $parent.$key |
| 128 |
if ($null -eq $attribute_meta) { |
| 129 |
$attribute_meta = $parent |
| 130 |
} |
| 131 |
} |
| 132 |
$attribute_child = $attribute_key_split[-1] |
| 133 |
} |
| 134 |
|
| 135 |
if ($attribute_meta) { |
| 136 |
if (($attribute_meta.PSObject.Properties.Name -eq "Collection").Count -gt 0) { |
| 137 |
return , (Convert-CollectionToList -collection $attribute_value) |
| 138 |
} |
| 139 |
$type = $attribute_meta.Schema.Type |
| 140 |
$value = $attribute_value |
| 141 |
if ($type -eq "enum") { |
| 142 |
# Attempt to convert the value from human friendly to enum value - use existing value if we fail |
| 143 |
$dot_net_class = Get-DotNetClassForAttribute -attribute_parent $attribute_parent |
| 144 |
$enum_attribute_name = $attribute_child.Substring(0, 1).ToUpper() + $attribute_child.Substring(1) |
| 145 |
$enum = $dot_net_class.GetProperty($enum_attribute_name).PropertyType.FullName |
| 146 |
if ($enum) { |
| 147 |
$enum_values = [Enum]::GetValues($enum) |
| 148 |
foreach ($enum_value in $enum_values) { |
| 149 |
if ($attribute_value.GetType() -is $enum_value.GetType()) { |
| 150 |
if ($enum_value -eq $attribute_value) { |
| 151 |
$value = $enum_value |
| 152 |
break |
| 153 |
} |
| 154 |
} |
| 155 |
else { |
| 156 |
if ([System.String]$enum_value -eq [System.String]$attribute_value) { |
| 157 |
$value = $enum_value |
| 158 |
break |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
# Try and cast the variable using the chosen type, revert to the default if it fails |
| 165 |
Set-Variable -Name casted_value -Value ($value -as ([type] $attribute_meta.TypeName)) |
| 166 |
if ($null -eq $casted_value) { |
| 167 |
$value |
| 168 |
} |
| 169 |
else { |
| 170 |
$casted_value |
| 171 |
} |
| 172 |
} |
| 173 |
else { |
| 174 |
$attribute_value |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
# Ensure WebAdministration module is loaded |
| 179 |
if ($null -eq (Get-Module -Name "WebAdministration" -ErrorAction SilentlyContinue)) { |
| 180 |
Import-Module WebAdministration |
| 181 |
$web_admin_dll_path = Join-Path $env:SystemRoot system32\inetsrv\Microsoft.Web.Administration.dll |
| 182 |
Add-Type -LiteralPath $web_admin_dll_path |
| 183 |
} |
| 184 |
|
| 185 |
$pool = Get-Item -LiteralPath IIS:\AppPools\$name -ErrorAction SilentlyContinue |
| 186 |
if ($state -eq "absent") { |
| 187 |
# Remove pool if present |
| 188 |
if ($pool) { |
| 189 |
try { |
| 190 |
Remove-WebAppPool -Name $name -WhatIf:$check_mode |
| 191 |
} |
| 192 |
catch { |
| 193 |
Fail-Json $result "Failed to remove Web App pool $($name): $($_.Exception.Message)" |
| 194 |
} |
| 195 |
$result.changed = $true |
| 196 |
} |
| 197 |
} |
| 198 |
else { |
| 199 |
# Add pool if absent |
| 200 |
if (-not $pool) { |
| 201 |
if (-not $check_mode) { |
| 202 |
try { |
| 203 |
New-WebAppPool -Name $name > $null |
| 204 |
} |
| 205 |
catch { |
| 206 |
Fail-Json $result "Failed to create new Web App Pool $($name): $($_.Exception.Message)" |
| 207 |
} |
| 208 |
} |
| 209 |
$result.changed = $true |
| 210 |
# If in check mode this pool won't actually exists so skip it |
| 211 |
if (-not $check_mode) { |
| 212 |
$pool = Get-Item -LiteralPath IIS:\AppPools\$name |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
# Cannot run the below in check mode if the pool did not always exist |
| 217 |
if ($pool) { |
| 218 |
# Modify pool based on parameters |
| 219 |
foreach ($attribute in $attributes.GetEnumerator()) { |
| 220 |
$attribute_key = $attribute.Name |
| 221 |
$new_raw_value = $attribute.Value |
| 222 |
$new_value = Convert-ToPropertyValue -pool $pool -attribute_key $attribute_key -attribute_value $new_raw_value |
| 223 |
|
| 224 |
$current_raw_value = Get-ItemProperty -LiteralPath IIS:\AppPools\$name -Name $attribute_key -ErrorAction SilentlyContinue |
| 225 |
$current_value = Convert-ToPropertyValue -pool $pool -attribute_key $attribute_key -attribute_value $current_raw_value |
| 226 |
|
| 227 |
$changed = Compare-Value -current $current_value -new $new_value |
| 228 |
if ($changed -eq $true) { |
| 229 |
if ($new_value -is [Array]) { |
| 230 |
try { |
| 231 |
Clear-ItemProperty -LiteralPath IIS:\AppPools\$name -Name $attribute_key -WhatIf:$check_mode |
| 232 |
} |
| 233 |
catch { |
| 234 |
$msg = -join @( |
| 235 |
"Failed to clear attribute to Web App Pool $name. Attribute: $attribute_key, " |
| 236 |
"Exception: $($_.Exception.Message)" |
| 237 |
) |
| 238 |
Fail-Json -obj $result -message $msg |
| 239 |
} |
| 240 |
foreach ($value in $new_value) { |
| 241 |
try { |
| 242 |
New-ItemProperty -LiteralPath IIS:\AppPools\$name -Name $attribute_key -Value @{value = $value } -WhatIf:$check_mode > $null |
| 243 |
} |
| 244 |
catch { |
| 245 |
$msg = -join @( |
| 246 |
"Failed to add new attribute to Web App Pool $name. Attribute: $attribute_key, " |
| 247 |
"Value: $value, Exception: $($_.Exception.Message)" |
| 248 |
) |
| 249 |
Fail-Json -obj $result -message $msg |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
else { |
| 254 |
try { |
| 255 |
Set-ItemProperty -LiteralPath IIS:\AppPools\$name -Name $attribute_key -Value $new_value -WhatIf:$check_mode |
| 256 |
} |
| 257 |
catch { |
| 258 |
$msg = -join @( |
| 259 |
"Failed to set attribute to Web App Pool $name. Attribute: $attribute_key, " |
| 260 |
"Value: $new_value, Exception: $($_.Exception.Message)" |
| 261 |
) |
| 262 |
Fail-Json $result $msg |
| 263 |
} |
| 264 |
} |
| 265 |
$result.changed = $true |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
# Set the state of the pool |
| 270 |
if ($pool.State -eq "Stopped") { |
| 271 |
if ($state -eq "started" -or $state -eq "restarted") { |
| 272 |
if (-not $check_mode) { |
| 273 |
try { |
| 274 |
Start-WebAppPool -Name $name > $null |
| 275 |
} |
| 276 |
catch { |
| 277 |
Fail-Json $result "Failed to start Web App Pool $($name): $($_.Exception.Message)" |
| 278 |
} |
| 279 |
} |
| 280 |
$result.changed = $true |
| 281 |
} |
| 282 |
} |
| 283 |
else { |
| 284 |
if ($state -eq "stopped") { |
| 285 |
if (-not $check_mode) { |
| 286 |
try { |
| 287 |
Stop-WebAppPool -Name $name > $null |
| 288 |
} |
| 289 |
catch { |
| 290 |
Fail-Json $result "Failed to stop Web App Pool $($name): $($_.Exception.Message)" |
| 291 |
} |
| 292 |
} |
| 293 |
$result.changed = $true |
| 294 |
} |
| 295 |
elseif ($state -eq "restarted") { |
| 296 |
if (-not $check_mode) { |
| 297 |
try { |
| 298 |
Restart-WebAppPool -Name $name > $null |
| 299 |
} |
| 300 |
catch { |
| 301 |
Fail-Json $result "Failed to restart Web App Pool $($name): $($_.Exception.Message)" |
| 302 |
} |
| 303 |
} |
| 304 |
$result.changed = $true |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
# Get all the current attributes for the pool |
| 311 |
$pool = Get-Item -LiteralPath IIS:\AppPools\$name -ErrorAction SilentlyContinue |
| 312 |
$elements = @("attributes", "cpu", "failure", "processModel", "recycling") |
| 313 |
|
| 314 |
foreach ($element in $elements) { |
| 315 |
if ($element -eq "attributes") { |
| 316 |
$attribute_collection = $pool.Attributes |
| 317 |
$attribute_parent = $pool |
| 318 |
} |
| 319 |
else { |
| 320 |
$attribute_collection = $pool.$element.Attributes |
| 321 |
$attribute_parent = $pool.$element |
| 322 |
} |
| 323 |
|
| 324 |
foreach ($attribute in $attribute_collection) { |
| 325 |
$attribute_name = $attribute.Name |
| 326 |
if ($attribute_name -notlike "*password*") { |
| 327 |
$attribute_value = $attribute_parent.$attribute_name |
| 328 |
|
| 329 |
$result.info.$element.Add($attribute_name, $attribute_value) |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
# Manually get the periodicRestart attributes in recycling |
| 335 |
foreach ($attribute in $pool.recycling.periodicRestart.Attributes) { |
| 336 |
$attribute_name = $attribute.Name |
| 337 |
$attribute_value = $pool.recycling.periodicRestart.$attribute_name |
| 338 |
$result.info.recycling.periodicRestart.Add($attribute_name, $attribute_value) |
| 339 |
} |
| 340 |
|
| 341 |
Exit-Json $result |