removed hardcoded paths, check for .exe error exit code, use get-attr for required param check.

pull/18777/head
Phil 9 years ago committed by Matt Clay
parent f1579a1207
commit 55c671a18b

@ -26,44 +26,45 @@ $result = New-Object psobject @{
changed = $false changed = $false
} }
If ($params.timezone) { $timezone = Get-Attr -obj $params -name timezone -failifempty $true -resultobj $result
Try {
# Get the current timezone set
$currentTZ = $(C:\Windows\System32\tzutil /g)
If ( $currentTZ -eq $params.timezone ) { Try {
Exit-Json $result "$params.timezone is already set on this machine" # Get the current timezone set
} $currentTZ = $(tzutil.exe /g)
Else { If ($LASTEXITCODE -ne 0) { Throw "An error occured when getting the current machine's timezone setting." }
$tzExists = $false
#Check that timezone can even be set (if it is listed from tzutil as an available timezone to the machine) If ( $currentTZ -eq $timezone ) {
$tzList = $(C:\Windows\System32\tzutil /l) Exit-Json $result "$timezone is already set on this machine"
ForEach ($tz in $tzList) { }
If ( $tz -eq $params.timezone ) { Else {
$tzExists = $true $tzExists = $false
break #Check that timezone can even be set (if it is listed from tzutil as an available timezone to the machine)
} $tzList = $(tzutil.exe /l)
If ($LASTEXITCODE -ne 0) { Throw "An error occured when listing the available timezones." }
ForEach ($tz in $tzList) {
If ( $tz -eq $timezone ) {
$tzExists = $true
break
} }
}
If ( $tzExists ) { If ( $tzExists ) {
C:\Windows\System32\tzutil /s "$params.timezone" tzutil.exe /s "$timezone"
$newTZ = $(C:\Windows\System32\tzutil /g) If ($LASTEXITCODE -ne 0) { Throw "An error occured when setting the specified timezone with tzutil." }
$newTZ = $(tzutil.exe /g)
If ($LASTEXITCODE -ne 0) { Throw "An error occured when getting the current machine's timezone setting." }
If ( $params.timezone -eq $newTZ ) { If ( $timezone -eq $newTZ ) {
$result.changed = $true $result.changed = $true
}
}
Else {
Fail-Json $result "The specified timezone: $params.timezone isn't supported on the machine."
} }
} }
} Else {
Catch { Fail-Json $result "The specified timezone: $timezone isn't supported on the machine."
Fail-Json $result "Error setting timezone to: $params.timezone." }
} }
} }
Else { Catch {
Fail-Json $result "Parameter: timezone is required." Fail-Json $result "Error setting timezone to: $timezone."
} }

Loading…
Cancel
Save