@ -19,47 +19,43 @@
# WANT_JSON
# POWERSHELL_COMMON
Import-Module Servermanager ;
Import-Module Servermanager
$params = Parse-Args $args ;
$result = New-Object PSObject -Property @ {
$result = @ {
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 = $name -split ',' | % { $_ . Trim ( ) }
$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 "
$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' "
}
$name = $name -split ',' | % { $_ . Trim ( ) }
$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
$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
# 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
$addWF = $true
}
else {
throw [ System.Exception ] " Not supported on this version of Windows "
throw [ System.Exception ] " Not supported on this version of Windows "
}
}
catch {
@ -67,24 +63,25 @@ catch {
}
If ( $state -eq " present " ) {
# Base params to cover both Add/Install-WindowsFeature
If ( $state -eq " present " ) {
# Base params to cover both Add/Install-WindowsFeature
$InstallParams = @ {
" Name " = $name ;
" Restart " = $Restart ;
" IncludeAllSubFeature " = $includesubfeatures ;
" ErrorAction " = " SilentlyContinue "
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 ( ! ( test-p ath $source ) ) {
if ( -not ( Test-Path -P ath $source ) ) {
Fail-Json $result " Failed to find source path $source "
}
$InstallParams . add ( " Source " , $source )
}
@ -92,9 +89,9 @@ If ($state -eq "present") {
$InstallParams . add ( " IncludeManagementTools " , $includemanagementtools )
}
}
try {
$featureresult = Invoke-Expression " $addCmdlet @InstallParams "
$featureresult = Invoke-Expression " $addCmdlet @InstallParams "
}
catch {
Fail-Json $result $_ . Exception . Message
@ -102,14 +99,15 @@ If ($state -eq "present") {
}
ElseIf ( $state -eq " absent " ) {
$UninstallParams = @ {
" Name " = $name ;
" Restart " = $Restart ;
" ErrorAction " = " SilentlyContinue "
$UninstallParams = @ {
Name = $name
Restart = $restart
ErrorAction = " SilentlyContinue "
WhatIf = $check_mode
}
try {
$featureresult = Invoke-Expression " $removeCmdlet @UninstallParams "
try {
$featureresult = Invoke-Expression " $removeCmdlet @UninstallParams "
}
catch {
Fail-Json $result $_ . Exception . Message
@ -125,13 +123,13 @@ If ($featureresult.FeatureResult)
ForEach ( $item in $featureresult . FeatureResult ) {
$message = @ ( )
ForEach ( $msg in $item . Message ) {
$message + = New-Object PSObject -Property @ {
$message + = @ {
message_type = $msg . MessageType . ToString ( )
error_code = $msg . ErrorCode
text = $msg . Text
}
}
$installed_features + = New-Object PSObject -Property @ {
$installed_features + = @ {
id = $item . Id
display_name = $item . DisplayName
message = $message
@ -143,10 +141,10 @@ If ($featureresult.FeatureResult)
$result . changed = $true
}
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 )
$result . feature_result = $installed_features
$result . success = ( $featureresult . Success . ToString ( ) | ConvertTo-Bool )
$result . exitcode = $featureresult . ExitCode . ToString ( )
$result . restart_needed = ( $featureresult . RestartNeeded . ToString ( ) | ConvertTo-Bool )
If ( $result . success ) {
Exit-Json $result