Skip to content

Commit eb02481

Browse files
committed
Changes to xPowerPlan
Improved the description for parameter Name Removed ShouldProcess and SupportsShouldProcess Added comment-based help Cleaned up the code somewhat
1 parent 74aebb6 commit eb02481

3 files changed

Lines changed: 66 additions & 13 deletions

File tree

DSCResources/MSFT_xPowerPlan/MSFT_xPowerPlan.psm1

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1+
<#
2+
.SYNOPSIS
3+
Returns the current state of the power plan.
4+
5+
.PARAMETER IsSingleInstance
6+
Specifies the resource is a single instance, the value must be 'Yes'.
7+
8+
.PARAMETER Name
9+
Specifies the name of the power plan to assign to the node.
10+
11+
.EXAMPLE
12+
Get-TargetResource -IsSingleInstance 'Yes' -Name 'High performance'
13+
#>
114
function Get-TargetResource
215
{
316
[CmdletBinding()]
417
[OutputType([System.Collections.Hashtable])]
518
param
619
(
20+
# This is best practice when writing a single-instance DSC resource.
721
[Parameter(Mandatory = $true)]
822
[ValidateSet('Yes')]
923
[System.String]
@@ -17,7 +31,13 @@ function Get-TargetResource
1731

1832
try
1933
{
20-
$plan = Get-CimInstance -Name root\cimv2\power -Class Win32_PowerPlan -Filter "ElementName = '$Name'"
34+
$arguments = @{
35+
Name = 'root\cimv2\power'
36+
Class = 'Win32_PowerPlan'
37+
Filter = "ElementName = '$Name'"
38+
}
39+
40+
$plan = Get-CimInstance @arguments
2141
if ($plan)
2242
{
2343
if( $plan.IsActive )
@@ -47,11 +67,25 @@ function Get-TargetResource
4767
}
4868
}
4969

70+
<#
71+
.SYNOPSIS
72+
Assign the power plan to the node.
73+
74+
.PARAMETER IsSingleInstance
75+
Specifies the resource is a single instance, the value must be 'Yes'.
76+
77+
.PARAMETER Name
78+
Specifies the name of the power plan to assign to the node.
79+
80+
.EXAMPLE
81+
Set-TargetResource -IsSingleInstance 'Yes' -Name 'High performance'
82+
#>
5083
function Set-TargetResource
5184
{
52-
[CmdletBinding(SupportsShouldProcess)]
85+
[CmdletBinding()]
5386
param
5487
(
88+
# This is best practice when writing a single-instance DSC resource.
5589
[Parameter(Mandatory = $true)]
5690
[ValidateSet('Yes')]
5791
[System.String]
@@ -63,26 +97,45 @@ function Set-TargetResource
6397
$Name
6498
)
6599

66-
if ($PSCmdlet.ShouldProcess($Name, 'Activating power plan'))
100+
try
67101
{
68-
try
69-
{
70-
$plan = Get-CimInstance -Name root\cimv2\power -Class Win32_PowerPlan -Filter "ElementName = '$Name'"
71-
Invoke-CimMethod -InputObject $plan -MethodName Activate
72-
}
73-
catch
74-
{
75-
Throw "Unable to set the power plan $Name to the active plan. Error message: $($_.Exception.Message)"
102+
Write-Verbose -Message "Activating power plan $Name"
103+
104+
$arguments = @{
105+
Name = 'root\cimv2\power'
106+
Class = 'Win32_PowerPlan'
107+
Filter = "ElementName = '$Name'"
76108
}
109+
110+
$plan = Get-CimInstance @arguments
111+
$plan | Invoke-CimMethod -MethodName Activate
112+
}
113+
catch
114+
{
115+
Throw "Unable to set the power plan $Name to the active plan. Error message: $($_.Exception.Message)"
77116
}
78117
}
79118

119+
<#
120+
.SYNOPSIS
121+
Tests if the power plan is assigned to the node.
122+
123+
.PARAMETER IsSingleInstance
124+
Specifies the resource is a single instance, the value must be 'Yes'.
125+
126+
.PARAMETER Name
127+
Specifies the name of the power plan to assign to the node.
128+
129+
.EXAMPLE
130+
Test-TargetResource -IsSingleInstance 'Yes' -Name 'High performance'
131+
#>
80132
function Test-TargetResource
81133
{
82134
[CmdletBinding()]
83135
[OutputType([System.Boolean])]
84136
param
85137
(
138+
# This is best practice when writing a single-instance DSC resource.
86139
[Parameter(Mandatory = $true)]
87140
[ValidateSet('Yes')]
88141
[System.String]

DSCResources/MSFT_xPowerPlan/MSFT_xPowerPlan.schema.mof

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
class MSFT_xPowerPlan : OMI_BaseResource
33
{
44
[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;
5+
[Required, Description("The name of the power plan to activate.")] String Name;
66
};

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ xScheduledTask has the following properties:
7575
xPowerPlan resource has following properties:
7676

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

8080
## Versions
8181

0 commit comments

Comments
 (0)