Skip to content

Commit

Permalink
Merge pull request #7 from rsWinAutomationSupport/staging
Browse files Browse the repository at this point in the history
Releasing 2.2.0
  • Loading branch information
Alexei Andreyev committed Feb 16, 2016
2 parents b4bdd98 + e54f16b commit d3bbd6f
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 24 deletions.
22 changes: 22 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Set default behaviour, in case users don't have core.autocrlf set.
text=auto

# Explicitly declare text files we want to always be normalized and converted
# to native line endings on checkout.
.md text
.gitattributes text

# Declare files that will always have CRLF line endings on checkout.
.ps1 text eol=crlf
.psm1 text eol=crlf
.psd1 text eol=crlf
.psc1 text eol=crlf
.ps1xml text eol=crlf
.clixml text eol=crlf
.xml text eol=crlf
.mof text eol=crlf
.txt text eol=crlf

# Denote all files that are truly binary and should not be mergeable.
.dll binary
.exe binary
142 changes: 122 additions & 20 deletions DSCResources/RS_rsTime/RS_rsTime.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ function Get-TargetResource
(
[parameter(Mandatory = $true)]
[System.String]
$TimeZone
$TimeZone,
[string[]]
$PeerList
)

[string]$CurrentTZ = Invoke-Command {tzutil /g}
Write-Verbose "Current TZ Setting is $CurrentTZ"
Write-Verbose "Current system time setting is: $CurrentTZ"

$CurrentPeers = @()
((Invoke-Command {w32tm /query /peers}).Split("`n") | Where-Object { $_.contains("Peer:") }) | ForEach-Object {$CurrentPeers += ($_.split()[1])}

if ($CurrentPeers)
{
Write-Verbose "Current manual peer list setting is: $CurrentPeers"
}

$returnValue = @{
TimeZone = $CurrentTZ
PeerList = $CurrentPeers
}

$returnValue
$returnValue
}


Expand All @@ -27,18 +37,73 @@ function Set-TargetResource
(
[parameter(Mandatory = $true)]
[System.String]
$TimeZone
$TimeZone,
[string[]]
$PeerList
)

try
{
[string]$CurrentTZ = Invoke-Command {tzutil /g}
Write-Verbose "Setting TZ Setting to $TimeZone"
Invoke-Command {tzutil /s $TimeZone}
$CurrentSetting = (Get-TargetResource -TimeZone $TimeZone -PeerList $PeerList)

if ($CurrentSetting.TimeZone -ne $TimeZone)
{
Write-Verbose "Setting Time Zone to $TimeZone"
Invoke-Command {tzutil /s $TimeZone}
}

if ($PeerList)
{
#[array]$PeerList = $PeerList.Split()
$wmiW32Time = (Get-CimInstance -ClassName win32_service | Where-Object {$_.Name -eq "W32Time"})

# Check if W32Time service was unregistered, but not stopped
if (($wmiW32Time.startmode -eq "Disabled") -and ($wmiW32Time.State -eq "Running"))
{
Write-Verbose "Windows Time service might have been unregistered, but not stopped... Stopping now..."
Stop-Service w32time -ErrorAction SilentlyContinue
}

# Check if w32time service is not registered
if (-not [bool](Get-Service w32time -ErrorAction SilentlyContinue))
{
Write-Verbose "Registering and starting Windows Time service"
Invoke-Command {w32tm /register}
}

if ((Get-Service w32time).Status -ne "Running")
{
Write-Verbose "Starting Windows Times service"
Start-Service w32Time
}

$wmiW32Time = (Get-CimInstance -ClassName win32_service | Where-Object {$_.Name -eq "W32Time"})
if ($wmiW32Time.Startmode -ne "Auto")
{
Set-Service w32Time -StartupType "Automatic"
}

# Check if configured peer list matches DSC setting & correct as needed
if ([bool](Compare-Object $CurrentSetting.PeerList $PeerList))
{
Write-Verbose "Configuring Windows Time service"
Invoke-Command {w32tm /config /manualpeerlist:$PeerList /syncfromflags:manual /update}
}
}
else
{
# Unregister w32time service
if ([bool](Get-Service w32time -ErrorAction SilentlyContinue))
{
Write-Verbose "Unregistering Windows Time service"
Stop-Service w32time
Invoke-Command {w32tm /unregister}
}
}
}
catch
{

}
}

