Skip to content

Commit f80a636

Browse files
authored
Merge pull request #57 from PowerShell/dev
Merging release pull request
2 parents 88ceaf4 + 7adf6e2 commit f80a636

13 files changed

Lines changed: 483 additions & 17 deletions

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DSCResource.Tests
1+
DSCResource.Tests
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
xSQLServerEndpoint: MSFT_xSQLServerEndpoint
10+
xSQLServerConfiguration: MSFT_xSQLServerConfiguration
11+
xSQLServerRole: MSFT_xSQLServerRole
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+
'Get-LocalizedData'
43+
)
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
5+
<#
6+
.SYNOPSIS
7+
Returns the current state of the power plan.
8+
9+
.PARAMETER IsSingleInstance
10+
Specifies the resource is a single instance, the value must be 'Yes'.
11+
12+
.PARAMETER Name
13+
Specifies the name of the power plan to assign to the node.
14+
15+
.EXAMPLE
16+
Get-TargetResource -IsSingleInstance 'Yes' -Name 'High performance'
17+
#>
18+
function Get-TargetResource
19+
{
20+
[CmdletBinding()]
21+
[OutputType([System.Collections.Hashtable])]
22+
param
23+
(
24+
# This is best practice when writing a single-instance DSC resource.
25+
[Parameter(Mandatory = $true)]
26+
[ValidateSet('Yes')]
27+
[System.String]
28+
$IsSingleInstance,
29+
30+
[Parameter(Mandatory = $true)]
31+
[ValidateNotNullOrEmpty()]
32+
[System.String]
33+
$Name
34+
)
35+
36+
$arguments = @{
37+
Name = 'root\cimv2\power'
38+
Class = 'Win32_PowerPlan'
39+
Filter = "ElementName = '$Name'"
40+
}
41+
42+
try
43+
{
44+
$plan = Get-CimInstance @arguments
45+
}
46+
catch
47+
{
48+
throw ($script:localizedData.PowerPlanCIMError -f $($arguments.Class) )
49+
}
50+
51+
if ($plan)
52+
{
53+
if ($plan.IsActive)
54+
{
55+
Write-Verbose -Message ($script:localizedData.PowerPlanIsActive -f $Name)
56+
$activePlanName = $Name
57+
}
58+
else
59+
{
60+
Write-Verbose -Message ($script:localizedData.PowerPlanIsNotActive -f $Name)
61+
$activePlanName = $null
62+
}
63+
}
64+
else
65+
{
66+
throw ($script:localizedData.PowerPlanNotFound -f $Name)
67+
}
68+
69+
return @{
70+
IsSingleInstance = $IsSingleInstance
71+
Name = $activePlanName
72+
}
73+
}
74+
75+
<#
76+
.SYNOPSIS
77+
Assign the power plan to the node.
78+
79+
.PARAMETER IsSingleInstance
80+
Specifies the resource is a single instance, the value must be 'Yes'.
81+
82+
.PARAMETER Name
83+
Specifies the name of the power plan to assign to the node.
84+
85+
.EXAMPLE
86+
Set-TargetResource -IsSingleInstance 'Yes' -Name 'High performance'
87+
#>
88+
function Set-TargetResource
89+
{
90+
[CmdletBinding()]
91+
param
92+
(
93+
# This is best practice when writing a single-instance DSC resource.
94+
[Parameter(Mandatory = $true)]
95+
[ValidateSet('Yes')]
96+
[System.String]
97+
$IsSingleInstance,
98+
99+
[Parameter(Mandatory = $true)]
100+
[ValidateNotNullOrEmpty()]
101+
[System.String]
102+
$Name
103+
)
104+
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+
113+
try
114+
{
115+
$plan = Get-CimInstance @arguments
116+
}
117+
catch
118+
{
119+
throw ($script:localizedData.PowerPlanCIMError -f $($arguments.Class) )
120+
}
121+
122+
try
123+
{
124+
$plan | Invoke-CimMethod -MethodName Activate
125+
}
126+
catch
127+
{
128+
throw ($script:localizedData.PowerPlanWasUnableToBeSet -f $Name, $($_.Exception.Message))
129+
}
130+
}
131+
132+
<#
133+
.SYNOPSIS
134+
Tests if the power plan is assigned to the node.
135+
136+
.PARAMETER IsSingleInstance
137+
Specifies the resource is a single instance, the value must be 'Yes'.
138+
139+
.PARAMETER Name
140+
Specifies the name of the power plan to assign to the node.
141+
142+
.EXAMPLE
143+
Test-TargetResource -IsSingleInstance 'Yes' -Name 'High performance'
144+
#>
145+
function Test-TargetResource
146+
{
147+
[CmdletBinding()]
148+
[OutputType([System.Boolean])]
149+
param
150+
(
151+
# This is best practice when writing a single-instance DSC resource.
152+
[Parameter(Mandatory = $true)]
153+
[ValidateSet('Yes')]
154+
[System.String]
155+
$IsSingleInstance,
156+
157+
[Parameter(Mandatory = $true)]
158+
[ValidateNotNullOrEmpty()]
159+
[System.String]
160+
$Name
161+
)
162+
163+
$returnValue = $false
164+
165+
Write-Verbose -Message ($script:localizedData.PowerPlanIsBeingValidated -f $Name)
166+
167+
$getTargetResourceResult = Get-TargetResource -IsSingleInstance $IsSingleInstance -Name $Name
168+
if ($getTargetResourceResult.Name -eq $Name)
169+
{
170+
$returnValue = $true
171+
}
172+
173+
return $returnValue
174+
}
175+
176+
Export-ModuleMember -Function *-TargetResource
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[ClassVersion("1.0.0.0"), FriendlyName("xPowerPlan")]
2+
class MSFT_xPowerPlan : OMI_BaseResource
3+
{
4+
[Key, Description("Specifies the resource is a single instance, the value must be 'Yes'"), ValueMap{"Yes"}, Values{"Yes"}] String IsSingleInstance;
5+
[Required, Description("The name of the power plan to activate.")] String Name;
6+
};
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+
[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+
PowerPlanIsBeingValidated = 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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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
9+
{
10+
param
11+
(
12+
[Parameter()]
13+
[String[]]
14+
$NodeName = 'localhost'
15+
)
16+
17+
Import-DSCResource -ModuleName xComputerManagement
18+
19+
Node $NodeName
20+
{
21+
xPowerPlan SetPlanHighPerformance
22+
{
23+
IsSingleInstance = 'Yes'
24+
Name = 'High performance'
25+
}
26+
}
27+
}
28+
29+
Sample_xPowerPlan
30+
Start-DscConfiguration -Path Sample_xPowerPlan -Wait -Verbose -Force

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,20 @@ xScheduledTask has the following properties:
7171
* Ensure: Present if the task should exist, false if it should be removed - optional, defaults to 'Ensure'
7272
* ExecuteAsCredential: The credential this task should execute as - Optional, defaults to running as 'NT AUTHORITY\SYSTEM'
7373

74+
## xPowerPlan
75+
xPowerPlan resource has following properties:
76+
77+
* IsSingleInstance: Specifies the resource is a single instance, the value must be 'Yes'.
78+
* Name: The name of the power plan to activate.
7479

7580
## Versions
7681

7782
### Unreleased
7883

84+
### 1.9.0.0
85+
* Added resources
86+
- xPowerPlan
87+
7988
### 1.8.0.0
8089
* Converted AppVeyor.yml to pull Pester from PSGallery instead of Chocolatey.
8190
* Changed AppVeyor.yml to use default image

Tests/Integration/MSFT_xScheduledTask.Config.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,4 @@ Configuration xScheduledTask_Disable
130130
Ensure="Present"
131131
}
132132
}
133-
}
133+
}

Tests/Integration/MSFT_xScheduledTask.Integration.Tests.ps1

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ try
2828

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

210210
It "should compile a MOF file without error" {

0 commit comments

Comments
 (0)