Skip to content

Commit 74aebb6

Browse files
committed
Changes to xPowerPlan
Made the resource a single-instance resource
1 parent eb79a4b commit 74aebb6

5 files changed

Lines changed: 47 additions & 198 deletions

File tree

DSCResources/MSFT_xPowerPlan/MSFT_xPowerPlan.psm1

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
1-
$script:presentStateDefaultPlanName = 'High performance'
2-
# Absent state is hard-coded to plan name 'Balanced' because that is the default plan after OS installation
3-
$script:absentStateDefaultPlanName = 'Balanced'
4-
51
function Get-TargetResource
62
{
73
[CmdletBinding()]
84
[OutputType([System.Collections.Hashtable])]
95
param
106
(
117
[Parameter(Mandatory = $true)]
12-
[ValidateSet("Present","Absent")]
8+
[ValidateSet('Yes')]
139
[System.String]
14-
$Ensure,
10+
$IsSingleInstance,
1511

12+
[parameter(Mandatory = $true)]
13+
[ValidateNotNullOrEmpty()]
1614
[System.String]
17-
$Name = $script:presentStateDefaultPlanName
15+
$Name
1816
)
1917

2018
try
2119
{
22-
if ($Ensure -eq 'Absent' )
23-
{
24-
$Name = $script:absentStateDefaultPlanName
25-
}
26-
2720
$plan = Get-CimInstance -Name root\cimv2\power -Class Win32_PowerPlan -Filter "ElementName = '$Name'"
2821
if ($plan)
2922
{
@@ -49,7 +42,7 @@ function Get-TargetResource
4942
}
5043

5144
return @{
52-
Ensure = $Ensure
45+
IsSingleInstance = $IsSingleInstance
5346
Name = $activePlanName
5447
}
5548
}
@@ -60,19 +53,16 @@ function Set-TargetResource
6053
param
6154
(
6255
[Parameter(Mandatory = $true)]
63-
[ValidateSet("Present","Absent")]
56+
[ValidateSet('Yes')]
6457
[System.String]
65-
$Ensure,
58+
$IsSingleInstance,
6659

60+
[parameter(Mandatory = $true)]
61+
[ValidateNotNullOrEmpty()]
6762
[System.String]
68-
$Name = $script:presentStateDefaultPlanName
63+
$Name
6964
)
7065

71-
if ($Ensure -eq 'Absent' )
72-
{
73-
$Name = $script:absentStateDefaultPlanName
74-
}
75-
7666
if ($PSCmdlet.ShouldProcess($Name, 'Activating power plan'))
7767
{
7868
try
@@ -94,22 +84,20 @@ function Test-TargetResource
9484
param
9585
(
9686
[Parameter(Mandatory = $true)]
97-
[ValidateSet("Present","Absent")]
87+
[ValidateSet('Yes')]
9888
[System.String]
99-
$Ensure,
89+
$IsSingleInstance,
10090

91+
[parameter(Mandatory = $true)]
92+
[ValidateNotNullOrEmpty()]
10193
[System.String]
102-
$Name = $script:presentStateDefaultPlanName
94+
$Name
10395
)
10496

10597
$returnValue = $false
10698

107-
$result = Get-TargetResource -Ensure $Ensure -Name $Name
108-
if ($result.Ensure -eq 'Present' -and $result.Name -eq $Name )
109-
{
110-
$returnValue = $true
111-
}
112-
elseif ($result.Ensure -eq 'Absent' -and $result.Name -eq $script:absentStateDefaultPlanName )
99+
$result = Get-TargetResource -IsSingleInstance $IsSingleInstance -Name $Name
100+
if ($result.Name -eq $Name)
113101
{
114102
$returnValue = $true
115103
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[ClassVersion("1.0.0.0"), FriendlyName("xPowerPlan")]
22
class MSFT_xPowerPlan : OMI_BaseResource
33
{
4-
[Key, Description("Use value 'Present' if the plan name should be activated. Use value 'Absent' to active the default plan for the operating system (hard-coded to plan 'Balanced')."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
5-
[Write, Description("The name och the plan to activate. Default value is 'High performance'.")] String Name;
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 plan to activate.")] String Name;
66
};

Examples/Sample_xPowerPlan.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ configuration Sample_xPowerPlan
1111
{
1212
xPowerPlan SetPlanHighPerformance
1313
{
14-
Ensure = 'Present'
14+
IsSingleInstance = 'Yes'
1515
Name = 'High performance'
1616
}
1717
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ xScheduledTask has the following properties:
7474
## xPowerPlan
7575
xPowerPlan resource has following properties:
7676

77-
* Ensure: Use value 'Present' if the plan name should be activated. Use value 'Absent' to active the default plan for the operating system (hard-coded to plan 'Balanced').
78-
* Name: The name och the plan to activate. Default value is 'High performance'.
77+
* IsSingleInstance: Specifies the resource is a single instance, the value must be 'Yes'.
78+
* Name: The name of the plan to activate.
7979

8080
## Versions
8181

0 commit comments

Comments
 (0)