Skip to content

Commit

Permalink
Merge pull request #288 from PlagueHO/Issue-50-B
Browse files Browse the repository at this point in the history
Add SystemLocale Resource - Fixes #50
  • Loading branch information
PlagueHO authored Nov 25, 2019
2 parents 1a5e100 + 4825448 commit f709d86
Show file tree
Hide file tree
Showing 12 changed files with 520 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text eol=crlf
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
- SmbShare:
- Add parameter ScopeName to support creating shares in a different
scope. Fixes [Issue #284](https://github.com/PowerShell/ComputerManagementDsc/issues/284)
- Added `.gitattributes` to ensure CRLF is used when pulling repository - Fixes
[Issue #290](https://github.com/PowerShell/ComputerManagementDsc/issues/290).
- SystemLocale:
- Migrated SystemLocale from [SystemLocaleDsc](https://github.com/PowerShell/SystemLocaleDsc).
- RemoteDesktopAdmin:
- Correct Context messages in integration tests by adding 'When'.

## 7.1.0.0

Expand Down
194 changes: 194 additions & 0 deletions DSCResources/MSFT_SystemLocale/MSFT_SystemLocale.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
$modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'Modules'

# Import the ComputerManagementDsc Common Modules
Import-Module -Name (Join-Path -Path $modulePath `
-ChildPath (Join-Path -Path 'ComputerManagementDsc.Common' `
-ChildPath 'ComputerManagementDsc.Common.psm1')) -Force

# Import the ComputerManagementDsc Resource Helper Module
Import-Module -Name (Join-Path -Path $modulePath `
-ChildPath (Join-Path -Path 'ComputerManagementDsc.Common' `
-ChildPath 'ComputerManagementDsc.Common.psm1'))

# Import Localization Strings
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_SystemLocale'

<#
.SYNOPSIS
Returns the current System Local on the node.
.PARAMETER IsSingleInstance
Specifies the resource is a single instance, the value must be 'Yes'.
.PARAMETER SystemLocale
Specifies the System Locale.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[ValidateSet('Yes')]
[System.String]
$IsSingleInstance,

[Parameter(Mandatory = $true)]
[System.String]
$SystemLocale
)

Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.GettingSystemLocaleMessage)
) -join '' )

# Get the current System Locale
$currentSystemLocale = Get-WinSystemLocale `
-ErrorAction Stop

# Generate the return object.
$returnValue = @{
IsSingleInstance = $IsSingleInstance
SystemLocale = $currentSystemLocale.Name
}

return $returnValue
} # Get-TargetResource

<#
.SYNOPSIS
Sets the current System Locale on the node.
.PARAMETER IsSingleInstance
Specifies the resource is a single instance, the value must be 'Yes'.
.PARAMETER SystemLocale
Specifies the System Locale.
#>
function Set-TargetResource
{
# Suppressing this rule because $global:DSCMachineStatus is used to trigger a reboot when there are pending changes.
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')]
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[ValidateSet('Yes')]
[System.String]
$IsSingleInstance,

[Parameter(Mandatory = $true)]
[System.String]
$SystemLocale
)

Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.SettingSystemLocaleMessage)
) -join '' )

# Get the current System Locale
$currentSystemLocale = Get-WinSystemLocale `
-ErrorAction Stop

if ($currentSystemLocale.Name -ne $SystemLocale)
{
Set-WinSystemLocale `
-SystemLocale $SystemLocale `
-ErrorAction Stop

$global:DSCMachineStatus = 1

Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.SystemLocaleUpdatedMessage -f $SystemLocale)
) -join '' )
}
} # Set-TargetResource

<#
.SYNOPSIS
Tests if the current System Locale on the node needs to be changed.
.PARAMETER IsSingleInstance
Specifies the resource is a single instance, the value must be 'Yes'.
.PARAMETER SystemLocale
Specifies the System Locale.
.OUTPUTS
Returns false if the System Locale needs to be changed or true if it is correct.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[ValidateSet('Yes')]
[System.String]
$IsSingleInstance,

[Parameter(Mandatory = $true)]
[System.String]
$SystemLocale
)

Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.TestingSystemLocaleMessage)
) -join '' )

if (-not (Test-SystemLocaleValue -SystemLocale $SystemLocale))
{
New-InvalidArgumentException `
-Message ($script:localizedData.InvalidSystemLocaleError -f $SystemLocale) `
-ArgumentName 'SystemLocale'
} # if

# Get the current System Locale
$currentSystemLocale = Get-WinSystemLocale `
-ErrorAction Stop

if ($currentSystemLocale.Name -ne $SystemLocale)
{
Write-Verbose -Message ( @(
"$($MyInvocation.MyCommand): "
$($script:localizedData.SystemLocaleParameterNeedsUpdateMessage -f `
$currentSystemLocale.Name,$SystemLocale)
) -join '' )

return $false
}
return $true
} # Test-TargetResource

<#
.SYNOPSIS
Checks the provided System Locale against the list of valid cultures.
.PARAMETER SystemLocale
The System Locale to check the validitiy of.
#>
function Test-SystemLocaleValue
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$SystemLocale
)

$validCultures = [System.Globalization.CultureInfo]::GetCultures(`
[System.Globalization.CultureTypes]::AllCultures`
).name

return ($SystemLocale -in $validCultures)
}

