Skip to content

Commit

Permalink
Changes to xComputerManagement and xPowerPlan
Browse files Browse the repository at this point in the history
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
  • Loading branch information
johlju committed Nov 19, 2016
1 parent eb02481 commit 4a60e5a
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 48 deletions.
46 changes: 46 additions & 0 deletions DSCResources/CommonResourceHelper.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<#
.SYNOPSIS
Retrieves the localized string data based on the machine's culture.
Falls back to en-US strings if the machine's culture is not supported.
.PARAMETER ResourceName
The name of the resource as it appears before '.strings.psd1' of the localized string file.
For example:
WindowsOptionalFeature: MSFT_WindowsOptionalFeature
Service: MSFT_ServiceResource
Registry: MSFT_RegistryResource
#>
function Get-LocalizedData
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]
$ResourceName
)

$resourceDirectory = Join-Path -Path $PSScriptRoot -ChildPath $ResourceName
$localizedStringFileLocation = Join-Path -Path $resourceDirectory -ChildPath $PSUICulture

if (-not (Test-Path -Path $localizedStringFileLocation))
{
# Fallback to en-US
$localizedStringFileLocation = Join-Path -Path $resourceDirectory -ChildPath 'en-US'
}

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

return $localizedData
}

Export-ModuleMember -Function @(
'Test-IsNanoServer',
'New-InvalidArgumentException',
'New-InvalidOperationException',
'Get-LocalizedData'
)
83 changes: 49 additions & 34 deletions DSCResources/MSFT_xPowerPlan/MSFT_xPowerPlan.psm1
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Import-Module -Name (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) `
-ChildPath 'CommonResourceHelper.psm1')
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_xPowerPlan'

<#
.SYNOPSIS
Returns the current state of the power plan.
Expand All @@ -23,42 +27,43 @@ function Get-TargetResource
[System.String]
$IsSingleInstance,

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

$arguments = @{
Name = 'root\cimv2\power'
Class = 'Win32_PowerPlan'
Filter = "ElementName = '$Name'"
}

try
{
$arguments = @{
Name = 'root\cimv2\power'
Class = 'Win32_PowerPlan'
Filter = "ElementName = '$Name'"
}

$plan = Get-CimInstance @arguments
if ($plan)
}
catch
{
throw ($script:localizedData.PowerPlanCIMError -f $($arguments.Class) )
}

if ($plan)
{
if( $plan.IsActive )
{
if( $plan.IsActive )
{
Write-Verbose "The power plan '$Name' is the active plan"
$activePlanName = $Name
}
else
{
Write-Verbose "The power plan '$Name' is not the active plan"
$activePlanName = $null
}
Write-Verbose -Message ($script:localizedData.PowerPlanIsActive -f $Name)
$activePlanName = $Name
}
else
{
throw "Unable to find the power plan $Name."
Write-Verbose -Message ($script:localizedData.PowerPlanIsNotActive -f $Name)
$activePlanName = $null
}
}
catch
else
{
throw $_
throw ($script:localizedData.PowerPlanNotFound -f $Name)
}

return @{
Expand Down Expand Up @@ -91,28 +96,36 @@ function Set-TargetResource
[System.String]
$IsSingleInstance,

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

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

$arguments = @{
Name = 'root\cimv2\power'
Class = 'Win32_PowerPlan'
Filter = "ElementName = '$Name'"
}

try
{
Write-Verbose -Message "Activating power plan $Name"

$arguments = @{
Name = 'root\cimv2\power'
Class = 'Win32_PowerPlan'
Filter = "ElementName = '$Name'"
}
$plan = Get-CimInstance @arguments
}
catch
{
throw ($script:localizedData.PowerPlanCIMError -f $($arguments.Class) )
}

$plan = Get-CimInstance @arguments
try
{
$plan | Invoke-CimMethod -MethodName Activate
}
catch
{
Throw "Unable to set the power plan $Name to the active plan. Error message: $($_.Exception.Message)"
throw ($script:localizedData.PowerPlanWasUnableToBeSet -f $Name, $($_.Exception.Message))
}
}

Expand Down Expand Up @@ -141,16 +154,18 @@ function Test-TargetResource
[System.String]
$IsSingleInstance,

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

$returnValue = $false

$result = Get-TargetResource -IsSingleInstance $IsSingleInstance -Name $Name
if ($result.Name -eq $Name)
Write-Verbose -Message ($script:localizedData.PowerPlanIsBeingValidate -f $Name)

$getTargetResourceResult = Get-TargetResource -IsSingleInstance $IsSingleInstance -Name $Name
if ($getTargetResourceResult.Name -eq $Name)
{
$returnValue = $true
}
Expand Down
6 changes: 6 additions & 0 deletions DSCResources/MSFT_xPowerPlan/en-US/MSFT_xPowerPlan.schema.mfl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[Description("This resource is used to activate a power plan.") : Amended,AMENDMENT, LOCALE("MS_409")]
class MSFT_xPowerPlan : OMI_BaseResource
{
[Key, Description("Specifies the resource is a single instance, the value must be 'Yes'") : Amended] String IsSingleInstance;
[Required, Description("The name of the power plan to activate.") : Amended] String Name;
};
11 changes: 11 additions & 0 deletions DSCResources/MSFT_xPowerPlan/en-US/MSFT_xPowerPlan.strings.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Localized resources for WindowsOptionalFeature

ConvertFrom-StringData @'
PowerPlanIsActive = The power plan '{0}' is the active plan.
PowerPlanIsNotActive = The power plan '{0}' is not the active plan.
PowerPlanNotFound = Unable to find the power plan '{0}'.
PowerPlanIsBeingActivated = Activating power plan '{0}'
PowerPlanIsBeingValidate = Validating power plan '{0}'
PowerPlanWasUnableToBeSet = Unable to set the power plan '{0}' to the active plan. Error message: {1}
PowerPlanCIMError = Could not get the Common Information Model (CIM) instances of class {0}
'@
13 changes: 11 additions & 2 deletions Examples/Sample_xPowerPlan.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
configuration Sample_xPowerPlan
<#
.SYNOPSIS
Example to set a power plan.
.DESCRIPTION
This examples sets the active power plan to the 'High performance' plan.
#>
Configuration Sample_xPowerPlan
{
param
(
[string[]] $NodeName = 'localhost'
[Parameter()]
[String[]]
$NodeName = 'localhost'
)

Import-DSCResource -ModuleName xComputerManagement
Expand Down
30 changes: 18 additions & 12 deletions Tests/Unit/MSFT_xPowerPlan.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@ $TestEnvironment = Initialize-TestEnvironment `

