2015-06-22 14:40:49 +00:00
|
|
|
#!powershell
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Copyright 2015, Phil Schwartz <schwartzmx@gmail.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
|
|
|
|
|
2017-02-21 06:01:09 +00:00
|
|
|
$params = Parse-Args $args -supports_check_mode $true
|
|
|
|
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
|
2017-06-14 16:34:46 +00:00
|
|
|
$diff_support = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool" -default $false
|
2017-02-21 06:01:09 +00:00
|
|
|
|
2017-06-01 23:43:08 +00:00
|
|
|
$timezone = Get-AnsibleParam -obj $params -name "timezone" -type "str" -failifempty $true
|
|
|
|
|
|
|
|
$result = @{
|
|
|
|
changed = $false
|
2017-06-14 16:34:46 +00:00
|
|
|
previous_timezone = $timezone
|
|
|
|
timezone = $timezone
|
2017-06-01 23:43:08 +00:00
|
|
|
}
|
2015-06-22 14:40:49 +00:00
|
|
|
|
2015-06-25 20:55:23 +00:00
|
|
|
Try {
|
|
|
|
# Get the current timezone set
|
2017-06-14 16:34:46 +00:00
|
|
|
$result.previous_timezone = $(tzutil.exe /g)
|
2017-02-21 06:01:09 +00:00
|
|
|
If ($LASTEXITCODE -ne 0) {
|
|
|
|
Throw "An error occurred when getting the current machine's timezone setting."
|
|
|
|
}
|
2015-06-25 20:55:23 +00:00
|
|
|
|
2017-06-14 16:34:46 +00:00
|
|
|
if ( $result.previous_timezone -eq $timezone ) {
|
|
|
|
Exit-Json $result "Timezone '$timezone' is already set on this machine"
|
2017-02-21 06:01:09 +00:00
|
|
|
} Else {
|
2017-06-14 16:34:46 +00:00
|
|
|
# Check that timezone is listed as an available timezone to the machine
|
2015-06-25 20:55:23 +00:00
|
|
|
$tzList = $(tzutil.exe /l)
|
2017-02-21 06:01:09 +00:00
|
|
|
If ($LASTEXITCODE -ne 0) {
|
|
|
|
Throw "An error occurred when listing the available timezones."
|
|
|
|
}
|
2017-06-14 16:34:46 +00:00
|
|
|
|
|
|
|
$tzExists = $false
|
2015-06-25 20:55:23 +00:00
|
|
|
ForEach ($tz in $tzList) {
|
|
|
|
If ( $tz -eq $timezone ) {
|
|
|
|
$tzExists = $true
|
|
|
|
break
|
2015-06-22 14:40:49 +00:00
|
|
|
}
|
2015-06-25 20:55:23 +00:00
|
|
|
}
|
2017-06-14 16:34:46 +00:00
|
|
|
if (-not $tzExists) {
|
|
|
|
Fail-Json $result "The specified timezone: $timezone isn't supported on the machine."
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($check_mode) {
|
|
|
|
$result.changed = $true
|
|
|
|
} else {
|
|
|
|
tzutil.exe /s "$timezone"
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
|
|
Throw "An error occurred when setting the specified timezone with tzutil."
|
|
|
|
}
|
2015-06-22 14:40:49 +00:00
|
|
|
|
2017-06-14 16:34:46 +00:00
|
|
|
$new_timezone = $(tzutil.exe /g)
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
|
|
Throw "An error occurred when getting the current machine's timezone setting."
|
|
|
|
}
|
2015-06-22 14:40:49 +00:00
|
|
|
|
2017-06-14 16:34:46 +00:00
|
|
|
if ($timezone -eq $new_timezone) {
|
2015-06-25 20:55:23 +00:00
|
|
|
$result.changed = $true
|
2015-06-22 14:40:49 +00:00
|
|
|
}
|
2017-06-14 16:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($diff_support) {
|
|
|
|
$result.diff = @{
|
|
|
|
before = "$($result.previous_timezone)`n"
|
|
|
|
after = "$timezone`n"
|
|
|
|
}
|
2015-06-25 20:55:23 +00:00
|
|
|
}
|
2015-06-22 14:40:49 +00:00
|
|
|
}
|
2017-02-21 06:01:09 +00:00
|
|
|
} Catch {
|
2015-06-25 20:55:23 +00:00
|
|
|
Fail-Json $result "Error setting timezone to: $timezone."
|
2015-06-22 14:40:49 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 06:01:09 +00:00
|
|
|
Exit-Json $result
|