allow using --check on win_iis_webapppool module (#50528) (#64812)

* allow using --check on win_iis_webapppool module

* Added changelog and slight logic tweak

* Fix typo in changelog fragment

(cherry picked from commit 23a751323b)
pull/64874/merge
Jordan Borean 6 years ago committed by Matt Davis
parent 07365b2040
commit f3c58f554c

@ -0,0 +1,2 @@
bugfixes:
- win_iis_webapppool - Do not try and set attributes in check mode when the pool did not exist

@ -201,72 +201,75 @@ if ($state -eq "absent") {
} }
} }
# Modify pool based on parameters # Cannot run the below in check mode if the pool did not always exist
foreach ($attribute in $attributes.GetEnumerator()) { if ($pool) {
$attribute_key = $attribute.Name # Modify pool based on parameters
$new_raw_value = $attribute.Value foreach ($attribute in $attributes.GetEnumerator()) {
$new_value = Convert-ToPropertyValue -pool $pool -attribute_key $attribute_key -attribute_value $new_raw_value $attribute_key = $attribute.Name
$new_raw_value = $attribute.Value
$new_value = Convert-ToPropertyValue -pool $pool -attribute_key $attribute_key -attribute_value $new_raw_value
$current_raw_value = Get-ItemProperty -Path IIS:\AppPools\$name -Name $attribute_key -ErrorAction SilentlyContinue $current_raw_value = Get-ItemProperty -Path IIS:\AppPools\$name -Name $attribute_key -ErrorAction SilentlyContinue
$current_value = Convert-ToPropertyValue -pool $pool -attribute_key $attribute_key -attribute_value $current_raw_value $current_value = Convert-ToPropertyValue -pool $pool -attribute_key $attribute_key -attribute_value $current_raw_value
$changed = Compare-Values -current $current_value -new $new_value $changed = Compare-Values -current $current_value -new $new_value
if ($changed -eq $true) { if ($changed -eq $true) {
if ($new_value -is [Array]) { if ($new_value -is [Array]) {
try {
Clear-ItemProperty -Path IIS:\AppPools\$name -Name $attribute_key -WhatIf:$check_mode
} catch {
Fail-Json -obj $result -message "Failed to clear attribute to Web App Pool $name. Attribute: $attribute_key, Exception: $($_.Exception.Message)"
}
foreach ($value in $new_value) {
try { try {
New-ItemProperty -Path IIS:\AppPools\$name -Name $attribute_key -Value @{value=$value} -WhatIf:$check_mode > $null Clear-ItemProperty -Path IIS:\AppPools\$name -Name $attribute_key -WhatIf:$check_mode
} catch { } catch {
Fail-Json -obj $result -message "Failed to add new attribute to Web App Pool $name. Attribute: $attribute_key, Value: $value, Exception: $($_.Exception.Message)" Fail-Json -obj $result -message "Failed to clear attribute to Web App Pool $name. Attribute: $attribute_key, Exception: $($_.Exception.Message)"
}
foreach ($value in $new_value) {
try {
New-ItemProperty -Path IIS:\AppPools\$name -Name $attribute_key -Value @{value=$value} -WhatIf:$check_mode > $null
} catch {
Fail-Json -obj $result -message "Failed to add new attribute to Web App Pool $name. Attribute: $attribute_key, Value: $value, Exception: $($_.Exception.Message)"
}
}
} else {
try {
Set-ItemProperty -Path IIS:\AppPools\$name -Name $attribute_key -Value $new_value -WhatIf:$check_mode
} catch {
Fail-Json $result "Failed to set attribute to Web App Pool $name. Attribute: $attribute_key, Value: $new_value, Exception: $($_.Exception.Message)"
} }
} }
} else { $result.changed = $true
try {
Set-ItemProperty -Path IIS:\AppPools\$name -Name $attribute_key -Value $new_value -WhatIf:$check_mode
} catch {
Fail-Json $result "Failed to set attribute to Web App Pool $name. Attribute: $attribute_key, Value: $new_value, Exception: $($_.Exception.Message)"
}
} }
$result.changed = $true
} }
}
# Set the state of the pool # Set the state of the pool
if ($pool.State -eq "Stopped") { if ($pool.State -eq "Stopped") {
if ($state -eq "started" -or $state -eq "restarted") { if ($state -eq "started" -or $state -eq "restarted") {
if (-not $check_mode) { if (-not $check_mode) {
try { try {
Start-WebAppPool -Name $name > $null Start-WebAppPool -Name $name > $null
} catch { } catch {
Fail-Json $result "Failed to start Web App Pool $($name): $($_.Exception.Message)" Fail-Json $result "Failed to start Web App Pool $($name): $($_.Exception.Message)"
}
} }
$result.changed = $true
} }
$result.changed = $true } else {
} if ($state -eq "stopped") {
} else { if (-not $check_mode) {
if ($state -eq "stopped") { try {
if (-not $check_mode) { Stop-WebAppPool -Name $name > $null
try { } catch {
Stop-WebAppPool -Name $name > $null Fail-Json $result "Failed to stop Web App Pool $($name): $($_.Exception.Message)"
} catch { }
Fail-Json $result "Failed to stop Web App Pool $($name): $($_.Exception.Message)"
} }
} $result.changed = $true
$result.changed = $true } elseif ($state -eq "restarted") {
} elseif ($state -eq "restarted") { if (-not $check_mode) {
if (-not $check_mode) { try {
try { Restart-WebAppPool -Name $name > $null
Restart-WebAppPool -Name $name > $null } catch {
} catch { Fail-Json $result "Failed to restart Web App Pool $($name): $($_.Exception.Message)"
Fail-Json $result "Failed to restart Web App Pool $($name): $($_.Exception.Message)" }
} }
$result.changed = $true
} }
$result.changed = $true
} }
} }
} }

Loading…
Cancel
Save