2014-09-26 01:01:01 +00:00
#!powershell
# This file is part of Ansible.
#
# Copyright 2014, Paul Durivage <paul.durivage@rackspace.com>
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# WANT_JSON
# POWERSHELL_COMMON
2017-03-15 02:01:03 +00:00
Import-Module Servermanager
2014-09-26 01:01:01 +00:00
2017-03-15 02:01:03 +00:00
$result = @ {
2014-09-26 01:01:01 +00:00
changed = $false
}
2017-03-15 02:01:03 +00:00
$params = Parse-Args $args -supports_check_mode $true
$check_mode = Get-AnsibleParam -obj $params -name " _ansible_check_mode " -type " bool " -default $false
2016-01-07 18:36:32 +00:00
2017-03-15 02:01:03 +00:00
$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 "
2017-05-03 22:52:08 +00:00
# DEPRECATED 2.4, potential removal in 2.6+
2017-03-15 02:01:03 +00:00
$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 "
2014-09-26 01:01:01 +00:00
2017-03-15 02:01:03 +00:00
$name = $name -split ',' | % { $_ . Trim ( ) }
2014-09-26 01:01:01 +00:00
2017-05-03 22:52:08 +00:00
If ( $restart ) {
Add-DeprecationWarning -obj $result -message " The 'restart' parameter causes instability. Use the 'win_reboot' action conditionally on 'reboot_required' return value instead " -version 2.6
}
2014-09-26 01:01:01 +00:00
2017-01-27 01:56:24 +00:00
# Determine which cmdlets we need to work with. Then we can set options appropriate for the cmdlet
$installWF = $false
$addWF = $false
try {
2017-03-15 02:01:03 +00:00
# We can infer uninstall/remove if install/add cmdlets exist
if ( Get-Command " Install-WindowsFeature " -ErrorAction SilentlyContinue ) {
$addCmdlet = " Install-WindowsFeature "
$removeCmdlet = " Uninstall-WindowsFeature "
$installWF = $true
2017-01-27 01:56:24 +00:00
}
elseif ( Get-Command " Add-WindowsFeature " -ErrorAction SilentlyContinue ) {
$addCmdlet = " Add-WindowsFeature "
$removeCmdlet = " Remove-WindowsFeature "
2017-03-15 02:01:03 +00:00
$addWF = $true
2017-01-27 01:56:24 +00:00
}
else {
2017-03-15 02:01:03 +00:00
throw [ System.Exception ] " Not supported on this version of Windows "
2016-01-07 18:36:32 +00:00
}
2017-01-27 01:56:24 +00:00
}
catch {
Fail-Json $result $_ . Exception . Message
}
2017-03-15 02:01:03 +00:00
If ( $state -eq " present " ) {
# Base params to cover both Add/Install-WindowsFeature
2016-01-07 18:36:32 +00:00
$InstallParams = @ {
2017-03-15 02:01:03 +00:00
Name = $name
Restart = $restart
IncludeAllSubFeature = $includesubfeatures
ErrorAction = " SilentlyContinue "
WhatIf = $check_mode
2016-01-07 18:36:32 +00:00
}
2017-03-15 02:01:03 +00:00
2017-01-27 01:56:24 +00:00
# IncludeManagementTools and source are options only for Install-WindowsFeature
if ( $installWF ) {
2017-03-15 02:01:03 +00:00
2017-01-27 01:56:24 +00:00
if ( $source ) {
2017-03-15 02:01:03 +00:00
if ( -not ( Test-Path -Path $source ) ) {
2017-01-27 01:56:24 +00:00
Fail-Json $result " Failed to find source path $source "
2016-01-07 18:36:32 +00:00
}
2017-03-15 02:01:03 +00:00
2017-01-27 01:56:24 +00:00
$InstallParams . add ( " Source " , $source )
2015-09-17 18:44:36 +00:00
}
2017-01-27 01:56:24 +00:00
if ( $IncludeManagementTools ) {
$InstallParams . add ( " IncludeManagementTools " , $includemanagementtools )
2015-09-17 18:44:36 +00:00
}
2014-09-26 01:01:01 +00:00
}
2017-03-15 02:01:03 +00:00
2017-01-27 01:56:24 +00:00
try {
2017-03-15 02:01:03 +00:00
$featureresult = Invoke-Expression " $addCmdlet @InstallParams "
2017-01-27 01:56:24 +00:00
}
2014-09-26 01:01:01 +00:00
catch {
Fail-Json $result $_ . Exception . Message
}
}
2015-09-17 18:44:36 +00:00
ElseIf ( $state -eq " absent " ) {
2017-01-27 01:56:24 +00:00
2017-03-15 02:01:03 +00:00
$UninstallParams = @ {
Name = $name
Restart = $restart
ErrorAction = " SilentlyContinue "
WhatIf = $check_mode
2017-01-27 01:56:24 +00:00
}
2017-03-15 02:01:03 +00:00
try {
$featureresult = Invoke-Expression " $removeCmdlet @UninstallParams "
2014-09-26 01:01:01 +00:00
}
catch {
Fail-Json $result $_ . Exception . Message
}
}
# Loop through results and create a hash containing details about
# each role/feature that is installed/removed
$installed_features = @ ( )
#$featureresult.featureresult is filled if anything was changed
2015-09-17 18:44:36 +00:00
If ( $featureresult . FeatureResult )
2014-09-26 01:01:01 +00:00
{
ForEach ( $item in $featureresult . FeatureResult ) {
2015-09-17 18:44:36 +00:00
$message = @ ( )
ForEach ( $msg in $item . Message ) {
2017-03-15 02:01:03 +00:00
$message + = @ {
2015-09-17 18:44:36 +00:00
message_type = $msg . MessageType . ToString ( )
error_code = $msg . ErrorCode
text = $msg . Text
}
}
2017-03-15 02:01:03 +00:00
$installed_features + = @ {
2015-09-17 18:44:36 +00:00
id = $item . Id
2014-09-26 01:01:01 +00:00
display_name = $item . DisplayName
2015-09-17 18:44:36 +00:00
message = $message
2017-05-03 22:52:08 +00:00
reboot_required = $item . RestartNeeded . ToString ( ) | ConvertTo-Bool
2014-09-26 01:01:01 +00:00
skip_reason = $item . SkipReason . ToString ( )
2015-09-17 18:44:36 +00:00
success = $item . Success . ToString ( ) | ConvertTo-Bool
2017-05-03 22:52:08 +00:00
# DEPRECATED 2.4, potential removal in 2.6+ (standardize naming to "reboot_required")
restart_needed = $item . RestartNeeded . ToString ( ) | ConvertTo-Bool
2014-09-26 01:01:01 +00:00
}
}
$result . changed = $true
}
2017-03-15 02:01:03 +00:00
$result . feature_result = $installed_features
$result . success = ( $featureresult . Success . ToString ( ) | ConvertTo-Bool )
$result . exitcode = $featureresult . ExitCode . ToString ( )
2017-05-03 22:52:08 +00:00
$result . reboot_required = ( $featureresult . RestartNeeded . ToString ( ) | ConvertTo-Bool )
# DEPRECATED 2.4, potential removal in 2.6+ (standardize naming to "reboot_required")
$result . restart_needed = $result . reboot_required
2015-06-22 08:06:48 +00:00
2015-09-17 18:44:36 +00:00
If ( $result . success ) {
Exit-Json $result
}
ElseIf ( $state -eq " present " ) {
Fail-Json $result " Failed to add feature "
}
Else {
Fail-Json $result " Failed to remove feature "
}