Skip to content

Commit

Permalink
Merge pull request #203 from J0F3/Windows-API-instead-WMI
Browse files Browse the repository at this point in the history
PowerPlan: Windows APIs instead of WMI (alternative for PR #202)
  • Loading branch information
PlagueHO authored Mar 28, 2019
2 parents cbb7886 + 96360a4 commit 0bfea1c
Show file tree
Hide file tree
Showing 10 changed files with 874 additions and 148 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Unreleased

- 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 Windows APIs instead of WMI/CIM
(Workaround for Server 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.2.0.0

- WindowsEventLog:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ $script:localizedData = Get-LocalizedData `
Specifies the resource is a single instance, the value must be 'Yes'.
.PARAMETER Name
Specifies the name of the power plan to assign to the node.
Specifies the name or GUID of the power plan to assign to the node.
.EXAMPLE
Get-TargetResource -IsSingleInstance 'Yes' -Name 'High performance'
Expand All @@ -46,44 +46,35 @@ function Get-TargetResource
$Name
)

$getCimInstanceArguments = @{
Name = 'root\cimv2\power'
Class = 'Win32_PowerPlan'
Filter = "ElementName = '$Name'"
}
$desiredPowerPlan = Get-PowerPlan -PowerPlan $Name

try
{
$plan = Get-CimInstance @getCimInstanceArguments
}
catch
if ($desiredPowerPlan)
{
New-InvalidOperationException `
-Message ($script:localizedData.PowerPlanCimError -f $getCimInstanceArguments.Class)
}
$activePowerPlan = Get-ActivePowerPlan

if ($plan)
{
if ($plan.IsActive)
if ($activePowerPlan -eq $desiredPowerPlan.Guid)
{
Write-Verbose -Message ($script:localizedData.PowerPlanIsActive -f $Name)
Write-Verbose -Message ($script:localizedData.PowerPlanIsActive -f $desiredPowerPlan.FriendlyName)
$isActive = $true
}
else
{
Write-Verbose -Message ($script:localizedData.PowerPlanIsNotActive -f $Name)
Write-Verbose -Message ($script:localizedData.PowerPlanIsNotActive -f $desiredPowerPlan.FriendlyName)
$isActive = $false
}

return @{
IsSingleInstance = $IsSingleInstance
Name = $Name
IsActive = $isActive
}

}
else
{
New-InvalidOperationException `
-Message ($script:localizedData.PowerPlanNotFound -f $Name)
}

return @{
IsSingleInstance = $IsSingleInstance
Name = $Name
IsActive = $plan.IsActive
}
}

<#
Expand All @@ -94,7 +85,7 @@ function Get-TargetResource
Specifies the resource is a single instance, the value must be 'Yes'.
.PARAMETER Name
Specifies the name of the power plan to assign to the node.
Specifies the name or GUID of the power plan to assign to the node.
.EXAMPLE
Set-TargetResource -IsSingleInstance 'Yes' -Name 'High performance'
Expand All @@ -118,30 +109,16 @@ function Set-TargetResource

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

$getCimInstanceArguments = @{
Name = 'root\cimv2\power'
Class = 'Win32_PowerPlan'
Filter = "ElementName = '$Name'"
}

try
{
$plan = Get-CimInstance @getCimInstanceArguments
}
catch
{
New-InvalidOperationException `
-Message ($script:localizedData.PowerPlanCimError -f $getCimInstanceArguments.Class)
}
$desiredPowerPlan = Get-PowerPlan -PowerPlan $Name

try
if ($desiredPowerPlan)
{
$plan | Invoke-CimMethod -MethodName Activate
Set-ActivePowerPlan -PowerPlanGuid $desiredPowerPlan.Guid
}
catch
else
{
New-InvalidOperationException `
-Message ($script:localizedData.PowerPlanWasUnableToBeSet -f $Name, $_.Exception.Message)
-Message ($script:localizedData.PowerPlanNotFound -f $Name)
}
}

Expand All @@ -153,7 +130,7 @@ function Set-TargetResource
Specifies the resource is a single instance, the value must be 'Yes'.
.PARAMETER Name
Specifies the name of the power plan to assign to the node.
Specifies the name or GUID of the power plan to assign to the node.
.EXAMPLE
Test-TargetResource -IsSingleInstance 'Yes' -Name 'High performance'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
class MSFT_PowerPlan : OMI_BaseResource
{
[Key, Description("Specifies the resource is a single instance, the value must be 'Yes'."), ValueMap{"Yes"}, Values{"Yes"}] String IsSingleInstance;
[Required, Description("The name of the power plan to activate.")] String Name;
[Required, Description("The name or GUID of the power plan to activate.")] String Name;
[Read, Description("Determines if the power plan is active.")] Boolean IsActive;
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
class MSFT_PowerPlan : OMI_BaseResource
{
[Key, Description("Specifies the resource is a single instance, the value must be 'Yes'.") : Amended] String IsSingleInstance;
[Description("The name of the power plan to activate.") : Amended] String Name;
[Description("The name or GUID of the power plan to activate.") : Amended] String Name;
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +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}
PowerPlanCimError = Could not get the Common Information Model (CIM) instances of class '{0}'.
'@
Loading

0 comments on commit 0bfea1c

Please sign in to comment.