Skip to content

Commit

Permalink
Merge pull request #57 from PowerShell/dev
Browse files Browse the repository at this point in the history
Merging release pull request
  • Loading branch information
kwirkykat authored Dec 14, 2016
2 parents 88ceaf4 + 7adf6e2 commit f80a636
Show file tree
Hide file tree
Showing 13 changed files with 483 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DSCResource.Tests
DSCResource.Tests
43 changes: 43 additions & 0 deletions DSCResources/CommonResourceHelper.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<#
.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:
xSQLServerEndpoint: MSFT_xSQLServerEndpoint
xSQLServerConfiguration: MSFT_xSQLServerConfiguration
xSQLServerRole: MSFT_xSQLServerRole
#>
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 @(
'Get-LocalizedData'
)
176 changes: 176 additions & 0 deletions DSCResources/MSFT_xPowerPlan/MSFT_xPowerPlan.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
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.
.PARAMETER IsSingleInstance
Specifies the resource is a single instance, the value must be 'Yes'.
.PARAMETER Name
Specifies the name of the power plan to assign to the node.
.EXAMPLE
Get-TargetResource -IsSingleInstance 'Yes' -Name 'High performance'
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
# This is best practice when writing a single-instance DSC resource.
[Parameter(Mandatory = $true)]
[ValidateSet('Yes')]
[System.String]
$IsSingleInstance,

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

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

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

if ($plan)
{
if ($plan.IsActive)
{
Write-Verbose -Message ($script:localizedData.PowerPlanIsActive -f $Name)
$activePlanName = $Name
}
else
{
Write-Verbose -Message ($script:localizedData.PowerPlanIsNotActive -f $Name)
$activePlanName = $null
}
}
else
{
throw ($script:localizedData.PowerPlanNotFound -f $Name)
}

return @{
IsSingleInstance = $IsSingleInstance
Name = $activePlanName
}
}

<#
.SYNOPSIS
Assign the power plan to the node.
.PARAMETER IsSingleInstance
Specifies the resource is a single instance, the value must be 'Yes'.
.PARAMETER Name
Specifies the name of the power plan to assign to the node.
.EXAMPLE
Set-TargetResource -IsSingleInstance 'Yes' -Name 'High performance'
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
# This is best practice when writing a single-instance DSC resource.
[Parameter(Mandatory = $true)]
[ValidateSet('Yes')]
[System.String]
$IsSingleInstance,

[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
{
$plan = Get-CimInstance @arguments
}
catch
{
throw ($script:localizedData.PowerPlanCIMError -f $($arguments.Class) )
}

try
{
$plan | Invoke-CimMethod -MethodName Activate
}
catch
{
throw ($script:localizedData.PowerPlanWasUnableToBeSet -f $Name, $($_.Exception.Message))
}
}

<#
.SYNOPSIS
Tests if the power plan is assigned to the node.
.PARAMETER IsSingleInstance
Specifies the resource is a single instance, the value must be 'Yes'.
.PARAMETER Name
Specifies the name of the power plan to assign to the node.
.EXAMPLE
Test-TargetResource -IsSingleInstance 'Yes' -Name 'High performance'
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
# This is best practice when writing a single-instance DSC resource.
[Parameter(Mandatory = $true)]
[ValidateSet('Yes')]
[System.String]
$IsSingleInstance,

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

$returnValue = $false

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

$getTargetResourceResult = Get-TargetResource -IsSingleInstance $IsSingleInstance -Name $Name
if ($getTargetResourceResult.Name -eq $Name)
{
$returnValue = $true
}

return $returnValue
}

Export-ModuleMember -Function *-TargetResource
6 changes: 6 additions & 0 deletions DSCResources/MSFT_xPowerPlan/MSFT_xPowerPlan.schema.mof
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[ClassVersion("1.0.0.0"), FriendlyName("xPowerPlan")]
class MSFT_xPowerPlan : OMI_BaseResource
{
[Key, Description("Specifies the resource is a single instance, the value must be 'Yes'"), ValueMap{"Yes"}, Values{"Yes"}] String IsSingleInstance;
[Required, Description("The name of the power plan to activate.")] String Name;
};
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;
[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}'
PowerPlanIsBeingValidated = 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}
'@
30 changes: 30 additions & 0 deletions Examples/Sample_xPowerPlan.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<#
.SYNOPSIS
Example to set a power plan.
.DESCRIPTION
This examples sets the active power plan to the 'High performance' plan.
#>
Configuration Sample_xPowerPlan
{
param
(
[Parameter()]
[String[]]
$NodeName = 'localhost'
)

Import-DSCResource -ModuleName xComputerManagement

Node $NodeName
{
xPowerPlan SetPlanHighPerformance
{
IsSingleInstance = 'Yes'
Name = 'High performance'
}
}
}

Sample_xPowerPlan
Start-DscConfiguration -Path Sample_xPowerPlan -Wait -Verbose -Force
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,20 @@ xScheduledTask has the following properties:
* Ensure: Present if the task should exist, false if it should be removed - optional, defaults to 'Ensure'
* ExecuteAsCredential: The credential this task should execute as - Optional, defaults to running as 'NT AUTHORITY\SYSTEM'