#endregion HEADER

function Invoke-TestSetup {
}

function Invoke-TestCleanup {
Restore-TestEnvironment -TestEnvironment $TestEnvironment
}

# Begin Testing
try
{

Invoke-TestSetup

Describe "$($script:DSCResourceName)\Get-TargetResource" {
BeforeEach {
$testParameters = @{
Expand Down Expand Up @@ -78,8 +72,8 @@ try
} -ModuleName $script:DSCResourceName -Verifiable
}

It 'Should throw an error' {
{ Get-TargetResource @testParameters } | Should Throw
It 'Should throw the correct error' {
{ Get-TargetResource @testParameters } | Should Throw 'Could not get the Common Information Model (CIM) instances of class Win32_PowerPlan'
}
}

Expand All @@ -91,7 +85,7 @@ try
}

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

Expand Down Expand Up @@ -120,15 +114,27 @@ try
}
}

Context 'When the Get-CimInstance cannot retrive information about power plans' {
BeforeEach {
Mock -CommandName Get-CimInstance -MockWith {
throw
} -ModuleName $script:DSCResourceName -Verifiable
}

It 'Should throw the correct error' {
{ Set-TargetResource @testParameters } | Should Throw 'Could not get the Common Information Model (CIM) instances of class Win32_PowerPlan'
}
}

Context 'When the Invoke-CimMethod throws an error' {
BeforeEach {
Mock -CommandName Invoke-CimMethod -MockWith {
throw
throw 'Failed to set value'
} -ModuleName $script:DSCResourceName -Verifiable
}

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

Expand Down

0 comments on commit 4a60e5a

Please sign in to comment.