Expand All @@ -51,32 +116,69 @@ function Test-TargetResource
(
[parameter(Mandatory = $true)]
[System.String]
$TimeZone
$TimeZone,
[string[]]
$PeerList
)

try
{
[string]$CurrentTZ = Invoke-Command {tzutil /g}
Write-Verbose "Current TZ Setting is $CurrentTZ"
$CurrentSetting = (Get-TargetResource -TimeZone $TimeZone -PeerList $PeerList)

if ($CurrentTZ -like $TimeZone)
if ($CurrentSetting.TimeZone -eq $TimeZone)
{
Write-Verbose "TZ setting is consistent"
$result = $true
if ($PeerList)
{
# Check if w32time service is registered, running and set to auto-start
if ([bool](Get-Service w32time -ErrorAction SilentlyContinue))
{
$w32TimeMode = (Get-CimInstance -ClassName win32_service -Property startmode, name | Where-Object {$_.Name -eq "W32Time"} | Select-Object startmode).Startmode
if ($w32TimeMode -ne "Auto")
{
Write-Verbose "W32Time service is not set to Auto-start"
return $false
}

if ((Get-Service w32time).Status -ne "Running")
{
Write-Verbose "W32Time service is not running"
return $false
}
}
else
{
Write-Verbose "W32Time service is not registered"
return $false
}

if ([bool](Compare-Object $CurrentSetting.PeerList $PeerList.Split() ))
{
Write-Verbose "Current manual peer list does not match DSC settings"
return $false
}
}
else
{
if ([bool](Get-Service w32time -ErrorAction SilentlyContinue))
{
Write-Verbose "Windows Time service is registered, which does not match current DSC settigns"
return $false
}
}

Write-Verbose "Timezone settings are consistent"
return $true
}
else
{
Write-Verbose "TZ setting is inconsistent"
$result = $false
Write-Verbose "Timezone settings are not consistent"
return $false
}

}
catch
{

}

$result
}

Export-ModuleMember -Function *-TargetResource
Binary file modified DSCResources/RS_rsTime/RS_rsTime.schema.mof
Binary file not shown.
Binary file modified DSCResources/RS_rsUserLocale/RS_rsUserLocale.psm1
Binary file not shown.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ rsInternationalSettings DSC Module

##Changelog

######v0.0.1
######2.2.0
Added support for setting manual peer list for NTP servers in rsTime using the PeerList variable
######2.1.0
Added support for setting time zone, culture, system local and all local (including default) user profile settings.

####To-do:
Add support for provision of an optional time server parameter

##Syntax##

See usage examples below...
Expand Down Expand Up @@ -64,7 +63,15 @@ This will force all system and user settings to UK codepage, including input set
InputLocaleID = "00000809"
}

**Add manual NTP peer server list**

To override the default time.windows.com NTP server or the automatic AD member time synchronisation, you can specify the PeerList parameter for rsTime, which will register the Windows Time service and configure it accordingly. Please note that omitting this parameter will always result in Windows Time Service to be unregistered if one is running already.

rsTime time
{
TimeZone = "GMT Standard Time"
PeerList = @("0.pool.ntp.org","1.pool.ntp.org","2.pool.ntp.org")
}

###Acceptable parameter values

Expand All @@ -78,6 +85,9 @@ Use the following PowerShell command to list all available options:
System time zone name description. Ex "GMT Standard Time"
Use the `tzutil /l` command to list all possible options.

#####$PeerList
An array of NTP servers to configure the local Windows Time service.

#####$LocationID
Geographical System location ID (GEOID). Refer to [Table of Geographical Locations](http://msdn.microsoft.com/en-us/library/windows/desktop/dd374073(v=vs.85).aspx) for full list of possible options.

Expand Down
Binary file modified rsInternationalSettings.psd1
Binary file not shown.

0 comments on commit d3bbd6f

Please sign in to comment.