Skip to content

Commit

Permalink
Update code per WindowsOptionalFeature PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kwirkykat committed Sep 20, 2016
1 parent 697761b commit 4e51c7a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 41 deletions.
16 changes: 8 additions & 8 deletions DSCResources/CommonResourceHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ function New-InvalidOperationException
Falls back to en-US strings if the machine's culture is not supported.
.PARAMETER ResourceName
The name of the resource as it appears in the file path after 'MSFT_x'.
The name of the resource as it appears before '.strings.psd1' of the localized string file.
For example:
For WindowsOptionalFeature: WindowsOptionalFeature
For Service: ServiceResource
For Registry: RegistryResource
For WindowsOptionalFeature: MSFT_xWindowsOptionalFeature
For Service: MSFT_xServiceResource
For Registry: MSFT_xRegistryResource
#>
function Get-LocalizedData
{
Expand All @@ -108,7 +108,7 @@ function Get-LocalizedData
$ResourceName
)

$resourceDirectory = (Join-Path -Path $PSScriptRoot -ChildPath "MSFT_x$ResourceName")
$resourceDirectory = (Join-Path -Path $PSScriptRoot -ChildPath $ResourceName)
$localizedStringFileLocation = Join-Path -Path $resourceDirectory -ChildPath $PSUICulture

if (-not (Test-Path -Path $localizedStringFileLocation))
Expand All @@ -118,11 +118,11 @@ function Get-LocalizedData
}

Import-LocalizedData `
-BindingVariable 'LocalizedData' `
-FileName "MSFT_x$ResourceName.strings.psd1" `
-BindingVariable 'localizedData' `
-FileName "$ResourceName.strings.psd1" `
-BaseDirectory $localizedStringFileLocation

return $LocalizedData
return $localizedData
}

Export-ModuleMember -Function `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
param ()

Import-Module -Name (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath 'CommonResourceHelper.psm1')
$script:localizedData = Get-LocalizedData -ResourceName 'WindowsOptionalFeature'
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_xWindowsOptionalFeature'

<#
.SYNOPSIS
Expand Down Expand Up @@ -87,7 +87,7 @@ function Get-TargetResource
.PARAMETER LogPath
The path to the log file to log this operation.
There is not default value, but if not set, the log will appear at
There is no default value, but if not set, the log will appear at
%WINDIR%\Logs\Dism\dism.log.
.PARAMETER LogLevel
Expand Down Expand Up @@ -319,7 +319,6 @@ function Convert-FeatureStateToEnsure
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
$State
)
Expand All @@ -334,7 +333,7 @@ function Convert-FeatureStateToEnsure
}
else
{
Write-Warning ($script:localizedData.CouldNotCovertFeatureState -f $State)
Write-Warning ($script:localizedData.CouldNotConvertFeatureState -f $State)
return $State
}
}
Expand All @@ -359,7 +358,7 @@ function Assert-ResourcePrerequisitesValid
# Check that we're running on Server 2012 (or later) or on a client SKU
$operatingSystem = Get-CimInstance -ClassName 'Win32_OperatingSystem'

if (($operatingSystem.ProductType -ne 1) -and ([System.Int32] $operatingSystem.BuildNumber -lt 9600))
if (($operatingSystem.ProductType -eq 2) -and ([System.Int32] $operatingSystem.BuildNumber -lt 9600))
{
New-InvalidOperationException -Message $script:localizedData.NotSupportedSku
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# Localized resources for xWindowsOptionalFeature

ConvertFrom-StringData @'
DismNotAvailable = PowerShell module DISM could not be imported.
NotSupportedSku = This resource is available only on Windows client operating systems and Windows Server 2012 or later.
ElevationRequired = This resource must run as an Administrator.
ValidatingPrerequisites = Validating resource prerequisites...
CouldNotCovertFeatureState = Could not convert feature state '{0}' into Absent or Present.
RestartNeeded = Target machine needs to restart.
GetTargetResourceStartMessage = Started Get-TargetResource on the {0} feature.
GetTargetResourceEndMessage = Finished Get-TargetResource on the {0} feature.
SetTargetResourceStartMessage = Started Set-TargetResource on the {0} feature.
SetTargetResourceEndMessage = Finished Set-TargetResource on the {0} feature.
TestTargetResourceStartMessage = Started Test-TargetResource on the {0} feature.
TestTargetResourceEndMessage = Finished Test-TargetResource on the {0} feature.
FeatureInstalled = Installed feature {0}.
FeatureUninstalled = Uninstalled feature {0}.
ShouldProcessEnableFeature = Enable Windows optional feature
ShouldProcessDisableFeature = Disable Windows optional feature
DismNotAvailable = PowerShell module DISM could not be imported.
NotSupportedSku = This resource is available only on Windows client operating systems and Windows Server 2012 or later.
ElevationRequired = This resource must run as an Administrator.
ValidatingPrerequisites = Validating resource prerequisites...
CouldNotConvertFeatureState = Could not convert feature state '{0}' into Absent or Present.
RestartNeeded = Target machine needs to restart.
GetTargetResourceStartMessage = Started Get-TargetResource on the {0} feature.
GetTargetResourceEndMessage = Finished Get-TargetResource on the {0} feature.
SetTargetResourceStartMessage = Started Set-TargetResource on the {0} feature.
SetTargetResourceEndMessage = Finished Set-TargetResource on the {0} feature.
TestTargetResourceStartMessage = Started Test-TargetResource on the {0} feature.
TestTargetResourceEndMessage = Finished Test-TargetResource on the {0} feature.
FeatureInstalled = Installed feature {0}.
FeatureUninstalled = Uninstalled feature {0}.
ShouldProcessEnableFeature = Enable Windows optional feature
ShouldProcessDisableFeature = Disable Windows optional feature
'@
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,17 @@ These parameters will be the same for each Windows feature in the set. Please re
Provides a mechanism to enable or disable optional features on a target node.

#### Requirements
* Target machine must be running either a Windows client operating system or Windows Server 2012 or later.
* Target machine must have access to the DISM PowerShell module

* Target machine must be running either a Windows client operating system or Windows Server 2012 or later.
* Target machine must have access to the DISM PowerShell module

#### Parameters

* **[String] Name** _(Key)_: The name of the Windows optional feature to enable or disable.
* **[String] Ensure** _(Write)_: Specifies whether the feature should be enabled or disabled. To enable the feature, set this property to Present. To disable the feature, set the property to Absent. The default value is Present. { *Present* | Absent }.
* **[Boolean] RemoveFilesOnDisable** _(Write)_: Specifies that all files associated with the feature should be removed if the feature is being disabled.
* **[Boolean] NoWindowsUpdateCheck** _(Write)_: Specifies whether or not DISM contacts Windows Update (WU) when searching for the source files to enable the feature. If $true, DISM will not contact WU.
* **[String] LogPath** _(Write)_: The path to the log file to log this operation. There is not default value, but if not set, the log will appear at %WINDIR%\Logs\Dism\dism.log.
* **[String] LogPath** _(Write)_: The path to the log file to log this operation. There is no default value, but if not set, the log will appear at %WINDIR%\Logs\Dism\dism.log.
* **[String] LogLevel** _(Write)_: The maximum output level to show in the log. ErrorsOnly will log only errors. ErrorsAndWarning will log only errors and warnings. ErrorsAndWarningAndInformation will log errors, warnings, and debug information). The default value is "ErrorsAndWarningAndInformation". { ErrorsOnly | ErrorsAndWarning | *ErrorsAndWarningAndInformation* }.

