1
+ # See https://github.com/torgro/PowerPlan
2
+
3
+ function Get-Powerplan
4
+ {
5
+ <#
6
+ . Synopsis
7
+ Get a Powerplan by name or all of them
8
+ . DESCRIPTION
9
+ This cmdlet queries the CIM class Win32_PowerPlan. See also Set-PowerPlan cmdlet
10
+ . EXAMPLE
11
+ Get-Powerplan
12
+ This command will output all powerplans:
13
+ Caption :
14
+ Description : Automatically balances performance with energy consumption on capable hardware.
15
+ ElementName : Balanced
16
+ InstanceID : Microsoft:PowerPlan\{381b4222-f694-41f0-9685-ff5bb260df2e}
17
+ IsActive : False
18
+ PSComputerName :
19
+ Caption :
20
+ Description : Favors performance, but may use more energy.
21
+ ElementName : High performance
22
+ InstanceID : Microsoft:PowerPlan\{8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c}
23
+ IsActive : True
24
+ PSComputerName :
25
+ Caption :
26
+ Description : Saves energy by reducing your computer’s performance where possible.
27
+ ElementName : Power saver
28
+ InstanceID : Microsoft:PowerPlan\{a1841308-3541-4fab-bc81-f71556f20b4a}
29
+ IsActive : False
30
+ PSComputerName :
31
+ . EXAMPLE
32
+ Get-Powerplan -PlanName high*
33
+ This command will output all plans that begins with high
34
+ Caption :
35
+ Description : Favors performance, but may use more energy.
36
+ ElementName : High performance
37
+ InstanceID : Microsoft:PowerPlan\{8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c}
38
+ IsActive : True
39
+ PSComputerName :
40
+ . EXAMPLE
41
+ Get-PowerPlan -PlanName high* -ComputerName "Server1","Server2"
42
+ Will output the powerplan with name like high for server1 and server2
43
+
44
+ . EXAMPLE
45
+ Get-PowerPlan -Active
46
+ Will output the active powerplan
47
+ . OUTPUTS
48
+ CimInstance
49
+ . NOTES
50
+ Powerplan and performance
51
+ . COMPONENT
52
+ Powerplan
53
+ . ROLE
54
+ Powerplan
55
+ . FUNCTIONALITY
56
+ This cmdlet queries the CIM class Win32_PowerPlan
57
+ #>
58
+ [cmdletbinding ()]
59
+ [OutputType ([CimInstance []])]
60
+ Param (
61
+ [Parameter (
62
+ ValueFromPipeline = $true ,
63
+ ValueFromPipelineByPropertyName = $true ,
64
+ ValueFromRemainingArguments = $false
65
+ )]
66
+ [Alias (" ElementName" )]
67
+ [string ]$PlanName = " *"
68
+ ,
69
+ [Parameter (
70
+ ValueFromPipeline = $true ,
71
+ ValueFromPipelineByPropertyName = $true ,
72
+ ValueFromRemainingArguments = $false
73
+ )]
74
+ [string []]$ComputerName ,
75
+ [switch ]$Active
76
+ )
77
+
78
+ Begin
79
+ {
80
+ $f = $MyInvocation.InvocationName
81
+ Write-Verbose - Message " $f - START"
82
+
83
+ $GetCimInstance = @ {
84
+ Namespace = " root\cimv2\power"
85
+ ClassName = " Win32_PowerPlan"
86
+ }
87
+
88
+ if ($ComputerName )
89
+ {
90
+ $GetCimInstance.Add (" ComputerName" , $ComputerName )
91
+ }
92
+
93
+ if ($Active )
94
+ {
95
+ $GetCimInstance.Add (" Filter" , ' IsActive="True"' )
96
+ }
97
+ }
98
+
99
+ Process
100
+ {
101
+ if ($PlanName )
102
+ {
103
+ Get-CimInstance @GetCimInstance | Where-Object ElementName -Like " $PlanName "
104
+ }
105
+ else
106
+ {
107
+ Get-CimInstance @GetCimInstance
108
+ }
109
+ }
110
+
111
+ End
112
+ {
113
+ Write-Verbose - Message " $f - END"
114
+ }
115
+ }
116
+
117
+ function Set-PowerPlan
118
+ {
119
+ <#
120
+ . Synopsis
121
+ Sets a Powerplan by name or by value provided from the pipeline
122
+ . DESCRIPTION
123
+ This cmdlet invokes the CIM-method Activate in class Win32_PowerPlan. See also Get-PowerPlan cmdlet
124
+ . EXAMPLE
125
+ Set-PowerPlan -PlanName high*
126
+ This will set the current powerplan to High for the current computer
127
+ . EXAMPLE
128
+ Get-Powerplan -PlanName "Power Saver" | Set-PowerPlan
129
+ Will set the powerplan to "Power Saver" for current computer
130
+ . EXAMPLE
131
+ Get-Powerplan -PlanName "Power Saver" -ComputerName "Server1","Server2" | Set-PowerPlan
132
+ This will set the current powerpla to "Power Saver" for the computers Server1 and Server2
133
+ . EXAMPLE
134
+ Set-PowerPlan -PlanName "Power Saver" -ComputerName "Server1","Server2"
135
+ This will set the current powerpla to "Power Saver" for the computers Server1 and Server2
136
+ . NOTES
137
+ Powerplan and performance
138
+ . COMPONENT
139
+ Powerplan
140
+ . ROLE
141
+ Powerplan
142
+ . FUNCTIONALITY
143
+ This cmdlet invokes CIM-methods in the class Win32_PowerPlan
144
+ #>
145
+ [cmdletbinding (
146
+ SupportsShouldProcess = $true ,
147
+ ConfirmImpact = ' Medium'
148
+ )]
149
+ Param (
150
+ [Parameter (
151
+ ValueFromPipeline = $true ,
152
+ ValueFromPipelineByPropertyName = $true ,
153
+ ValueFromRemainingArguments = $false
154
+ )]
155
+ [Alias (" ElementName" )]
156
+ [string ]$PlanName = " *"
157
+ ,
158
+ [Parameter (
159
+ ValueFromPipeline = $true ,
160
+ ValueFromPipelineByPropertyName = $true ,
161
+ ValueFromRemainingArguments = $false
162
+ )]
163
+ [Alias (" PSComputerName" )]
164
+ [string []]$ComputerName
165
+ )
166
+
167
+ Begin
168
+ {
169
+ $f = $MyInvocation.InvocationName
170
+ Write-Verbose - Message " $f - START"
171
+ $GetCimInstance = @ {
172
+ Namespace = " root\cimv2\power"
173
+ ClassName = " Win32_PowerPlan"
174
+ }
175
+
176
+ if ($ComputerName )
177
+ {
178
+ $GetCimInstance.Add (" ComputerName" , $ComputerName )
179
+ }
180
+
181
+ $InvokeCimMethod = @ {
182
+ MethodName = " Activate"
183
+ }
184
+
185
+ if ($WhatIfPreference )
186
+ {
187
+ $InvokeCimMethod.Add (" WhatIf" , $true )
188
+ }
189
+ }
190
+
191
+ Process
192
+ {
193
+ Write-Verbose - Message " $f - ElementName=$PlanName "
194
+ $CimObjectPowerPlan = Get-CimInstance @GetCimInstance | Where-Object ElementName -like " $PlanName "
195
+
196
+ foreach ($Instance in $CimObjectPowerPlan )
197
+ {
198
+ if ($pscmdlet.ShouldProcess ($Instance ))
199
+ {
200
+ $null = Invoke-CimMethod - InputObject $Instance @InvokeCimMethod
201
+ }
202
+ }
203
+ if (-not $CimObjectPowerPlan )
204
+ {
205
+ Write-Warning - Message " Unable to find powerplan $PlanName "
206
+ }
207
+ }
208
+
209
+ End
210
+ {
211
+ Write-Verbose - Message " $f - END"
212
+ }
213
+
214
+ }
215
+
216
+ <#
217
+ DSC Resource
218
+ Manages the power plan selection for a computer.
219
+ #>
220
+ [DscResource ()]
221
+ class PowerPlan
222
+ {
223
+
224
+ <#
225
+ This property is the name of an available power plan.
226
+ #>
227
+ [DscProperty (Key)]
228
+ [string ]$Name
229
+
230
+ <#
231
+ Sets the specified power plan as active.
232
+ #>
233
+ [void ] Set()
234
+ {
235
+ Set-PowerPlan $this.Name
236
+ }
237
+
238
+ <#
239
+ Tests if the machine is using the specified power plan.
240
+ #>
241
+ [bool ] Test()
242
+ {
243
+ if ((Get-PowerPlan - Active).ElementName -eq $this.Name )
244
+ {
245
+ return $true
246
+ }
247
+ else
248
+ {
249
+ return $false
250
+ }
251
+ }
252
+
253
+ <#
254
+ Returns an instance of this class to identify the active plan.
255
+ #>
256
+ [PowerPlan ] Get()
257
+ {
258
+ $this.Name = (Get-PowerPlan - Active).ElementName
259
+ return $this
260
+ }
261
+ }
0 commit comments