Skip to content

Commit 4a60e5a

Browse files
committed
Changes to xComputerManagement and xPowerPlan
Added helper module to handle localizaton with Get-LocalizedData in xComputerManagement Added localication to xPowerPlan Improved error handling in xPowerPlan Added comment-based help to example
1 parent eb02481 commit 4a60e5a

6 files changed

Lines changed: 141 additions & 48 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<#
2+
.SYNOPSIS
3+
Retrieves the localized string data based on the machine's culture.
4+
Falls back to en-US strings if the machine's culture is not supported.
5+
6+
.PARAMETER ResourceName
7+
The name of the resource as it appears before '.strings.psd1' of the localized string file.
8+
For example:
9+
WindowsOptionalFeature: MSFT_WindowsOptionalFeature
10+
Service: MSFT_ServiceResource
11+
Registry: MSFT_RegistryResource
12+
#>
13+
function Get-LocalizedData
14+
{
15+
[CmdletBinding()]
16+
param
17+
(
18+
[Parameter(Mandatory = $true)]
19+
[ValidateNotNullOrEmpty()]
20+
[String]
21+
$ResourceName
22+
)
23+
24+
$resourceDirectory = Join-Path -Path $PSScriptRoot -ChildPath $ResourceName
25+
$localizedStringFileLocation = Join-Path -Path $resourceDirectory -ChildPath $PSUICulture
26+
27+
if (-not (Test-Path -Path $localizedStringFileLocation))
28+
{
29+
# Fallback to en-US
30+
$localizedStringFileLocation = Join-Path -Path $resourceDirectory -ChildPath 'en-US'
31+
}
32+
33+
Import-LocalizedData `
34+
-BindingVariable 'localizedData' `
35+
-FileName "$ResourceName.strings.psd1" `
36+
-BaseDirectory $localizedStringFileLocation
37+
38+
return $localizedData
39+
}
40+
41+
Export-ModuleMember -Function @(
42+
'Test-IsNanoServer',
43+
'New-InvalidArgumentException',
44+
'New-InvalidOperationException',
45+
'Get-LocalizedData'
46+
)

DSCResources/MSFT_xPowerPlan/MSFT_xPowerPlan.psm1

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Import-Module -Name (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) `
2+
-ChildPath 'CommonResourceHelper.psm1')
3+
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_xPowerPlan'
4+
15
<#
26
.SYNOPSIS
37
Returns the current state of the power plan.
@@ -23,42 +27,43 @@ function Get-TargetResource
2327
[System.String]
2428
$IsSingleInstance,
2529

26-
[parameter(Mandatory = $true)]
30+
[Parameter(Mandatory = $true)]
2731
[ValidateNotNullOrEmpty()]
2832
[System.String]
2933
$Name
3034
)
3135

36+
$arguments = @{
37+
Name = 'root\cimv2\power'
38+
Class = 'Win32_PowerPlan'
39+
Filter = "ElementName = '$Name'"
40+
}
41+
3242
try
3343
{
34-
$arguments = @{
35-
Name = 'root\cimv2\power'
36-
Class = 'Win32_PowerPlan'
37-
Filter = "ElementName = '$Name'"
38-
}
39-
4044
$plan = Get-CimInstance @arguments
41-
if ($plan)
45+
}
46+
catch
47+
{
48+
throw ($script:localizedData.PowerPlanCIMError -f $($arguments.Class) )
49+
}
50+
51+
if ($plan)
52+
{
53+
if( $plan.IsActive )
4254
{
43-
if( $plan.IsActive )
44-
{
45-
Write-Verbose "The power plan '$Name' is the active plan"
46-
$activePlanName = $Name
47-
}
48-
else
49-
{
50-
Write-Verbose "The power plan '$Name' is not the active plan"
51-
$activePlanName = $null
52-
}
55+
Write-Verbose -Message ($script:localizedData.PowerPlanIsActive -f $Name)
56+
$activePlanName = $Name
5357
}
5458
else
5559
{
56-
throw "Unable to find the power plan $Name."
60+
Write-Verbose -Message ($script:localizedData.PowerPlanIsNotActive -f $Name)
61+
$activePlanName = $null
5762
}
5863
}
59-
catch
64+
else
6065
{
61-
throw $_
66+
throw ($script:localizedData.PowerPlanNotFound -f $Name)
6267
}
6368

6469
return @{
@@ -91,28 +96,36 @@ function Set-TargetResource
9196
[System.String]
9297
$IsSingleInstance,
9398

94-
[parameter(Mandatory = $true)]
99+
[Parameter(Mandatory = $true)]
95100
[ValidateNotNullOrEmpty()]
96101
[System.String]
97102
$Name
98103
)
99104

105+
Write-Verbose -Message ($script:localizedData.PowerPlanIsBeingActivated -f $Name)
106+
107+
$arguments = @{
108+
Name = 'root\cimv2\power'
109+
Class = 'Win32_PowerPlan'
110+
Filter = "ElementName = '$Name'"
111+
}
112+
100113
try
101114
{
102-
Write-Verbose -Message "Activating power plan $Name"
103-
104-
$arguments = @{
105-
Name = 'root\cimv2\power'
106-
Class = 'Win32_PowerPlan'
107-
Filter = "ElementName = '$Name'"
108-
}
115+
$plan = Get-CimInstance @arguments
116+
}
117+
catch
118+
{
119+
throw ($script:localizedData.PowerPlanCIMError -f $($arguments.Class) )
120+
}
109121

