1 #!powershell 2 3 # Copyright: (c) 2015, Phil Schwartz <schwartzmx@gmail.com> 4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 5 6 #Requires -Module Ansible.ModuleUtils.Legacy 7 8 $params = Parse-Args $args -supports_check_mode $true 9 $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false 10 $diff_support = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool" -default $false 11 12 $timezone = Get-AnsibleParam -obj $params -name "timezone" -type "str" -failifempty $true 13 14 $result = @{ 15 changed = $false 16 previous_timezone = $timezone 17 timezone = $timezone 18 } 19 20 Try { 21 # Get the current timezone set 22 $result.previous_timezone = $(tzutil.exe /g) 23 If ($LASTEXITCODE -ne 0) { 24 Throw "An error occurred when getting the current machine's timezone setting." 25 } 26 27 if ( $result.previous_timezone -eq $timezone ) { 28 Exit-Json $result "Timezone '$timezone' is already set on this machine" 29 } Else { 30 # Check that timezone is listed as an available timezone to the machine 31 $tzList = $(tzutil.exe /l).ToLower() 32 If ($LASTEXITCODE -ne 0) { 33 Throw "An error occurred when listing the available timezones." 34 } 35 36 $tzExists = $tzList.Contains(($timezone -Replace '_dstoff').ToLower()) 37 if (-not $tzExists) { 38 Fail-Json $result "The specified timezone: $timezone isn't supported on the machine." 39 } 40 41 if ($check_mode) { 42 $result.changed = $true 43 } else { 44 tzutil.exe /s "$timezone" 45 if ($LASTEXITCODE -ne 0) { 46 Throw "An error occurred when setting the specified timezone with tzutil." 47 } 48 49 $new_timezone = $(tzutil.exe /g) 50 if ($LASTEXITCODE -ne 0) { 51 Throw "An error occurred when getting the current machine's timezone setting." 52 } 53 54 if ($timezone -eq $new_timezone) { 55 $result.changed = $true 56 } 57 } 58 59 if ($diff_support) { 60 $result.diff = @{ 61 before = "$($result.previous_timezone)`n" 62 after = "$timezone`n" 63 } 64 } 65 } 66 } Catch { 67 Fail-Json $result "Error setting timezone to: $timezone." 68 } 69 70 Exit-Json $result 71