Export-ModuleMember -Function *-TargetResource
6 changes: 6 additions & 0 deletions DSCResources/MSFT_SystemLocale/MSFT_SystemLocale.schema.mof
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[ClassVersion("1.0.0.0"), FriendlyName("SystemLocale")]
class MSFT_SystemLocale : OMI_BaseResource
{
[Key, Description("Specifies the resource is a single instance, the value must be 'Yes'"), ValueMap{"Yes"}, Values{"Yes"}] String IsSingleInstance;
[Required, Description("Specifies the System Locale.")] String SystemLocale;
};
10 changes: 10 additions & 0 deletions DSCResources/MSFT_SystemLocale/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Description

Ths resource is used set the system locale on a Windows machine.

To get a list of valid Windows System Locales use the command:
`[System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::AllCultures).name`

If the System Locale is changed by this resource, it will require the node
to reboot. If the LCM is not configured to allow restarting, the configuration
will not be able to be applied until a manual restart occurs.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# culture="en-US"
ConvertFrom-StringData -StringData @'
GettingSystemLocaleMessage = Getting Windows system locale.
SettingSystemLocaleMessage = Setting Windows system locale.
SystemLocaleUpdatedMessage = Windows system locale updated to "{0}". A system restart is required.
TestingSystemLocaleMessage = Testing Windows system locale.
SystemLocaleParameterNeedsUpdateMessage = Windows system locale is "{0}" but should be "{1}". Change required.
InvalidSystemLocaleError = The Windows system locale "{0}" is invalid.
'@
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<#PSScriptInfo
.VERSION 1.0.0
.GUID 66476d02-bd04-4d5d-ac49-d64724716f41
.AUTHOR Microsoft Corporation
.COMPANYNAME Microsoft Corporation
.COPYRIGHT (c) Microsoft Corporation. All rights reserved.
.TAGS DSCConfiguration
.LICENSEURI https://github.com/PowerShell/ComputerManagementDsc/blob/master/LICENSE
.PROJECTURI https://github.com/PowerShell/ComputerManagementDsc
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES First version.
.PRIVATEDATA 2016-Datacenter,2016-Datacenter-Server-Core
#>

#Requires -module ComputerManagementDsc

<#
.DESCRIPTION
This example will set the System Locale of LocalHost to 'ja-JP'.
To use this example, run it using PowerShell.
#>
Configuration SystemLocale_SetSystemLocale_Config
{
param
(
[Parameter()]
[System.String[]]
$NodeName = 'localhost'
)

Import-DSCResource -ModuleName ComputerManagementDsc

Node $NodeName
{
SystemLocale SystemLocaleExample
{
IsSingleInstance = 'Yes'
SystemLocale = 'ja-JP'
}
}
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ The **ComputerManagementDsc** module contains the following resources:
containing this resource may be compiled on Windows Server 2008 R2/Windows 7 but
can not be applied._
- **SmbServerConfiguration**: this resource is used to configure the SMB Server
settings on the local machine.
settings on the local machine.
- **SmbShare**: this resource is used to manage SMB shares on a machine.
- **SystemLocale**: this resource is used to set the system locale on a
Windows machine
- **TimeZone**: this resource is used for setting the time zone on a machine.
- **VirtualMemory**: allows configuration of properties of the paging file on
the local computer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ try
$script:winStationsRegistryKey = 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp'

Describe "$($script:dscResourceName)_Integration" {

Context 'Set Remote Desktop for Administration to Denied' {
Context 'When setting Remote Desktop for Administration to Denied' {
$CurrentConfig = 'setToDenied'
$ConfigDir = (Join-Path -Path $TestDrive -ChildPath $CurrentConfig)
$ConfigMof = (Join-Path -Path $ConfigDir -ChildPath 'localhost.mof')
Expand Down Expand Up @@ -61,7 +60,7 @@ try
}
}

Context 'Set Remote Desktop for Administration to Allowed' {
Context 'When setting Remote Desktop for Administration to Allowed' {
$CurrentConfig = 'setToAllowed'
$ConfigDir = (Join-Path -Path $TestDrive -ChildPath $CurrentConfig)
$ConfigMof = (Join-Path -Path $ConfigDir -ChildPath 'localhost.mof')
Expand Down Expand Up @@ -93,7 +92,7 @@ try
}
}

Context 'Set Remote Desktop for Administration to Allowed with Secure Authentication' {
Context 'When settting Remote Desktop for Administration to Allowed with Secure Authentication' {
$CurrentConfig = 'setToAllowedSecure'
$ConfigDir = (Join-Path -Path $TestDrive -ChildPath $CurrentConfig)
$ConfigMof = (Join-Path -Path $ConfigDir -ChildPath 'localhost.mof')
Expand Down Expand Up @@ -127,7 +126,7 @@ try
}
}

Context 'Set Remote Desktop for Administration to Allowed with NonSecure Authentication' {
Context 'When settting Remote Desktop for Administration to Allowed with NonSecure Authentication' {
$CurrentConfig = 'setToAllowedNonSecure'
$ConfigDir = (Join-Path -Path $TestDrive -ChildPath $CurrentConfig)
$ConfigMof = (Join-Path -Path $ConfigDir -ChildPath 'localhost.mof')
Expand Down
Loading

0 comments on commit f709d86

Please sign in to comment.