Enable the specified Windows optional feature and output logs to the specified path
Description
This example shows how you can use the WindowsOptionalFeature
resource with user-provided values
to ensure a Windows optional feature is enabled.
You must specify the name of the Windows optional feature to enable with the FeatureName parameter, which sets the Name property of the resource.
You must specify the path to a log file with the LogPath parameter, which sets the LogPath property of the resource.
With Ensure set to Present
and the Name property set to the user-provided value from the
FeatureName parameter, the resource enables the specified Windows optional feature if it's
disabled.
With LogPath set to the user-provided value from the LogPath parameter, the resource writes
the logs for enabling the feature to that file instead of %WINDIR%\Logs\Dism\dism.log
.
With Invoke-DscResource
This script shows how you can use the WindowsOptionalFeature
resource with the
Invoke-DscResource
cmdlet to ensure a user-specified feature is enabled.
[CmdletBinding()]
param(
[Parameter (Mandatory = $true)]
[String]
$FeatureName,
[Parameter(Mandatory = $true)]
[String]
$LogPath
)
begin {
$SharedParameters = @{
Name = 'WindowsOptionalFeature'
ModuleName = 'PSDscResource'
Properties = @{
Name = $FeatureName
Ensure = 'Present'
LogPath = $LogPath
}
}
$NonGetProperties = @(
'Ensure'
'LogPath'
)
}
process {
$TestResult = Invoke-DscResource -Method Test @SharedParameters
if ($TestResult.InDesiredState) {
$QueryParameters = $SharedParameters.Clone()
foreach ($Property in $NonGetProperties) {
$QueryParameters.Properties.Remove($Property)
}
Invoke-DscResource -Method Get @QueryParameters
} else {
Invoke-DscResource -Method Set @SharedParameters
}
}
With a Configuration
This snippet shows how you can define a Configuration
with a WindowsOptionalFeature
resource
block to ensure a user-specified feature is enabled.
Important
There's a limitation in machine configuration that prevents a DSC Resource from using any PowerShell cmdlets not included in PowerShell itself or in a module on the PowerShell Gallery. This example is provided for demonstrative purposes, but because the DSC Resource uses cmdlets from the DISM module, which ships as one of the Windows modules, it won't work in machine configuration.
Configuration Enable {
param(
[Parameter (Mandatory = $true)]
[String]
$FeatureName,
[Parameter(Mandatory = $true)]
[String]
$LogPath
)
Import-DscResource -ModuleName 'PSDscResources'
Node Localhost {
WindowsOptionalFeature TelnetClient {
Name = $FeatureName
Ensure = 'Present'
LogPath = $LogPath
}
}
}
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for