Skip to content

Commit

Permalink
Sync with CommonTasks (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
raandree authored Nov 29, 2023
1 parent 29dcecb commit 2f61150
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 47 additions & 3 deletions source/DSCResources/WindowsFeatures/WindowsFeatures.schema.psm1
Original file line number Diff line number Diff line change
@@ -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)
{
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
}
36 changes: 21 additions & 15 deletions source/DSCResources/WindowsServices/WindowsServices.schema.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'
}
}
}

Expand All @@ -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

Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/DSCResources/Assets/Config/WindowsFeatures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion tests/Unit/DSCResources/Assets/Config/WindowsServices.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ Services:
- Name: Dummy3
StartupType: AutomaticDelayedStart
- Name: MSSQL$Instance
StartupType: AutomaticDelayedStart
StartupType: AutomaticDelayedStart
- Name: AbsentSvc
Ensure: Absent
StartupType: AutomaticDelayedStart

0 comments on commit 2f61150

Please sign in to comment.