#### Read-Only Properties from Get-TargetResource
Expand All @@ -313,7 +314,8 @@ Provides a mechanism to enable or disable optional features on a target node.
* **[String] DisplayName** _(Read)_: The display name retrieved from the Windows optional feature.

#### Examples
* [Enable the specified windows optional feature and output logs to the specified path](https://github.com/PowerShell/xPSDesiredStateConfiguration/blob/dev/Examples/Sample_xWindowsOptionalFeature.ps1)

* [Enable the specified windows optional feature and output logs to the specified path](https://github.com/PowerShell/xPSDesiredStateConfiguration/blob/dev/Examples/Sample_xWindowsOptionalFeature.ps1)

### xWindowsOptionalFeatureSet
Note: xWindowsOptionalFeature is only supported on Windows client or Windows Server 2012 (and later) SKUs.
Expand Down
13 changes: 5 additions & 8 deletions Tests/Unit/MSFT_xWindowsOptionalFeature.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ try
InModuleScope 'MSFT_xWindowsOptionalFeature' {
Describe 'xWindowsOptionalFeature Unit Tests' {
BeforeAll {
Import-Module -Name 'Dism'

$script:testFeatureName = 'TestFeature'

$script:fakeEnabledFeature = [PSCustomObject] @{
Expand Down Expand Up @@ -58,11 +60,6 @@ try
Mock Assert-ResourcePrerequisitesValid -MockWith { }
Mock Dism\Get-WindowsOptionalFeature { $FeatureName -eq $script:testFeatureName } -MockWith { return $script:fakeEnabledFeature }

It 'Should return a Boolean' {
$testTargetResourceResult = Test-TargetResource -Name $testFeatureName
$testTargetResourceResult -is [System.Boolean] | Should Be $true
}

It 'Should return true when Ensure set to Present' {
Test-TargetResource -Name $testFeatureName -Ensure 'Present' | Should Be $true
}
Expand Down Expand Up @@ -242,19 +239,19 @@ try

It 'Should throw when the DISM module is not available' {
Mock Import-Module -ParameterFilter { $Name -eq 'Dism' } -MockWith { Write-Error 'Cannot find module' }
{ Assert-ResourcePrerequisitesValid } | Should Throw
{ Assert-ResourcePrerequisitesValid } | Should Throw $script:localizedData.DismNotAvailable
}

Mock Import-Module -ParameterFilter { $Name -eq 'Dism' } -MockWith { }

It 'Should throw when operating system is Server 2008 R2' {
Mock Get-CimInstance -ParameterFilter { $ClassName -eq 'Win32_OperatingSystem' } -MockWith { return $fakeWin32OSObjects['Server2008R2'] }
{ Assert-ResourcePrerequisitesValid } | Should Throw
{ Assert-ResourcePrerequisitesValid } | Should Throw $script:localizedData.NotSupportedSku
}

It 'Should throw when operating system is Server 2012' {
Mock Get-CimInstance -ParameterFilter { $ClassName -eq 'Win32_OperatingSystem' } -MockWith { return $fakeWin32OSObjects['Server2012'] }
{ Assert-ResourcePrerequisitesValid } | Should Throw
{ Assert-ResourcePrerequisitesValid } | Should Throw $script:localizedData.NotSupportedSku
}

It 'Should not throw when operating system is Windows 7' {
Expand Down

0 comments on commit 4e51c7a

Please sign in to comment.