## xPowerPlan
xPowerPlan resource has following properties:

* IsSingleInstance: Specifies the resource is a single instance, the value must be 'Yes'.
* Name: The name of the power plan to activate.

## Versions

### Unreleased

### 1.9.0.0
* Added resources
- xPowerPlan

### 1.8.0.0
* Converted AppVeyor.yml to pull Pester from PSGallery instead of Chocolatey.
* Changed AppVeyor.yml to use default image
Expand Down
2 changes: 1 addition & 1 deletion Tests/Integration/MSFT_xScheduledTask.Config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,4 @@ Configuration xScheduledTask_Disable
Ensure="Present"
}
}
}
}
18 changes: 9 additions & 9 deletions Tests/Integration/MSFT_xScheduledTask.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ try

Context "No scheduled task exists, but it should" {
$CurrentConfig = "xScheduledTask_Add"
$ConfigDir = (Join-Path $TestEnvironment.WorkingFolder $CurrentConfig)
$ConfigDir = (Join-Path $TestDrive $CurrentConfig)
$ConfigMof = (Join-Path $ConfigDir "localhost.mof")

It "should compile a MOF file without error" {
Expand All @@ -50,7 +50,7 @@ try

Context "A scheduled task with minutes based repetition exists, but has the wrong settings" {
$CurrentConfig = "xScheduledTask_Edit1"
$ConfigDir = (Join-Path $TestEnvironment.WorkingFolder $CurrentConfig)
$ConfigDir = (Join-Path $TestDrive $CurrentConfig)
$ConfigMof = (Join-Path $ConfigDir "localhost.mof")

It "should compile a MOF file without error" {
Expand All @@ -72,7 +72,7 @@ try

Context "A scheduled task with hourly based repetition exists, but has the wrong settings" {
$CurrentConfig = "xScheduledTask_Edit2"
$ConfigDir = (Join-Path $TestEnvironment.WorkingFolder $CurrentConfig)
$ConfigDir = (Join-Path $TestDrive $CurrentConfig)
$ConfigMof = (Join-Path $ConfigDir "localhost.mof")

It "should compile a MOF file without error" {
Expand All @@ -94,7 +94,7 @@ try

Context "A scheduled task with daily based repetition exists, but has the wrong settings" {
$CurrentConfig = "xScheduledTask_Edit3"
$ConfigDir = (Join-Path $TestEnvironment.WorkingFolder $CurrentConfig)
$ConfigDir = (Join-Path $TestDrive $CurrentConfig)
$ConfigMof = (Join-Path $ConfigDir "localhost.mof")

It "should compile a MOF file without error" {
Expand All @@ -116,7 +116,7 @@ try

Context "A scheduled task exists and is configured with the wrong working directory" {
$CurrentConfig = "xScheduledTask_Edit4"
$ConfigDir = (Join-Path $TestEnvironment.WorkingFolder $CurrentConfig)
$ConfigDir = (Join-Path $TestDrive $CurrentConfig)
$ConfigMof = (Join-Path $ConfigDir "localhost.mof")

It "should compile a MOF file without error" {
Expand All @@ -138,7 +138,7 @@ try

Context "A scheduled task exists and is configured with the wrong executable arguments" {
$CurrentConfig = "xScheduledTask_Edit5"
$ConfigDir = (Join-Path $TestEnvironment.WorkingFolder $CurrentConfig)
$ConfigDir = (Join-Path $TestDrive $CurrentConfig)
$ConfigMof = (Join-Path $ConfigDir "localhost.mof")

It "should compile a MOF file without error" {
Expand All @@ -160,7 +160,7 @@ try

Context "A scheduled task exists, but it shouldn't" {
$CurrentConfig = "xScheduledTask_Remove"
$ConfigDir = (Join-Path $TestEnvironment.WorkingFolder $CurrentConfig)
$ConfigDir = (Join-Path $TestDrive $CurrentConfig)
$ConfigMof = (Join-Path $ConfigDir "localhost.mof")

It "should compile a MOF file without error" {
Expand All @@ -182,7 +182,7 @@ try

Context "A scheduled task exists, and should be enabled" {
$CurrentConfig = "xScheduledTask_Enable"
$ConfigDir = (Join-Path $TestEnvironment.WorkingFolder $CurrentConfig)
$ConfigDir = (Join-Path $TestDrive $CurrentConfig)
$ConfigMof = (Join-Path $ConfigDir "localhost.mof")

It "should compile a MOF file without error" {
Expand All @@ -204,7 +204,7 @@ try

Context "A scheduled task exists, and should be disabled" {
$CurrentConfig = "xScheduledTask_Disable"
$ConfigDir = (Join-Path $TestEnvironment.WorkingFolder $CurrentConfig)
$ConfigDir = (Join-Path $TestDrive $CurrentConfig)
$ConfigMof = (Join-Path $ConfigDir "localhost.mof")

It "should compile a MOF file without error" {
Expand Down
Loading

0 comments on commit f80a636

Please sign in to comment.