summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/windows/win_feature.ps1
blob: e7feabd3307cd91a068f159cd8adf98c934cada6 (plain)
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
#!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

Import-Module Servermanager

$result = @{
    changed = $false
}

$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"

# DEPRECATED 2.4, potential removal in 2.6+
$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() }

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
}

# Determine which cmdlets we need to work with. Then we can set options appropriate for the cmdlet
$installWF= $false
$addWF = $false

try {
    # 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
    }
    elseif (Get-Command "Add-WindowsFeature" -ErrorAction SilentlyContinue) {
        $addCmdlet = "Add-WindowsFeature"
        $removeCmdlet = "Remove-WindowsFeature"
        $addWF = $true
    }
    else {
        throw [System.Exception] "Not supported on this version of Windows"
    }
}
catch {
    Fail-Json $result $_.Exception.Message
}


If ($state -eq "present") {

    # Base params to cover both Add/Install-WindowsFeature
    $InstallParams = @{
        Name = $name
        Restart = $restart
        IncludeAllSubFeature = $includesubfeatures
        ErrorAction = "SilentlyContinue"
        WhatIf = $check_mode
    }

    # IncludeManagementTools and source are options only for Install-WindowsFeature
    if ($installWF) {

        if ($source) {
            if (-not (Test-Path -Path $source)) {
                Fail-Json $result "Failed to find source path $source"
            }

            $InstallParams.add("Source",$source)
        }

        if ($IncludeManagementTools) {
            $InstallParams.add("IncludeManagementTools",$includemanagementtools)
        }
    }

    try {
        $featureresult = Invoke-Expression "$addCmdlet @InstallParams"
    }
    catch {
        Fail-Json $result $_.Exception.Message
    }
}
ElseIf ($state -eq "absent") {

    $UninstallParams = @{
        Name = $name
        Restart = $restart
        ErrorAction = "SilentlyContinue"
        WhatIf = $check_mode
    }

    try {
        $featureresult = Invoke-Expression "$removeCmdlet @UninstallParams"
    }
    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
If ($featureresult.FeatureResult)
{
    ForEach ($item in $featureresult.FeatureResult) {
        $message = @()
        ForEach ($msg in $item.Message) {
            $message += @{
                message_type = $msg.MessageType.ToString()
                error_code = $msg.ErrorCode
                text = $msg.Text
            }
        }
        $installed_features += @{
            id = $item.Id
            display_name = $item.DisplayName
            message = $message
            reboot_required = $item.RestartNeeded.ToString() | ConvertTo-Bool
            skip_reason = $item.SkipReason.ToString()
            success = $item.Success.ToString() | ConvertTo-Bool

            # DEPRECATED 2.4, potential removal in 2.6+ (standardize naming to "reboot_required")
            restart_needed = $item.RestartNeeded.ToString() | ConvertTo-Bool
        }
    }
    $result.changed = $true
}

$result.feature_result = $installed_features
$result.success = ($featureresult.Success.ToString() | ConvertTo-Bool)
$result.exitcode = $featureresult.ExitCode.ToString()
$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

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"
}