110-
$plan = Get-CimInstance @arguments
122+
try
123+
{
111124
$plan | Invoke-CimMethod -MethodName Activate
112125
}
113126
catch
114127
{
115-
Throw "Unable to set the power plan $Name to the active plan. Error message: $($_.Exception.Message)"
128+
throw ($script:localizedData.PowerPlanWasUnableToBeSet -f $Name, $($_.Exception.Message))
116129
}
117130
}
118131

@@ -141,16 +154,18 @@ function Test-TargetResource
141154
[System.String]
142155
$IsSingleInstance,
143156

144-
[parameter(Mandatory = $true)]
157+
[Parameter(Mandatory = $true)]
145158
[ValidateNotNullOrEmpty()]
146159
[System.String]
147160
$Name
148161
)
149162

150163
$returnValue = $false
151164

152-
$result = Get-TargetResource -IsSingleInstance $IsSingleInstance -Name $Name
153-
if ($result.Name -eq $Name)
165+
Write-Verbose -Message ($script:localizedData.PowerPlanIsBeingValidate -f $Name)
166+
167+
$getTargetResourceResult = Get-TargetResource -IsSingleInstance $IsSingleInstance -Name $Name
168+
if ($getTargetResourceResult.Name -eq $Name)
154169
{
155170
$returnValue = $true
156171
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[Description("This resource is used to activate a power plan.") : Amended,AMENDMENT, LOCALE("MS_409")]
2+
class MSFT_xPowerPlan : OMI_BaseResource
3+
{
4+
[Key, Description("Specifies the resource is a single instance, the value must be 'Yes'") : Amended] String IsSingleInstance;
5+
[Required, Description("The name of the power plan to activate.") : Amended] String Name;
6+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Localized resources for WindowsOptionalFeature
2+
3+
ConvertFrom-StringData @'
4+
PowerPlanIsActive = The power plan '{0}' is the active plan.
5+
PowerPlanIsNotActive = The power plan '{0}' is not the active plan.
6+
PowerPlanNotFound = Unable to find the power plan '{0}'.
7+
PowerPlanIsBeingActivated = Activating power plan '{0}'
8+
PowerPlanIsBeingValidate = Validating power plan '{0}'
9+
PowerPlanWasUnableToBeSet = Unable to set the power plan '{0}' to the active plan. Error message: {1}
10+
PowerPlanCIMError = Could not get the Common Information Model (CIM) instances of class {0}
11+
'@

Examples/Sample_xPowerPlan.ps1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
configuration Sample_xPowerPlan
1+
<#
2+
.SYNOPSIS
3+
Example to set a power plan.
4+
5+
.DESCRIPTION
6+
This examples sets the active power plan to the 'High performance' plan.
7+
#>
8+
Configuration Sample_xPowerPlan
29
{
310
param
411
(
5-
[string[]] $NodeName = 'localhost'
12+
[Parameter()]
13+
[String[]]
14+
$NodeName = 'localhost'
615
)
716

817
Import-DSCResource -ModuleName xComputerManagement

Tests/Unit/MSFT_xPowerPlan.Tests.ps1

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,13 @@ $TestEnvironment = Initialize-TestEnvironment `
2020

2121
#endregion HEADER
2222

23-
function Invoke-TestSetup {
24-
}
25-
2623
function Invoke-TestCleanup {
2724
Restore-TestEnvironment -TestEnvironment $TestEnvironment
2825
}
2926

3027
# Begin Testing
3128
try
3229
{
33-
34-
Invoke-TestSetup
35-
3630
Describe "$($script:DSCResourceName)\Get-TargetResource" {
3731
BeforeEach {
3832
$testParameters = @{
@@ -78,8 +72,8 @@ try
7872
} -ModuleName $script:DSCResourceName -Verifiable
7973
}
8074

81-
It 'Should throw an error' {
82-
{ Get-TargetResource @testParameters } | Should Throw
75+
It 'Should throw the correct error' {
76+
{ Get-TargetResource @testParameters } | Should Throw 'Could not get the Common Information Model (CIM) instances of class Win32_PowerPlan'
8377
}
8478
}
8579

@@ -91,7 +85,7 @@ try
9185
}
9286

9387
It 'Should throw saying it was not able to find the plan High performance' {
94-
{ Get-TargetResource @testParameters } | Should Throw 'Unable to find the power plan High performance.'
88+
{ Get-TargetResource @testParameters } | Should Throw "Unable to find the power plan 'High performance'."
9589
}
9690
}
9791

@@ -120,15 +114,27 @@ try
120114
}
121115
}
122116

117+
Context 'When the Get-CimInstance cannot retrive information about power plans' {
118+
BeforeEach {
119+
Mock -CommandName Get-CimInstance -MockWith {
120+
throw
121+
} -ModuleName $script:DSCResourceName -Verifiable
122+
}
123+
124+
It 'Should throw the correct error' {
125+
{ Set-TargetResource @testParameters } | Should Throw 'Could not get the Common Information Model (CIM) instances of class Win32_PowerPlan'
126+
}
127+
}
128+
123129
Context 'When the Invoke-CimMethod throws an error' {
124130
BeforeEach {
125131
Mock -CommandName Invoke-CimMethod -MockWith {
126-
throw
132+
throw 'Failed to set value'
127133
} -ModuleName $script:DSCResourceName -Verifiable
128134
}
129135

130-
It 'Should catch the error thrown by Invoke-CimMethod' {
131-
{ Set-TargetResource @testParameters } | Should Throw
136+
It 'Should catch the correct error thrown by Invoke-CimMethod' {
137+
{ Set-TargetResource @testParameters } | Should Throw "Unable to set the power plan 'High performance' to the active plan. Error message: Failed to set value"
132138
}
133139
}
134140

0 commit comments

Comments
 (0)