Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

* WindowsFeature
* Added Source parameter to support installation of windows feature which
need additional feature files. The specified path must be accessible by
the target computer.

## 2.12.0.0

* Ports style fixes that were recently made in xPSDesiredStateConfiguration
Expand Down
140 changes: 82 additions & 58 deletions DscResources/MSFT_WindowsFeature/MSFT_WindowsFeature.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ $errorActionPreference = 'Stop'
Set-StrictMode -Version 'Latest'

Import-Module -Name (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) `
-ChildPath 'CommonResourceHelper.psm1')
-ChildPath 'CommonResourceHelper.psm1')

# Localized messages for verbose and error messages in this resource
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_WindowsFeature'
Expand Down Expand Up @@ -34,16 +34,16 @@ function Get-TargetResource
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Name,

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Name,

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential
)

Write-Verbose -Message ($script:localizedData.GetTargetResourceStartMessage -f $Name)
Expand All @@ -56,8 +56,8 @@ function Get-TargetResource
if ($isWinServer2008R2SP1 -and $PSBoundParameters.ContainsKey('Credential'))
{
$feature = Invoke-Command -ScriptBlock { Get-WindowsFeature -Name $Name } `
-ComputerName . `
-Credential $Credential `
-ComputerName . `
-Credential $Credential
}
else
{
Expand All @@ -82,7 +82,7 @@ function Get-TargetResource

if ($PSBoundParameters.ContainsKey('Credential'))
{
$getWindowsFeatureParameters['Credential'] = $Credential
$getWindowsFeatureParameters['Credential'] = $Credential
}

if ($isWinServer2008R2SP1 -and $PSBoundParameters.ContainsKey('Credential'))
Expand All @@ -93,8 +93,8 @@ function Get-TargetResource
attribute on this server.
#>
$subFeature = Invoke-Command -ScriptBlock { Get-WindowsFeature -Name $currentSubFeatureName } `
-ComputerName . `
-Credential $Credential `
-ComputerName . `
-Credential $Credential
}
else
{
Expand Down Expand Up @@ -124,9 +124,9 @@ function Get-TargetResource

# Add all feature properties to the hash table
return @{
Name = $Name
DisplayName = $feature.DisplayName
Ensure = $ensureResult
Name = $Name
DisplayName = $feature.DisplayName
Ensure = $ensureResult
IncludeAllSubFeature = $includeAllSubFeature
}
}
Expand All @@ -144,6 +144,10 @@ function Get-TargetResource
or uninstalled ('Absent').
By default this is set to Present.

.PARAMETER Source
Specifies a path to additional feature source files if necessary for the installation
of the stated roles or features.

.PARAMETER IncludeAllSubFeature
Specifies whether or not all subfeatures should be installed or uninstalled with
the specified role or feature. Default is false.
Expand All @@ -164,30 +168,35 @@ function Set-TargetResource
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Name,

[Parameter()]
[ValidateSet('Present', 'Absent')]
[System.String]
$Ensure = 'Present',

[Parameter()]
[System.Boolean]
$IncludeAllSubFeature = $false,

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential,

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$LogPath
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Name,

[Parameter()]
[ValidateSet('Present', 'Absent')]
[System.String]
$Ensure = 'Present',

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$Source,

[Parameter()]
[System.Boolean]
$IncludeAllSubFeature = $false,

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential,

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$LogPath
)

Write-Verbose -Message ($script:localizedData.SetTargetResourceStartMessage -f $Name)
Expand All @@ -199,17 +208,22 @@ function Set-TargetResource
if ($Ensure -eq 'Present')
{
$addWindowsFeatureParameters = @{
Name = $Name
Name = $Name
IncludeAllSubFeature = $IncludeAllSubFeature
}

if ($PSBoundParameters.ContainsKey('LogPath'))
{
$addWindowsFeatureParameters['LogPath'] = $LogPath
$addWindowsFeatureParameters['LogPath'] = $LogPath
}

Write-Verbose -Message ($script:localizedData.InstallFeature -f $Name)

if (($isWinServer2008R2SP1 -eq $false) -and $PSBoundParameters.ContainsKey('Source'))
{
$addWindowsFeatureParameters['Source'] = $Source
}

if ($isWinServer2008R2SP1 -and $PSBoundParameters.ContainsKey('Credential'))
{
<#
Expand All @@ -218,14 +232,14 @@ function Set-TargetResource
attribute on this server.
#>
$feature = Invoke-Command -ScriptBlock { Add-WindowsFeature @addWindowsFeatureParameters } `
-ComputerName . `
-Credential $Credential
-ComputerName . `
-Credential $Credential
}
else
{
if ($PSBoundParameters.ContainsKey('Credential'))
{
$addWindowsFeatureParameters['Credential'] = $Credential
$addWindowsFeatureParameters['Credential'] = $Credential
}

$feature = Add-WindowsFeature @addWindowsFeatureParameters
Expand Down Expand Up @@ -256,7 +270,7 @@ function Set-TargetResource

if ($PSBoundParameters.ContainsKey('LogPath'))
{
$removeWindowsFeatureParameters['LogPath'] = $LogPath
$removeWindowsFeatureParameters['LogPath'] = $LogPath
}

Write-Verbose -Message ($script:localizedData.UninstallFeature -f $Name)
Expand All @@ -269,14 +283,14 @@ function Set-TargetResource
attribute on this server.
#>
$feature = Invoke-Command -ScriptBlock { Remove-WindowsFeature @removeWindowsFeatureParameters } `
-ComputerName . `
-Credential $Credential
-ComputerName . `
-Credential $Credential
}
else
{
if ($PSBoundParameters.ContainsKey('Credential'))
{
$addWindowsFeatureParameters['Credential'] = $Credential
$addWindowsFeatureParameters['Credential'] = $Credential
}

$feature = Remove-WindowsFeature @removeWindowsFeatureParameters
Expand Down Expand Up @@ -314,6 +328,11 @@ function Set-TargetResource
or uninstalled ('Absent').
By default this is set to Present.

.PARAMETER Source
Specifies a path to additional feature source files if necessary for the installation
of the stated roles or features.
Not used in Test-TargetResource.

.PARAMETER IncludeAllSubFeature
Specifies whether or not the installation state of all subfeatures should be tested with
the specified role or feature. Default is false.
Expand Down Expand Up @@ -348,6 +367,11 @@ function Test-TargetResource
[System.String]
$Ensure = 'Present',

[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$Source,

[Parameter()]
[System.Boolean]
$IncludeAllSubFeature = $false,
Expand Down Expand Up @@ -377,7 +401,7 @@ function Test-TargetResource

if ($PSBoundParameters.ContainsKey('Credential'))
{
$getWindowsFeatureParameters['Credential'] = $Credential
$getWindowsFeatureParameters['Credential'] = $Credential
}

Write-Verbose -Message ($script:localizedData.QueryFeature -f $Name)
Expand All @@ -391,8 +415,8 @@ function Test-TargetResource
attribute on this server.
#>
$feature = Invoke-Command -ScriptBlock { Get-WindowsFeature -Name $Name } `
-ComputerName . `
-Credential $Credential
-ComputerName . `
-Credential $Credential
}
else
{
Expand Down Expand Up @@ -422,8 +446,8 @@ function Test-TargetResource
attribute on this server.
#>
$subFeature = Invoke-Command -ScriptBlock { Get-WindowsFeature -Name $currentSubFeatureName } `
-ComputerName . `
-Credential $Credential
-ComputerName . `
-Credential $Credential
}
else
{
Expand Down Expand Up @@ -514,7 +538,7 @@ function Import-ServerManager

# Check if this operating system needs an update to the ServerManager cmdlets
if ($operatingSystem.Version.StartsWith('6.1.') -and `
$serverCoreOSCodes -contains $operatingSystem.OperatingSystemSKU)
$serverCoreOSCodes -contains $operatingSystem.OperatingSystemSKU)
{
Write-Verbose -Message $script:localizedData.EnableServerManagerPSHCmdletsFeature

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

[ClassVersion("1.0.0.0"), FriendlyName("WindowsFeature")]
[ClassVersion("1.0.0.0"), FriendlyName("WindowsFeature")]
class MSFT_WindowsFeature : OMI_BaseResource
{
[Key, Description("The name of the role or feature to install or uninstall.")] String Name;
[Write, Description("Specifies whether the role or feature should be installed or uninstalled. To install the feature, set this property to Present. To uninstall the feature, set the property to Absent."), ValueMap{"Present", "Absent"}, Values{"Present", "Absent"}] String Ensure;
[Write, Description("Specifies whether the subfeatures of the main feature should also be installed.")] Boolean IncludeAllSubFeature;
[Write, Description("Specifies the path to a source-directory to install certain features.")] String Source;
[Write, Description("The path to the log file to log this operation.")] String LogPath;
[Write, Description("A credential, if needed, to install or uninstall the role or feature."), EmbeddedInstance("MSFT_Credential")] String Credential;
[Read, Description("The display name of the retrieved role or feature.")] String DisplayName;
Expand Down
21 changes: 21 additions & 0 deletions Examples/Sample_WindowsFeature_WithSource.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<#
.SYNOPSIS
Installs the .NET 3.5 Framework feature, which needs additional files from the installation medium
which are specified by the Source-Parameter.
#>
Configuration WindowsFeatureExample_Install_WithSource
{
[CmdletBinding()]
param ()

Import-DscResource -ModuleName 'PSDscResources'

WindowsFeatureSet WindowsFeatureSet1
{
Name = 'NET-Framework-Core'
Ensure = 'Present'
IncludeAllSubFeature = $false
Source = 'E:\sources\sxs'
}
}

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ This resource **is not supported** on Nano Server.
* **[String] Ensure** _(Write)_: Specifies whether the feature should be installed (Present) or uninstalled (Absent) { *Present* | Absent }.
* **[Boolean] IncludeAllSubFeature** _(Write)_: Specifies whether or not all subfeatures should be installed with the specified role or feature. The default value is false.
* **[String] LogPath** _(Write)_: Indicates the path to a log file to log the operation.
* **[String] Source** _(Write)_: Indicates the location of the source file to use for installation, if necessary.

#### Read-Only Properties from Get-TargetResource

Expand Down