Skip to content

Commit

Permalink
Add Get-PowerPlan (replaces Get-PowerPlans) and Set-PowerPlan
Browse files Browse the repository at this point in the history
  • Loading branch information
J0F3 committed Jan 22, 2019
1 parent e062889 commit 533e7b3
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 180 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
- PowerPlan:
- Added support to specify the desired power plan either as name or guid.
Fixes [Issue #59](https://github.com/PowerShell/ComputerManagementDsc/issues/59)
- Changed the resource so it uses powercfg.exe instead of WMI/CIM.
(Workaround fo rServer 2012R2 Core, Nano Server, Server 2019 and Windows 10)
Fixes [Issue #155](https://github.com/PowerShell/ComputerManagementDsc/issues/155)
and [Issue #65](https://github.com/PowerShell/ComputerManagementDsc/issues/65)
- Changed the resource so it uses powercfg.exe instead of WMI/CIM
(Workaround fo rServer 2012R2 Core, Nano Server, Server 2019 and Windows 10).
Fixes [Issue #155](https://github.com/PowerShell/ComputerManagementDsc/issues/155)
and [Issue #65](https://github.com/PowerShell/ComputerManagementDsc/issues/65)

## 6.1.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function Get-TargetResource
$Name
)

$plan = Get-PowerPlans | Where-Object{($_.Name -eq $Name) -or ($_.Guid -eq $Name)}
$plan = Get-PowerPlan -Name $Name

if ($plan)
{
Expand Down Expand Up @@ -104,28 +104,16 @@ function Set-TargetResource

Write-Verbose -Message ($script:localizedData.PowerPlanIsBeingActivated -f $Name)

$plan = Get-PowerPlans | Where-Object{($_.Name -eq $Name) -or ($_.Guid -eq $Name)}
$plan = Get-PowerPlan -Name $Name

if($plan)
{
try
{
Invoke-Expression -Command "powercfg.exe /S $($plan.Guid)" -ErrorVariable powerCfgError
if ($powerCfgError)
{
Throw $powerCfgError
}
}
catch
{
New-InvalidOperationException `
-Message ($script:localizedData.PowerPlanWasUnableToBeSet -f $Name, $_.Exception.Message)
}
Set-PowerPlan -Guid $plan.Guid
}
else
{
New-InvalidOperationException `
-Message ($script:localizedData.PowerPlanNotFound -f $Name)
-Message ($script:localizedData.PowerPlanNotFound -f $Name)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ ConvertFrom-StringData @'
PowerPlanNotFound = Unable to find the power plan '{0}'.
PowerPlanIsBeingActivated = Activating power plan '{0}'.
PowerPlanIsBeingValidated = Validating power plan '{0}'.
PowerPlanWasUnableToBeSet = Unable to set the power plan '{0}' to the active plan. Error message: {1}
'@
Original file line number Diff line number Diff line change
Expand Up @@ -493,24 +493,48 @@ function Set-TimeZoneUsingDotNet

<#
.SYNOPSIS
This function gets all power plans/schemes available on the machine using the powercfg.exe utility and returns a custom object for each plan
It exists because the WMI classes are not available on all platforms (e.g on Server 2012 R2 core or nano server)
This function gets a power plans/schemes specified by its friendly name or GUID.
It returns a custom object with the properties of the power plan or
nothing if the powerplan does not exist on the computer.
.PARAMETER Name
Friendly name or GUID of a power plan to get.
.NOTES
This function is used by the PowerPlan resource
The powercfg.exe utility is used here because the Win32_PowerPLan class has
issues on some platforms (e.g Server 2012 R2 core or Nano Server).
This function is used by the PowerPlan resource.
#>
function Get-PowerPlans {
function Get-PowerPlan {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Name
)

$powercfgOutPut = Invoke-Expression -Command 'powercfg.exe /l'
# Set to stop so that the errors from powercfg.exe are terminating
$ErrorActionPreference = 'Stop'

try {
$powercfgOutPut = & powercfg.exe /L
}
catch {
New-InvalidOperationException -ErrorRecord $_
}

if(($null -eq $powercfgOutPut) -or ('' -eq $powercfgOutPut))
{
New-InvalidOperationException -Message $script:localizedData.UnableToGetPowerPlans
}

$allPlans = @()

foreach($line in $powercfgOutPut)
{

if($line -match "^.*?:[ ]*(?'guid'.*?)[ ]*\((?'name'.*?)\)")
{
$plan = [PSCustomObject]@{
Expand All @@ -528,9 +552,45 @@ function Get-PowerPlans {
}
}

return $allPlans
$selectedPlan = $allPlans | Where-Object -FilterScript {
($_.Name -eq $Name) -or
($_.Guid -eq $Name)
}

$selectedPlan
}

<#
.SYNOPSIS
This function activates the desired power plan (specified by its GUID).
.PARAMETER Guid
GUID of a power plan to activate.
.NOTES
The powercfg.exe utility is used here because the Win32_PowerPLan class has
issues on some platforms (e.g Server 2012 R2 core or Nano Server).
This function is used by the PowerPlan resource.
#>
function Set-PowerPlan {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.Guid]
$Guid
)

# Set to stop so that the errors from powercfg.exe are terminating
$ErrorActionPreference = 'Stop'

try {
& powercfg.exe /S $Guid
}
catch {
New-InvalidOperationException -ErrorRecord $_
}
}

Export-ModuleMember -Function `
Test-DscParameterState, `
Expand All @@ -540,4 +600,5 @@ Export-ModuleMember -Function `
Test-TimeZoneId, `
Set-TimeZoneId, `
Set-TimeZoneUsingDotNet, `
Get-PowerPlans
Get-PowerPlan, `
Set-PowerPlan
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ ConvertFrom-StringData @'
SettingTimeZoneMessage = Setting time zone to '{0}' using {1}.
TimeZoneUpdatedMessage = Time zone has been updated to '{0}'.
AddingSetTimeZoneDotNetTypeMessage = Adding .NET Set time zone Type.
UnableToGetPowerPlans = Unable to get available power plans with powercfg.exe /l. Unexpected empty output from powercfg.exe.
'@
Loading

0 comments on commit 533e7b3

Please sign in to comment.