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+ #>
114function 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+ #>
5083function 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+ #>
80132function 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 ]
0 commit comments