From 2f611504941423d984b00f4eaf55d4d2df7b2d8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raimund=20Andr=C3=A9e=20=5BMSFT=5D?= Date: Wed, 29 Nov 2023 19:42:08 +0100 Subject: [PATCH] Sync with CommonTasks (#15) --- CHANGELOG.md | 4 ++ .../WindowsFeatures.schema.psm1 | 50 +++++++++++++++++-- .../WindowsOptionalFeatures.schema.psm1 | 7 +-- .../WindowsServices.schema.psm1 | 36 +++++++------ .../Assets/Config/WindowsFeatures.yml | 8 +++ .../Assets/Config/WindowsServices.yml | 5 +- 6 files changed, 88 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9984a1b..39d8e22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - DscTagging: Added parameter 'BuildNumber'. +### Changed + +- Sync with CommonTasks. + ## [0.8.3] - 2023-03-16 ### Changed diff --git a/source/DSCResources/WindowsFeatures/WindowsFeatures.schema.psm1 b/source/DSCResources/WindowsFeatures/WindowsFeatures.schema.psm1 index fff48d3..58d198b 100644 --- a/source/DSCResources/WindowsFeatures/WindowsFeatures.schema.psm1 +++ b/source/DSCResources/WindowsFeatures/WindowsFeatures.schema.psm1 @@ -1,11 +1,28 @@ configuration WindowsFeatures { param ( - [Parameter(Mandatory = $true)] + [Parameter()] [string[]] - $Names + $Names, + + [Parameter()] + [hashtable[]] + $Features, + + [Parameter()] + [bool]$UseLegacyResource = $false ) Import-DscResource -ModuleName PSDesiredStateConfiguration + Import-DscResource -ModuleName xPSDesiredStateConfiguration + + $resourceName = if ($UseLegacyResource) + { + 'WindowsFeature' + } + else + { + 'xWindowsFeature' + } foreach ($n in $Names) { @@ -31,6 +48,33 @@ configuration WindowsFeatures { IncludeAllSubFeature = $includeAllSubFeature } - (Get-DscSplattedResource -ResourceName WindowsFeature -ExecutionName $params.Name -Properties $params -NoInvoke).Invoke($params) + (Get-DscSplattedResource -ResourceName $resourceName -ExecutionName $params.Name -Properties $params -NoInvoke).Invoke($params) + } + + <# + @{ + Name = [string] + [Credential = [PSCredential]] + [DependsOn = [string[]]] + [Ensure = [string]{ Absent | Present }] + [IncludeAllSubFeature = [bool]] + [LogPath = [string]] + [PsDscRunAsCredential = [PSCredential]] + [Source = [string]] +} + #> + foreach ($feature in $Features) + { + $resourceName = if ($feature.UseLegacyResource) + { + 'WindowsFeature' + } + else + { + 'xWindowsFeature' + } + $feature.remove('UseLegacyResource') + + (Get-DscSplattedResource -ResourceName $resourceName -ExecutionName $feature.Name -Properties $feature -NoInvoke).Invoke($feature) } } diff --git a/source/DSCResources/WindowsOptionalFeatures/WindowsOptionalFeatures.schema.psm1 b/source/DSCResources/WindowsOptionalFeatures/WindowsOptionalFeatures.schema.psm1 index e2f10f2..c04f1c9 100644 --- a/source/DSCResources/WindowsOptionalFeatures/WindowsOptionalFeatures.schema.psm1 +++ b/source/DSCResources/WindowsOptionalFeatures/WindowsOptionalFeatures.schema.psm1 @@ -14,16 +14,17 @@ configuration WindowsOptionalFeatures { ) Import-DscResource -ModuleName PSDesiredStateConfiguration + Import-DscResource -ModuleName xPSDesiredStateConfiguration foreach ($n in $Names) { - $ensure = 'Enable' + $ensure = 'Present' if ($n[0] -in '-', '+') { if ($n[0] -eq '-') { - $ensure = 'Disable' + $ensure = 'Absent' } $n = $n.Substring(1) } @@ -35,6 +36,6 @@ configuration WindowsOptionalFeatures { NoWindowsUpdateCheck = $NoWindowsUpdateCheck } - (Get-DscSplattedResource -ResourceName WindowsOptionalFeature -ExecutionName $params.Name -Properties $params -NoInvoke).Invoke($params) + (Get-DscSplattedResource -ResourceName xWindowsOptionalFeature -ExecutionName $params.Name -Properties $params -NoInvoke).Invoke($params) } } diff --git a/source/DSCResources/WindowsServices/WindowsServices.schema.psm1 b/source/DSCResources/WindowsServices/WindowsServices.schema.psm1 index 3adf217..99a2201 100644 --- a/source/DSCResources/WindowsServices/WindowsServices.schema.psm1 +++ b/source/DSCResources/WindowsServices/WindowsServices.schema.psm1 @@ -13,7 +13,10 @@ configuration WindowsServices { # Remove Case Sensitivity of ordered Dictionary or Hashtables $service = @{} + $service - $service.Ensure = 'Present' + if ([string]::IsNullOrWhiteSpace( $service.Ensure )) + { + $service.Ensure = 'Present' + } [boolean]$delayedStart = $false @@ -24,21 +27,24 @@ configuration WindowsServices { $delayedStart = $true } - # set defaults if no state is specified - if ([string]::IsNullOrWhiteSpace($service.State)) + if ($service.Ensure -eq 'Present') { - # check for running service only if none or a compatible startup type is specified - if ([string]::IsNullOrWhiteSpace($service.StartupType) -or ($service.StartupType -eq 'Automatic')) + # set defaults if no state is specified + if ([string]::IsNullOrWhiteSpace($service.State)) { - $service.State = 'Running' - } - elseif ($service.StartupType -eq 'Disabled') - { - $service.State = 'Stopped' - } - else - { - $service.State = 'Ignore' + # check for running service only if none or a compatible startup type is specified + if ([string]::IsNullOrWhiteSpace($service.StartupType) -or ($service.StartupType -eq 'Automatic')) + { + $service.State = 'Running' + } + elseif ($service.StartupType -eq 'Disabled') + { + $service.State = 'Stopped' + } + else + { + $service.State = 'Ignore' + } } } @@ -47,7 +53,7 @@ configuration WindowsServices { #how splatting of DSC resources works: https://gaelcolas.com/2017/11/05/pseudo-splatting-dsc-resources/ (Get-DscSplattedResource -ResourceName xService -ExecutionName $executionName -Properties $service -NoInvoke).Invoke($service) - if ($delayedStart -eq $true) + if ($service.Ensure -eq 'Present' -and $delayedStart -eq $true) { $serviceName = $Service.Name diff --git a/tests/Unit/DSCResources/Assets/Config/WindowsFeatures.yml b/tests/Unit/DSCResources/Assets/Config/WindowsFeatures.yml index 5bde8ac..015b293 100644 --- a/tests/Unit/DSCResources/Assets/Config/WindowsFeatures.yml +++ b/tests/Unit/DSCResources/Assets/Config/WindowsFeatures.yml @@ -3,3 +3,11 @@ Names: - -Web-Server - Hyper-V - '*RSAT' +Features: + - Name: NET-Framework-Core + Source: \\contoso.com\InstallationSources\WinSxs + UseLegacyResource: true + - Name: NET-Framework-Core + UseLegacyResource: false + - Name: RSAT-AD-Tools + IncludeAllSubFeature: true diff --git a/tests/Unit/DSCResources/Assets/Config/WindowsServices.yml b/tests/Unit/DSCResources/Assets/Config/WindowsServices.yml index 37845cb..b40ead1 100644 --- a/tests/Unit/DSCResources/Assets/Config/WindowsServices.yml +++ b/tests/Unit/DSCResources/Assets/Config/WindowsServices.yml @@ -18,4 +18,7 @@ Services: - Name: Dummy3 StartupType: AutomaticDelayedStart - Name: MSSQL$Instance - StartupType: AutomaticDelayedStart \ No newline at end of file + StartupType: AutomaticDelayedStart + - Name: AbsentSvc + Ensure: Absent + StartupType: AutomaticDelayedStart