ansible/windows/win_feature.ps1

150 lines
4.7 KiB
PowerShell
Raw Normal View History

#!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;
2015-09-17 20:44:36 +02:00
$result = New-Object PSObject -Property @{
changed = $false
}
$name = Get-Attr $params "name" -failifempty $true
2016-01-07 19:36:32 +01:00
$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'"
2014-07-08 18:04:12 +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
2016-01-07 19:36:32 +01:00
$source = Get-Attr $params "source" $false
2014-07-08 18:04:12 +02:00
If ($state -eq "present") {
2016-01-07 19:36:32 +01:00
if ($source)
{
if (!(test-path $source))
{
Fail-Json $result "Failed to find source path $source"
}
}
$InstallParams = @{
"Name"=$name;
"Restart"=$Restart;
"IncludeAllSubFeature"=$includesubfeatures;
"ErrorAction"="SilentlyContinue"
}
if ($IncludeManagementTools -eq $true)
{
2016-01-08 14:56:42 +01:00
$InstallParams.add("IncludeManagementTools",$includemanagementtools)
2016-01-07 19:36:32 +01:00
}
if ($source -ne $null)
{
2016-01-08 14:56:42 +01:00
$InstallParams.add("Source",$source)
2016-01-07 19:36:32 +01:00
}
try {
2015-09-17 20:44:36 +02:00
If (Get-Command "Install-WindowsFeature" -ErrorAction SilentlyContinue) {
2016-01-08 14:56:42 +01:00
$featureresult = Install-WindowsFeature @InstallParams
2015-09-17 20:44:36 +02:00
}
ElseIf (Get-Command "Add-WindowsFeature" -ErrorAction SilentlyContinue) {
2016-01-07 19:36:32 +01:00
if ($IncludeManagementTools)
{
$Params.Remove("IncludeManagementTools")
}
2016-01-08 14:56:42 +01:00
$featureresult = Add-WindowsFeature @InstallParams
2015-09-17 20:44:36 +02:00
}
Else {
Fail-Json $result "Not supported on this version of Windows"
}
}
catch {
Fail-Json $result $_.Exception.Message
}
}
2015-09-17 20:44:36 +02:00
ElseIf ($state -eq "absent") {
try {
2015-09-17 20:44:36 +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"
}
}
catch {
Fail-Json $result $_.Exception.Message
}
}
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
2015-09-17 20:44:36 +02:00
If ($featureresult.FeatureResult)
2014-07-10 12:00:00 +02:00
{
ForEach ($item in $featureresult.FeatureResult) {
2015-09-17 20:44:36 +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
2015-09-17 20:44:36 +02:00
message = $message
restart_needed = $item.RestartNeeded.ToString() | ConvertTo-Bool
2014-07-10 12:00:00 +02:00
skip_reason = $item.SkipReason.ToString()
2015-09-17 20:44:36 +02:00
success = $item.Success.ToString() | ConvertTo-Bool
2014-07-10 12:00:00 +02:00
}
}
2014-07-10 12:00:00 +02:00
$result.changed = $true
}
2015-09-17 20:44:36 +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)
2015-09-17 20:44:36 +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"
}