2014-06-19 00:06:49 +02: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
|
|
|
|
|
|
|
|
Import-Module Servermanager;
|
|
|
|
|
|
|
|
$params = Parse-Args $args;
|
|
|
|
|
2014-09-30 22:41:34 +02:00
|
|
|
$result = New-Object PSObject -Property @{
|
2014-06-19 00:06:49 +02:00
|
|
|
changed = $false
|
|
|
|
}
|
|
|
|
|
2015-08-23 01:01:11 +02:00
|
|
|
$name = Get-Attr $params "name" -failifempty $true
|
|
|
|
$name = $name -split ',' | % { $_.Trim() }
|
2014-06-19 19:17:10 +02:00
|
|
|
|
2015-08-23 01:01:11 +02:00
|
|
|
$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'"
|
2014-07-08 18:04:12 +02:00
|
|
|
}
|
|
|
|
|
2015-08-23 01:01:11 +02:00
|
|
|
$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
|
2014-07-08 18:04:12 +02:00
|
|
|
|
2014-06-19 00:06:49 +02:00
|
|
|
If ($state -eq "present") {
|
|
|
|
try {
|
2014-09-30 22:41:34 +02:00
|
|
|
If (Get-Command "Install-WindowsFeature" -ErrorAction SilentlyContinue) {
|
|
|
|
$featureresult = Install-WindowsFeature -Name $name -Restart:$restart -IncludeAllSubFeature:$includesubfeatures -IncludeManagementTools:$includemanagementtools -ErrorAction SilentlyContinue
|
|
|
|
}
|
|
|
|
ElseIf (Get-Command "Add-WindowsFeature" -ErrorAction SilentlyContinue) {
|
|
|
|
$featureresult = Add-WindowsFeature -Name $name -Restart:$restart -IncludeAllSubFeature:$includesubfeatures -ErrorAction SilentlyContinue
|
|
|
|
}
|
|
|
|
Else {
|
|
|
|
Fail-Json $result "Not supported on this version of Windows"
|
|
|
|
}
|
2014-06-19 00:06:49 +02:00
|
|
|
}
|
|
|
|
catch {
|
2014-06-19 19:17:10 +02:00
|
|
|
Fail-Json $result $_.Exception.Message
|
2014-06-19 00:06:49 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-30 22:41:34 +02:00
|
|
|
ElseIf ($state -eq "absent") {
|
2014-06-19 00:06:49 +02:00
|
|
|
try {
|
2014-09-30 22:41:34 +02:00
|
|
|
If (Get-Command "Uninstall-WindowsFeature" -ErrorAction SilentlyContinue) {
|
|
|
|
$featureresult = Uninstall-WindowsFeature -Name $name -Restart:$restart -ErrorAction SilentlyContinue
|
|
|
|
}
|
|
|
|
ElseIf (Get-Command "Remove-WindowsFeature" -ErrorAction SilentlyContinue) {
|
|
|
|
$featureresult = Remove-WindowsFeature -Name $name -Restart:$restart -ErrorAction SilentlyContinue
|
|
|
|
}
|
|
|
|
Else {
|
|
|
|
Fail-Json $result "Not supported on this version of Windows"
|
|
|
|
}
|
2014-06-19 00:06:49 +02:00
|
|
|
}
|
|
|
|
catch {
|
2014-06-19 19:17:10 +02:00
|
|
|
Fail-Json $result $_.Exception.Message
|
2014-06-19 00:06:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-19 19:17:48 +02:00
|
|
|
# Loop through results and create a hash containing details about
|
|
|
|
# each role/feature that is installed/removed
|
|
|
|
$installed_features = @()
|
2014-07-10 12:00:00 +02:00
|
|
|
#$featureresult.featureresult is filled if anything was changed
|
2014-09-30 22:41:34 +02:00
|
|
|
If ($featureresult.FeatureResult)
|
2014-07-10 12:00:00 +02:00
|
|
|
{
|
|
|
|
ForEach ($item in $featureresult.FeatureResult) {
|
2014-09-30 22:41:34 +02:00
|
|
|
$message = @()
|
|
|
|
ForEach ($msg in $item.Message) {
|
|
|
|
$message += New-Object PSObject -Property @{
|
|
|
|
message_type = $msg.MessageType.ToString()
|
|
|
|
error_code = $msg.ErrorCode
|
|
|
|
text = $msg.Text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$installed_features += New-Object PSObject -Property @{
|
|
|
|
id = $item.Id
|
2014-07-10 12:00:00 +02:00
|
|
|
display_name = $item.DisplayName
|
2014-09-30 22:41:34 +02:00
|
|
|
message = $message
|
|
|
|
restart_needed = $item.RestartNeeded.ToString() | ConvertTo-Bool
|
2014-07-10 12:00:00 +02:00
|
|
|
skip_reason = $item.SkipReason.ToString()
|
2014-09-30 22:41:34 +02:00
|
|
|
success = $item.Success.ToString() | ConvertTo-Bool
|
2014-07-10 12:00:00 +02:00
|
|
|
}
|
2014-06-19 00:06:49 +02:00
|
|
|
}
|
2014-07-10 12:00:00 +02:00
|
|
|
$result.changed = $true
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:41:34 +02:00
|
|
|
Set-Attr $result "feature_result" $installed_features
|
|
|
|
Set-Attr $result "success" ($featureresult.Success.ToString() | ConvertTo-Bool)
|
|
|
|
Set-Attr $result "exitcode" $featureresult.ExitCode.ToString()
|
|
|
|
Set-Attr $result "restart_needed" ($featureresult.RestartNeeded.ToString() | ConvertTo-Bool)
|
2014-06-19 19:17:10 +02:00
|
|
|
|
2014-09-30 22:41:34 +02: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"
|
|
|
|
}
|