1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/lib/ansible/modules/windows/win_environment.ps1
Matt Davis 0c34cdd0f7 fix #Requires to be valid in real Powershell (#31858)
* module_utils #Requires should not have .psm1 extension if "real" Powershell will ever execute them
* updated validate-modules to enforce this
* added check to disallow multi-module syntax on Ansible.ModuleUtils #Requires
2017-10-23 11:40:54 -07:00

67 lines
2.1 KiB
PowerShell

#!powershell
# Copyright: (c) 2015, Jon Hawkesworth (@jhawkesworth) <figs@unity.demon.co.uk>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#Requires -Module Ansible.ModuleUtils.Legacy
$ErrorActionPreference = "Stop"
$params = Parse-Args -arguments $args -supports_check_mode $true
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
$diff_mode = Get-AnsibleParam -obj $params -name "_ansible_diff" -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 "absent","present"
$value = Get-AnsibleParam -obj $params -name "value" -type "str"
$level = Get-AnsibleParam -obj $params -name "level" -type "str" -validateSet "machine","user","process" -failifempty $true
$before_value = [Environment]::GetEnvironmentVariable($name, $level)
$result = @{
before_value = $before_value
changed = $false
value = $value
}
# When removing environment, set value to $null if set
if ($state -eq "absent" -and $value) {
Add-Warning -obj $result -message "When removing environment variable '$name' it should not have a value '$value' set"
$value = $null
}
if ($state -eq "present" -and $before_value -ne $value) {
if (-not $check_mode) {
[Environment]::SetEnvironmentVariable($name, $value, $level)
}
$result.changed = $true
if ($diff_mode) {
if ($before_value -eq $null) {
$result.diff = @{
prepared = " [$level]`n+$name = $value`n"
}
} else {
$result.diff = @{
prepared = " [$level]`n-$name = $before_value`n+$name = $value`n"
}
}
}
} elseif ($state -eq "absent" -and $before_value -ne $null) {
if (-not $check_mode) {
[Environment]::SetEnvironmentVariable($name, $null, $level)
}
$result.changed = $true
if ($diff_mode) {
$result.diff = @{
prepared = " [$level]`n-$name = $before_value`n"
}
}
}
Exit-Json -obj $result