win_feature: Clean up and check-mode support (#21351)

* Clean up parameter handling and added check-mode support

Changes include:
- Remove trailing semi-colons
- Replaced PSObjects into normal hashes
- Make use of Get-AnsibleParam and types
- Added check-mode support

* Implemented -WhatIf:$check_mode support

* powershell.ps1: Ensure Fail-Json() works with Hashtables

Without this change a dictionary $result object would be emptied if it
is anything but a PSCustomObject. Now we also support Hashtables.

* Revert to original formatting
This commit is contained in:
Dag Wieers 2017-03-15 03:01:03 +01:00 committed by Matt Davis
parent 589c483cfc
commit 9755d2dbbc
2 changed files with 46 additions and 57 deletions

View file

@ -19,47 +19,43 @@
# WANT_JSON # WANT_JSON
# POWERSHELL_COMMON # POWERSHELL_COMMON
Import-Module Servermanager; Import-Module Servermanager
$params = Parse-Args $args; $result = @{
$result = New-Object PSObject -Property @{
changed = $false changed = $false
} }
$name = Get-Attr $params "name" -failifempty $true $params = Parse-Args $args -supports_check_mode $true
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present","absent"
$restart = Get-AnsibleParam -obj $params -name "restart" -type "bool" -default $false
$includesubfeatures = Get-AnsibleParam -obj $params -name "include_sub_features" -type "bool" -default $false
$includemanagementtools = Get-AnsibleParam -obj $params -name "include_management_tools" -type "bool" -default $false
$source = Get-AnsibleParam -obj $params -name "source" -type "str"
$name = $name -split ',' | % { $_.Trim() } $name = $name -split ',' | % { $_.Trim() }
$state = Get-Attr $params "state" "present"
$state = $state.ToString().ToLower()
If (($state -ne 'present') -and ($state -ne 'absent')) {
Fail-Json $result "state is '$state'; must be 'present' or 'absent'"
}
$restart = Get-Attr $params "restart" $false | ConvertTo-Bool
$includesubfeatures = Get-Attr $params "include_sub_features" $false | ConvertTo-Bool
$includemanagementtools = Get-Attr $params "include_management_tools" $false | ConvertTo-Bool
$source = Get-Attr $params "source" $false
# Determine which cmdlets we need to work with. Then we can set options appropriate for the cmdlet # Determine which cmdlets we need to work with. Then we can set options appropriate for the cmdlet
$installWF= $false $installWF= $false
$addWF = $false $addWF = $false
try { try {
# We can infer uninstall/remove if install/add cmdlets exist # We can infer uninstall/remove if install/add cmdlets exist
if (Get-Command "Install-WindowsFeature" -ErrorAction SilentlyContinue) { if (Get-Command "Install-WindowsFeature" -ErrorAction SilentlyContinue) {
$addCmdlet = "Install-WindowsFeature" $addCmdlet = "Install-WindowsFeature"
$removeCmdlet = "Uninstall-WindowsFeature" $removeCmdlet = "Uninstall-WindowsFeature"
$installWF = $true $installWF = $true
} }
elseif (Get-Command "Add-WindowsFeature" -ErrorAction SilentlyContinue) { elseif (Get-Command "Add-WindowsFeature" -ErrorAction SilentlyContinue) {
$addCmdlet = "Add-WindowsFeature" $addCmdlet = "Add-WindowsFeature"
$removeCmdlet = "Remove-WindowsFeature" $removeCmdlet = "Remove-WindowsFeature"
$addWF = $true $addWF = $true
} }
else { else {
throw [System.Exception] "Not supported on this version of Windows" throw [System.Exception] "Not supported on this version of Windows"
} }
} }
catch { catch {
@ -67,24 +63,25 @@ catch {
} }
If ($state -eq "present") { If ($state -eq "present") {
# Base params to cover both Add/Install-WindowsFeature # Base params to cover both Add/Install-WindowsFeature
$InstallParams = @{ $InstallParams = @{
"Name"=$name; Name = $name
"Restart"=$Restart; Restart = $restart
"IncludeAllSubFeature"=$includesubfeatures; IncludeAllSubFeature = $includesubfeatures
"ErrorAction"="SilentlyContinue" ErrorAction = "SilentlyContinue"
WhatIf = $check_mode
} }
# IncludeManagementTools and source are options only for Install-WindowsFeature # IncludeManagementTools and source are options only for Install-WindowsFeature
if ($installWF) { if ($installWF) {
if ($source) { if ($source) {
if (!(test-path $source)) { if (-not (Test-Path -Path $source)) {
Fail-Json $result "Failed to find source path $source" Fail-Json $result "Failed to find source path $source"
} }
$InstallParams.add("Source",$source) $InstallParams.add("Source",$source)
} }
@ -92,9 +89,9 @@ If ($state -eq "present") {
$InstallParams.add("IncludeManagementTools",$includemanagementtools) $InstallParams.add("IncludeManagementTools",$includemanagementtools)
} }
} }
try { try {
$featureresult = Invoke-Expression "$addCmdlet @InstallParams" $featureresult = Invoke-Expression "$addCmdlet @InstallParams"
} }
catch { catch {
Fail-Json $result $_.Exception.Message Fail-Json $result $_.Exception.Message
@ -102,14 +99,15 @@ If ($state -eq "present") {
} }
ElseIf ($state -eq "absent") { ElseIf ($state -eq "absent") {
$UninstallParams = @{ $UninstallParams = @{
"Name"=$name; Name = $name
"Restart"=$Restart; Restart = $restart
"ErrorAction"="SilentlyContinue" ErrorAction = "SilentlyContinue"
WhatIf = $check_mode
} }
try { try {
$featureresult = Invoke-Expression "$removeCmdlet @UninstallParams" $featureresult = Invoke-Expression "$removeCmdlet @UninstallParams"
} }
catch { catch {
Fail-Json $result $_.Exception.Message Fail-Json $result $_.Exception.Message
@ -125,13 +123,13 @@ If ($featureresult.FeatureResult)
ForEach ($item in $featureresult.FeatureResult) { ForEach ($item in $featureresult.FeatureResult) {
$message = @() $message = @()
ForEach ($msg in $item.Message) { ForEach ($msg in $item.Message) {
$message += New-Object PSObject -Property @{ $message += @{
message_type = $msg.MessageType.ToString() message_type = $msg.MessageType.ToString()
error_code = $msg.ErrorCode error_code = $msg.ErrorCode
text = $msg.Text text = $msg.Text
} }
} }
$installed_features += New-Object PSObject -Property @{ $installed_features += @{
id = $item.Id id = $item.Id
display_name = $item.DisplayName display_name = $item.DisplayName
message = $message message = $message
@ -143,10 +141,10 @@ If ($featureresult.FeatureResult)
$result.changed = $true $result.changed = $true
} }
Set-Attr $result "feature_result" $installed_features $result.feature_result = $installed_features
Set-Attr $result "success" ($featureresult.Success.ToString() | ConvertTo-Bool) $result.success = ($featureresult.Success.ToString() | ConvertTo-Bool)
Set-Attr $result "exitcode" $featureresult.ExitCode.ToString() $result.exitcode = $featureresult.ExitCode.ToString()
Set-Attr $result "restart_needed" ($featureresult.RestartNeeded.ToString() | ConvertTo-Bool) $result.restart_needed = ($featureresult.RestartNeeded.ToString() | ConvertTo-Bool)
If ($result.success) { If ($result.success) {
Exit-Json $result Exit-Json $result

View file

@ -38,11 +38,9 @@ options:
description: description:
- Names of roles or features to install as a single feature or a comma-separated list of features - Names of roles or features to install as a single feature or a comma-separated list of features
required: true required: true
default: null
state: state:
description: description:
- State of the features or roles on the system - State of the features or roles on the system
required: false
choices: choices:
- present - present
- absent - absent
@ -53,16 +51,12 @@ options:
choices: choices:
- yes - yes
- no - no
default: null
required: false
include_sub_features: include_sub_features:
description: description:
- Adds all subfeatures of the specified feature - Adds all subfeatures of the specified feature
choices: choices:
- yes - yes
- no - no
default: null
required: false
include_management_tools: include_management_tools:
description: description:
- Adds the corresponding management tools to the specified feature. - Adds the corresponding management tools to the specified feature.
@ -70,13 +64,10 @@ options:
choices: choices:
- yes - yes
- no - no
default: null
required: false
source: source:
description: description:
- Specify a source to install the feature from. - Specify a source to install the feature from.
- Not supported in Windows 2008. If present when using Windows 2008 this option will be ignored. - Not supported in Windows 2008. If present when using Windows 2008 this option will be ignored.
required: false
choices: [ ' {driveletter}:\sources\sxs', ' {IP}\Share\sources\sxs' ] choices: [ ' {driveletter}:\sources\sxs', ' {IP}\Share\sources\sxs' ]
version_added: "2.1" version_added: "2.1"
author: author: