Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows settings time #109

Open
wants to merge 88 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
81541a8
Add whatif support
Nov 2, 2024
b180c3a
Initial setup of Windows Setting Language
Gijsreyn Nov 4, 2024
51c2b63
Add examples
Gijsreyn Nov 4, 2024
83eb7a0
Update command to directly call language installer
Gijsreyn Nov 4, 2024
55b01a5
Update contribution
Gijsreyn Nov 4, 2024
c3847d2
Clarification
Gijsreyn Nov 4, 2024
251d306
Export test
Gijsreyn Nov 4, 2024
0f14f9d
Fix TODO
Gijsreyn Nov 4, 2024
b8dd560
Additional tip
Gijsreyn Nov 4, 2024
323d624
Initial module for Windows Update settings
Gijsreyn Nov 4, 2024
0ba034a
TODO task
Gijsreyn Nov 4, 2024
7ba6a69
Add assertions for delivery optimization
Gijsreyn Nov 4, 2024
81b64c3
Update the docs
Gijsreyn Nov 4, 2024
059b59b
Update table values
Gijsreyn Nov 4, 2024
4541bb4
Remove duplicate code
Gijsreyn Nov 4, 2024
102c763
Add whatif tests
Gijsreyn Nov 5, 2024
657e834
Merge branch 'main' into whatif-support
Gijsreyn Nov 5, 2024
5a06f85
Merge branch 'main' into windows-update-setting
Gijsreyn Nov 5, 2024
f8270f7
Small typo in docs
Gijsreyn Nov 5, 2024
5f01ee2
Initial setup for Microsoft.Windows.Setting.Time
Gijsreyn Nov 5, 2024
514868e
Fix up on validateset
Gijsreyn Nov 5, 2024
d74c29d
Merge branch 'main' into windows-setting-time
Gijsreyn Nov 5, 2024
506a589
Include Pester tests
Gijsreyn Nov 5, 2024
38a6184
Resolve remarks @Trenly
Gijsreyn Nov 5, 2024
e7a48b8
Merge branch 'main' into windows-settings-language
Gijsreyn Nov 5, 2024
e609482
Fix PSScriptAnalyzer rules
Gijsreyn Nov 5, 2024
48877f6
Comments
Gijsreyn Nov 5, 2024
41914ef
Resolve comments from Trenly
Nov 5, 2024
ba97203
Merge branch 'main' of https://github.com/Gijsreyn/winget-dsc into wh…
Gijsreyn Nov 6, 2024
8e9c8c5
Merge branch 'main' into windows-setting-time
Gijsreyn Nov 6, 2024
d816dc3
Re-add Invoke-Process
Gijsreyn Nov 6, 2024
cd15ed2
Fix spelling
Gijsreyn Nov 6, 2024
6370252
Spell checker test
Gijsreyn Nov 6, 2024
8539290
Merge branch 'main' into windows-update-setting
Gijsreyn Nov 6, 2024
fec4047
Merge branch 'main' of https://github.com/Gijsreyn/winget-dsc into wi…
Gijsreyn Nov 6, 2024
3bf7d6b
Merge branch 'windows-update-setting' of https://github.com/Gijsreyn/…
Gijsreyn Nov 6, 2024
7ed2c35
Add attributes for validation
Gijsreyn Nov 6, 2024
e7262b2
Merge branch 'whatif-support' of https://github.com/Gijsreyn/winget-d…
Gijsreyn Nov 6, 2024
6dfddf3
Spell checker words
Gijsreyn Nov 6, 2024
8d8ad12
Test
Gijsreyn Nov 6, 2024
1fbe8e2
Add line break for test
Gijsreyn Nov 6, 2024
872574f
Merge branch 'main' of https://github.com/Gijsreyn/winget-dsc into wi…
Gijsreyn Nov 6, 2024
70fae5c
Merge branch 'whatif-support' of https://github.com/Gijsreyn/winget-d…
Gijsreyn Nov 6, 2024
77cc13d
Merge branch 'windows-setting-time' of https://github.com/Gijsreyn/wi…
Gijsreyn Nov 6, 2024
5210dd3
Merge branch 'windows-update-setting' of https://github.com/Gijsreyn/…
Gijsreyn Nov 6, 2024
a00ec3e
Fix spelling
Gijsreyn Nov 6, 2024
35badfd
Temporary add names to pass spell checker
Gijsreyn Nov 6, 2024
b115da5
Adding workaround with enum and translation
Gijsreyn Nov 6, 2024
ab6e2ec
Merge branch 'windows-setting-time' of https://github.com/Gijsreyn/wi…
Gijsreyn Nov 6, 2024
85e10ef
Rewritten docs
Gijsreyn Nov 6, 2024
7644177
Merge branch 'windows-update-setting' of https://github.com/Gijsreyn/…
Gijsreyn Nov 6, 2024
ea7b053
Merge branch 'windows-settings-language' of https://github.com/Gijsre…
Gijsreyn Nov 6, 2024
fb5339a
Spelling
Gijsreyn Nov 6, 2024
cc1637b
Undo changes
Gijsreyn Nov 8, 2024
671a63e
Remove Python from merge
Gijsreyn Nov 8, 2024
213d157
Remove Python from merge
Gijsreyn Nov 8, 2024
5084eb4
Spelling added
Gijsreyn Nov 13, 2024
007bc8d
Spelling reverted
Gijsreyn Nov 13, 2024
bccf27c
Resolve conficts
Gijsreyn Nov 13, 2024
7d2ae98
Remove line break
Gijsreyn Nov 13, 2024
3548d89
Merge branch 'main' into windows-setting-time
Gijsreyn Nov 13, 2024
c2dad49
Add spelling
Gijsreyn Nov 13, 2024
b5f0eb8
Merge branch 'windows-setting-time' of https://github.com/Gijsreyn/wi…
Gijsreyn Nov 13, 2024
867a6b4
Update generic_terms.txt
Gijsreyn Nov 13, 2024
d31ed8a
Merge branch 'windows-setting-time' of https://github.com/Gijsreyn/wi…
Gijsreyn Nov 13, 2024
0ac57aa
Merge branch 'main' into windows-setting-time
Gijsreyn Nov 13, 2024
8b35f25
Fix markdownlint
Gijsreyn Nov 13, 2024
40ebfeb
Merge branch 'main' of https://github.com/Gijsreyn/winget-dsc into wi…
Gijsreyn Nov 13, 2024
44d7ecd
Merge branch 'windows-setting-time' of https://github.com/Gijsreyn/wi…
Gijsreyn Nov 13, 2024
edb3dcc
Resolve remarks and split class into two
Gijsreyn Nov 15, 2024
8c48fd3
Resolve conflict
Gijsreyn Nov 15, 2024
65bbcf1
Resolve conflict
Gijsreyn Nov 15, 2024
7af2f5e
Merge branch 'main' into windows-setting-time
Gijsreyn Nov 15, 2024
c83e56a
Add daylight saving
Gijsreyn Nov 15, 2024
3f47882
Spelling
Gijsreyn Nov 15, 2024
0e02a99
Minor change
Gijsreyn Nov 15, 2024
1c6bc34
Merge branch 'main' of https://github.com/Gijsreyn/winget-dsc into wi…
Dec 2, 2024
02fba3f
Fix timezone automatically
Gijsreyn Dec 7, 2024
9be2b65
Merge branch 'windows-setting-time' of https://github.com/Gijsreyn/wi…
Gijsreyn Dec 7, 2024
7be6cfe
Resolve spelling
Gijsreyn Dec 7, 2024
96ce50d
Add failing spelling
Gijsreyn Dec 7, 2024
1e577ee
Test location privacy when set timezone is automatically
Gijsreyn Dec 8, 2024
75cbef0
Add word
Gijsreyn Dec 8, 2024
9d0b2d0
Merge branch 'main' into windows-setting-time
Gijsreyn Dec 11, 2024
503fe50
Merge branch 'main' into windows-setting-time
Gijsreyn Dec 28, 2024
18feb8e
Remove tenary operator
Dec 28, 2024
94613f4
Create nonconfigurable property
Dec 28, 2024
686088e
Merge branch 'main' into windows-setting-time
Gijsreyn Jan 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/spelling/allow.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ websites
wekyb
Hmmss
MMdd
MMdd
immersivecontrolpanel
6 changes: 3 additions & 3 deletions .github/actions/spelling/expect/generic_terms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ ssh
usr
versioning
VGpu
<<<<<<< HEAD
Ntp
Systray
=======
ADDLOCAL
>>>>>>> b9c45c87b2a62b479e107979ab3714073a765e8e
dstoff
tzautoupdate
tzutil
22 changes: 11 additions & 11 deletions resources/Help/Microsoft.Windows.Setting.Time/TimeZone.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,39 @@ Module Name: Microsoft.Windows.Setting.Time
ms.date: 05/11/2024
online version:
schema: 2.0.0
title: Time
title: TimeZone
---

# Time

## SYNOPSIS

This `Time` DSC Resource allows you to manage the time zone, automatic time zone update, and system tray date/time visibility settings on a Windows machine.
This `TimeZone` DSC Resource allows you to manage the time zone, automatic time zone update, and system tray date/time visibility settings on a Windows machine.

## DESCRIPTION

This `Time` DSC Resource allows you to manage the time zone, automatic time zone update, and system tray date/time visibility settings on a Windows machine.
This `TimeZone` DSC Resource allows you to manage the time zone, automatic time zone update, and system tray date/time visibility settings on a Windows machine.

## PARAMETERS

| **Parameter** | **Attribute** | **DataType** | **Description** | **Allowed Values** |
| -------------------------- | ------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------- |
| `TimeZone` | Key | String | Specifies the time zone to set on the machine. | Any valid time zone identifier from `Get-TimeZone -ListAvailable` |
| `SetTimeZoneAutomatically` | Optional | Boolean | Whether to set the time zone automatically. The value should be a boolean. You can find the setting in `Settings -> Time & Language -> Date & Time -> Set time automatically. | `$true`, `$false` |
| `AdjustForDayLightSaving` | Optional | Boolean | Whether to adjust for daylight saving time. The value should be a boolean. You can find the setting in `Settings -> Time & Language -> Date & Time -> Adjust for daylight saving time automatically. | `$true`, `$false` |

| **Parameter** | **Attribute** | **DataType** | **Description** | **Allowed Values** |
| -------------------------- | ------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------- |
| `Id` | Key | String | Specifies the time zone to set on the machine. | Any valid time zone identifier from `Get-TimeZone -ListAvailable` |
| `SetTimeZoneAutomatically` | Optional | Boolean | Whether to set the time zone automatically. The value should be a boolean. You can find the setting in `Settings -> Time & Language -> Date & Time -> Set time zone automatically. | `$true`, `$false` |
| `SetTimeAutomatically` | Optional | Boolean | Whether to set the time automatically. The value should be a boolean. You can find the setting in `Settings -> Time & Language -> Date & Time -> Set time automatically. | `$true`, `$false` |
| `AdjustForDayLightSaving` | Optional | Boolean | Whether to adjust for daylight saving time. The value should be a boolean. You can find the setting in `Settings -> Time & Language -> Date & Time -> Adjust for daylight saving time automatically. | `$true`, `$false` |

## EXAMPLES

### EXAMPLE 1 - Set time zone to Pacific Standard Time

```powershell
Invoke-DscResource -Name Time -Method Set -Property @{ TimeZone = "Pacific Standard Time"}
Invoke-DscResource -Name TimeZone -ModuleName Microsoft.Windows.Setting.Time -Method Set -Property @{ Id = "Pacific Standard Time"}

# This example sets the time zone to Pacific Standard Time.
```

### EXAMPLE 2
### EXAMPLE 2 - Get current time zone

```powershell
Invoke-DscResource -Name Time -Method Get -Property {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
if ([string]::IsNullOrEmpty($env:TestRegistryPath)) {
$global:tzAutoUpdatePath = 'HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters'
$global:tzAutoUpdatePath = 'HKLM:\System\CurrentControlSet\Services\tzautoupdate'
$global:w32TimePath = 'HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters'
$global:timeZoneInformationPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\TimeZoneInformation'
} else {
$global:tzAutoUpdatePath = $global:timeZoneInformationPath = $env:TestRegistryPath
Expand Down Expand Up @@ -39,57 +40,107 @@ function Get-ValidTimeZone {

return $timeZoneId
}

function Test-LocationSettingPermission {
# On Windows 11, the HKLM is the location services, whereas HKCU is the Let apps access your location setting
$registryKeys = @('HKLM:\Software\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\location', 'HKCU:\Software\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\location', 'HKCU:\Software\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\location\windows.immersivecontrolpanel*')
foreach ($key in $registryKeys) {
$res = Get-ItemProperty -Path $key -Name 'Value' -ErrorAction SilentlyContinue
if ($res.Value -ne 'Allow') {
# TODO: Or should we throw an error?
return $false
}
}

return $true
Comment on lines +44 to +55
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that this should be its own separate DSC resource for just Location. I'm sure there are other settings that will depend on this, not just Time/TimeZone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partially, as it affects the TimeZone setting, what's your suggestion? A custom module to inherit those functions?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partially, as it affects the TimeZone setting, what's your suggestion? A custom module to inherit those functions?

Pardon my jumping into the conversation, but this felt like a good place. It seems to me that a number of these functions should be in a common PowerShell module (or similar location) of some kind. Maybe not so much this location permission testing function, as much as the Registry functions above. It seems like an unnecessary duplication to have every DSC resource independently define how to retrieve values from the Registry.

What do you think about some kind of centralization for common code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephengillie Welcome back and of course you can jump into the conversation. I fully agree with it. I think the DSC community does it also with the DscResource.Common module.

If you have any thoughts about implementing it, or want to have a discussion around it, I'm always open to it.

}

function Set-DaylightSavingTime {
param (
[Parameter()]
[switch] $Enable,

[Parameter(Mandatory = $true)]
[string] $Id
)

$command = $Enable.IsPresent ? ('tzutil /s "{0}"' -f $Id) : ('tzutil /s "{0}_dstoff"' -f $Id)

Invoke-Expression -Command $command

if ($LASTEXITCODE -ne 0) {
throw [System.Configuration.ConfigurationException]::new("Failed to set daylight saving time. Error: $LASTEXITCODE")
}
}
#endRegion Functions

#region Classes
<#
.SYNOPSIS
This `Time` DSC Resource allows you to manage the time zone, automatic time zone update, and system tray date/time visibility settings on a Windows machine.
This `TimeZone` DSC Resource allows you to manage the time zone, automatic time zone update, and system tray date/time visibility settings on a Windows machine.

.DESCRIPTION
This `Time` DSC Resource allows you to manage the time zone, automatic time zone update, and system tray date/time visibility settings on a Windows machine.
This `TimeZone` DSC Resource allows you to manage the time zone, automatic time zone update, and system tray date/time visibility settings on a Windows machine.

.PARAMETER TimeZone
The time zone to set on the machine. The value should be a valid time zone ID from the list of time zones (Get-TimeZone -ListAvailable).Id. The default value is the current time zone.
.PARAMETER Id
The Id to set on the machine. The value should be a valid time zone ID from the list of time zones (Get-TimeZone -ListAvailable).Id. The default value is the current time zone.

.PARAMETER SetTimeZoneAutomatically
Whether to set the time zone automatically. The value should be a boolean. You can find the setting in `Settings -> Time & Language -> Date & Time -> Set time automatically.
Whether to set the time zone automatically. The value should be a boolean. You can find the setting in `Settings -> Time & Language -> Date & Time -> Set time zone automatically.

.PARAMETER SetTimeAutomatically
Whether to set the time automatically. The value should be a boolean. You can find the setting in `Settings -> Time & Language -> Date & Time -> Set time automatically.

.PARAMETER AdjustForDaylightSaving
Whether to adjust for daylight saving time. The value should be a boolean. You can find the setting in `Settings -> Time & Language -> Date & Time -> Adjust for daylight saving time automatically.

.EXAMPLE
PS C:\> Invoke-DscResource -Name Time -Method Set -Property @{ TimeZone = "Pacific Standard Time"}
PS C:\> Invoke-DscResource -Name TimeZone -ModuleName Microsoft.Windows.Setting.Time -Method Set -Property @{ TimeZone = "Pacific Standard Time"}

This example sets the time zone to Pacific Standard Time.

.EXAMPLE
PS C:\> Invoke-DscResource -Name Time -Method Get -Property {}
PS C:\> Invoke-DscResource -Name TimeZone -ModuleName Microsoft.Windows.Setting.Time -Method Get -Property {}

This example gets the current time settings on the machine.
#>
[DscResource()]
class TimeZone {
[DscProperty(Key)]
[string] $TimeZone
[string] $Id

[DscProperty()]
[nullable[bool]] $SetTimeZoneAutomatically

[DscProperty()]
[nullable[bool]] $SetTimeAutomatically

Comment on lines +115 to +117
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about moving things like SetTimeAutomatically to a separate DSC resource called Time.
When I look at the settings page for TimeZone, I only see SetTimeZoneAutomatically and if that is not true, then we have the option to set the TimeZoneId and DaylightSaving. I think this DSC resource should try to replicate what we see in the settings app. I think this is the best way to prevent scope creep for this individual DSC resources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's precisely why I did it into one DSC resource. They bite each other. Even if we separate them both, we have to check what options are already enabled in both cases. If we set the Timezone to automatically, we might need to clean up the manual settings and vice versa.

[DscProperty()]
[nullable[bool]] $AdjustForDaylightSaving

static hidden [string] $SetTimeZoneAutomaticallyProperty = 'Type'
static hidden [string] $SetTimeZoneAutomaticallyProperty = 'Start'
static hidden [string] $SetTimeAutomaticallyProperty = 'Type'
static hidden [string] $AdjustForDaylightSavingProperty = 'DynamicDaylightTimeDisabled'
static hidden [string] $NtpEnabled = 'NTP'
static hidden [string] $NtpDisabled = 'NoSync'
static hidden [bool] $SupportsDaylightSavingProperty = $false
Gijsreyn marked this conversation as resolved.
Show resolved Hide resolved

TimeZone() {
$this.TimeZone = (Get-TimeZone).Id
$timeZone = Get-TimeZone
$this.Id = $timeZone.Id

# We set the SupportsDaylightSavingProperty to the value that is supported by the current time zone
[TimeZone]::SupportsDaylightSavingProperty = $timeZone.SupportsDaylightSavingTime ? $true : $false
Gijsreyn marked this conversation as resolved.
Show resolved Hide resolved
}

[TimeZone] Get() {
$currentState = [TimeZone]::new()
$currentState.SetTimeZoneAutomatically = [TimeZone]::GetTimeZoneAutoUpdateStatus()
$currentState.AdjustForDaylightSaving = [TimeZone]::GetDayLightSavingStatus()
$currentState.SetTimeZoneAutomatically = [TimeZone]::GetTimeZoneAutomaticallyStatus()
$currentState.SetTimeAutomatically = [TimeZone]::GetTimeAutomaticallyStatus()

if ($currentState::SupportsDaylightSavingProperty) {
$currentState.AdjustForDaylightSaving = [TimeZone]::GetDayLightSavingStatus()
}
Comment on lines +143 to +145
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value is not dependent on whether the time zone supports daylight saving or not. This should just be a registry check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't totally get you here. It is checking the registry with the GetDayLightSavingStatus() method?


return $currentState
}
Expand All @@ -101,55 +152,82 @@ class TimeZone {

$currentState = $this.Get()

if ($currentState.TimeZone -ne $this.TimeZone) {
Set-TimeZone -Id (Get-ValidTimeZone -TimeZone $this.TimeZone)
if ($currentState.Id -ne $this.Id) {
Set-TimeZone -Id (Get-ValidTimeZone -TimeZone $this.Id)
}

if (($null -ne $this.SetTimeZoneAutomatically) -and ($this.SetTimeZoneAutomatically -ne $currentState.SetTimeZoneAutomatically)) {
$desiredState = $this.SetTimeZoneAutomatically ? 3 : 4

if (Test-LocationSettingPermission) {
Set-ItemProperty -Path $global:tzAutoUpdatePath -Name ([TimeZone]::SetTimeZoneAutomaticallyProperty) -Value $desiredState
}
}

if ($currentState.SetTimeZoneAutomatically -ne $this.SetTimeZoneAutomatically) {
if (($null -ne $this.SetTimeAutomatically) -and ($this.SetTimeAutomatically -ne $currentState.SetTimeAutomatically)) {
$desiredState = $this.SetTimeAutomatically ? [TimeZone]::NtpEnabled : [TimeZone]::NtpDisabled

Set-ItemProperty -Path $global:tzAutoUpdatePath -Name ([TimeZone]::SetTimeZoneAutomaticallyProperty) -Value $desiredState
Set-ItemProperty -Path $global:w32TimePath -Name ([TimeZone]::SetTimeAutomaticallyProperty) -Value $desiredState
}

if ($currentState.AdjustForDaylightSaving -ne $this.AdjustForDaylightSaving) {
$desiredState = $this.AdjustForDaylightSaving ? 0 : 1
if ($currentState::SupportsDaylightSavingProperty -and ($null -ne $this.AdjustForDaylightSaving)) {
$desiredState = @{
Id = $this.Id
Enable = $this.AdjustForDaylightSaving
}

Set-ItemProperty -Path $global:timeZoneInformationPath -Name ([TimeZone]::AdjustForDaylightSavingProperty) -Value $desiredState
if ($this.AdjustForDaylightSaving -ne $currentState.AdjustForDaylightSaving) {
# Falling back on tzutil because the amount of registry keys to modify is too high, plus difficult to determine which one to modify
Set-DaylightSavingTime @desiredState
}
Gijsreyn marked this conversation as resolved.
Show resolved Hide resolved
}
}

[bool] Test() {
$currentState = $this.Get()

if (($null -ne $this.TimeZone) -and ($this.TimeZone -ne $currentState.TimeZone)) {
if ($this.Id -ne $currentState.Id) {
return $false
}

if (($null -ne $this.SetTimeZoneAutomatically) -and ($this.SetTimeZoneAutomatically -ne $currentState.SetTimeZoneAutomatically)) {
return $false
}

if (($null -ne $this.AdjustForDaylightSaving) -and ($this.AdjustForDaylightSaving -ne $currentState.AdjustForDaylightSaving)) {
if (($null -ne $this.SetTimeAutomatically) -and ($this.SetTimeAutomatically -ne $currentState.SetTimeAutomatically)) {
return $false
}

if ($currentState::SupportsDaylightSavingProperty -and ($null -ne $this.AdjustForDaylightSaving)) {
if ($this.AdjustForDaylightSaving -ne $currentState.AdjustForDaylightSaving) {
return $false
}
}

return $true
}

#region Time helper functions
static [bool] GetTimeZoneAutoUpdateStatus() {
# key should actually always be present, but we'll check anyway
#region TimeZone helper functions
static [bool] GetTimeZoneAutomaticallyStatus() {
$keyValue = TryGetRegistryValue -Key $global:tzAutoUpdatePath -Property ([TimeZone]::SetTimeZoneAutomaticallyProperty)
if ($null -eq $keyValue) {
return $true
} else {
return ($keyValue -eq 1)
return ($keyValue -eq 3)
}
}

static [bool] GetTimeAutomaticallyStatus() {
$keyValue = TryGetRegistryValue -Key $global:w32TimePath -Property ([TimeZone]::SetTimeAutomaticallyProperty)
if ($null -eq $keyValue) {
return $true
} else {
return ($keyValue -eq 'NTP')
}
}

static [bool] GetDayLightSavingStatus() {
# key should actually always be present, but we'll check anyway
$keyValue = TryGetRegistryValue -Key $global:timeZoneInformationPath -Property ([TimeZone]::SetTimeZoneAutomaticallyProperty)
$keyValue = TryGetRegistryValue -Key $global:timeZoneInformationPath -Property ([TimeZone]::AdjustForDaylightSavingProperty)
if ($null -eq $keyValue) {
return $true
} else {
Expand All @@ -168,7 +246,7 @@ class TimeZone {

return $parameters
}
#endRegion Time helper functions
#endRegion TimeZone helper functions
}

if ([string]::IsNullOrEmpty($env:TestRegistryPath)) {
Expand Down Expand Up @@ -302,6 +380,7 @@ class Clock {
return $parameters
}
#endRegion Clock helper functions

}
#endRegion Classes

Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,38 @@ Describe 'List available DSC resources' {
}

Describe 'TimeZone' {
It 'Set Time Zone' {
$desiredState = @{ TimeZone = 'Pacific Standard Time' }
It 'Set Time Zone only by Id' {
$desiredState = @{ Id = 'Pacific Standard Time' }

Invoke-DscResource -Name TimeZone -ModuleName Microsoft.Windows.Setting.Time -Method Set -Property $desiredState

$finalState = Invoke-DscResource -Name TimeZone -ModuleName Microsoft.Windows.Setting.Time -Method Test -Property @{}
$finalState.InDesiredState | Should -Be $true
}

It 'Set automatic time zone not synchronize' {
$object = [TimeZone]::new()
$object.SetTimeZoneAutomatically = 'NoSync'
It 'Set Time Zone automatically' {
$desiredState = @{ Id = 'W. Europe Standard Time'; SetTimeZoneAutomatically = $true }

# Set the state
$object.Set()
Invoke-DscResource -Name TimeZone -ModuleName Microsoft.Windows.Setting.Time -Method Set -Property $desiredState

$finalState = Invoke-DscResource -Name TimeZone -ModuleName Microsoft.Windows.Setting.Time -Method Test -Property @{}
$finalState.InDesiredState | Should -Be $true
}

# Test the state
$object.Test() | Should -Be $true
It 'Disable Time automatically' {
$desiredState = @{ Id = 'W. Europe Standard Time'; SetTimeAutomatically = $false }

Invoke-DscResource -Name TimeZone -ModuleName Microsoft.Windows.Setting.Time -Method Set -Property $desiredState

$finalState = Invoke-DscResource -Name TimeZone -ModuleName Microsoft.Windows.Setting.Time -Method Test -Property @{}
$finalState.InDesiredState | Should -Be $true
}

It 'Disable daylight saving' {
$desiredState = @{
TimeZone = (Get-TimeZone).Id
AdjustForDaylightSaving = $false
Id = (Get-TimeZone -ListAvailable | Where-Object { $_.SupportsDaylightSavingTime -eq $true } | Select-Object -First 1 -ExpandProperty Id)
SetTimeZoneAutomatically = $false
AdjustForDaylightSaving = $false
}

Invoke-DscResource -Name TimeZone -ModuleName Microsoft.Windows.Setting.Time -Method Set -Property $desiredState
Expand Down
Loading