|
|
@ -7,73 +7,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
$params = Parse-Args -arguments $args -supports_check_mode $true
|
|
|
|
$params = Parse-Args -arguments $args -supports_check_mode $true
|
|
|
|
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
|
|
|
|
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
|
|
|
|
|
|
|
|
$_remote_tmp = Get-AnsibleParam $params "_ansible_remote_tmp" -type "path" -default $env:TMP
|
|
|
|
|
|
|
|
|
|
|
|
# these are your module parameters
|
|
|
|
# these are your module parameters
|
|
|
|
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
|
|
|
|
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$result = @{
|
|
|
|
|
|
|
|
changed = $false
|
|
|
|
|
|
|
|
power_plan_name = $name
|
|
|
|
|
|
|
|
power_plan_enabled = $null
|
|
|
|
|
|
|
|
all_available_plans = $null
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$pinvoke_functions = @"
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Ansible.WinPowerPlan
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public enum AccessFlags : uint
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
AccessScheme = 16,
|
|
|
|
|
|
|
|
AccessSubgroup = 17,
|
|
|
|
|
|
|
|
AccessIndividualSetting = 18
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class NativeMethods
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
[DllImport("Kernel32.dll", SetLastError = true)]
|
|
|
|
|
|
|
|
public static extern IntPtr LocalFree(
|
|
|
|
|
|
|
|
IntPtr hMen);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("PowrProf.dll")]
|
|
|
|
|
|
|
|
public static extern UInt32 PowerEnumerate(
|
|
|
|
|
|
|
|
IntPtr RootPowerKey,
|
|
|
|
|
|
|
|
IntPtr SchemeGuid,
|
|
|
|
|
|
|
|
IntPtr SubGroupOfPowerSettingsGuid,
|
|
|
|
|
|
|
|
AccessFlags AccessFlags,
|
|
|
|
|
|
|
|
UInt32 Index,
|
|
|
|
|
|
|
|
IntPtr Buffer,
|
|
|
|
|
|
|
|
ref UInt32 BufferSize);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("PowrProf.dll")]
|
|
|
|
|
|
|
|
public static extern UInt32 PowerGetActiveScheme(
|
|
|
|
|
|
|
|
IntPtr UserRootPowerKey,
|
|
|
|
|
|
|
|
out IntPtr ActivePolicyGuid);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("PowrProf.dll")]
|
|
|
|
|
|
|
|
public static extern UInt32 PowerReadFriendlyName(
|
|
|
|
|
|
|
|
IntPtr RootPowerKey,
|
|
|
|
|
|
|
|
Guid SchemeGuid,
|
|
|
|
|
|
|
|
IntPtr SubGroupOfPowerSettingsGuid,
|
|
|
|
|
|
|
|
IntPtr PowerSettingGuid,
|
|
|
|
|
|
|
|
IntPtr Buffer,
|
|
|
|
|
|
|
|
ref UInt32 BufferSize);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("PowrProf.dll")]
|
|
|
|
|
|
|
|
public static extern UInt32 PowerSetActiveScheme(
|
|
|
|
|
|
|
|
IntPtr UserRootPowerKey,
|
|
|
|
|
|
|
|
Guid SchemeGuid);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
"@
|
|
|
|
|
|
|
|
$original_tmp = $env:TMP
|
|
|
|
|
|
|
|
$env:TMP = $_remote_tmp
|
|
|
|
|
|
|
|
Add-Type -TypeDefinition $pinvoke_functions
|
|
|
|
|
|
|
|
$env:TMP = $original_tmp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Function Get-LastWin32ErrorMessage {
|
|
|
|
|
|
|
|
param([Int]$ErrorCode)
|
|
|
|
|
|
|
|
$exp = New-Object -TypeName System.ComponentModel.Win32Exception -ArgumentList $ErrorCode
|
|
|
|
|
|
|
|
$error_msg = "{0} - (Win32 Error Code {1} - 0x{1:X8})" -f $exp.Message, $ErrorCode
|
|
|
|
|
|
|
|
return $error_msg
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Function Get-PlanName {
|
|
|
|
|
|
|
|
param([Guid]$Plan)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$buffer_size = 0
|
|
|
|
|
|
|
|
$buffer = [IntPtr]::Zero
|
|
|
|
|
|
|
|
[Ansible.WinPowerPlan.NativeMethods]::PowerReadFriendlyName([IntPtr]::Zero, $Plan, [IntPtr]::Zero, [IntPtr]::Zero,
|
|
|
|
|
|
|
|
$buffer, [ref]$buffer_size) > $null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$buffer = [System.Runtime.InteropServices.Marshal]::AllocHGlobal($buffer_size)
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$res = [Ansible.WinPowerPlan.NativeMethods]::PowerReadFriendlyName([IntPtr]::Zero, $Plan, [IntPtr]::Zero,
|
|
|
|
|
|
|
|
[IntPtr]::Zero, $buffer, [ref]$buffer_size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($res -ne 0) {
|
|
|
|
|
|
|
|
$err_msg = Get-LastWin32ErrorMessage -ErrorCode $res
|
|
|
|
|
|
|
|
Fail-Json -obj $result -message "Failed to get name for power scheme $Plan - $err_msg"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return [System.Runtime.InteropServices.Marshal]::PtrToStringUni($buffer)
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
[System.Runtime.InteropServices.Marshal]::FreeHGlobal($buffer)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Function Get-PowerPlans {
|
|
|
|
Function Get-PowerPlans {
|
|
|
|
Param ($PlanName)
|
|
|
|
$plans = @{}
|
|
|
|
If (-not $PlanName) {
|
|
|
|
|
|
|
|
Get-CimInstance -Name root\cimv2\power -Class Win32_PowerPlan |
|
|
|
|
$i = 0
|
|
|
|
Select-Object -Property ElementName, IsActive |
|
|
|
|
while ($true) {
|
|
|
|
ForEach-Object -Begin { $ht = @{} } -Process { $ht."$($_.ElementName)" = $_.IsActive } -End { $ht }
|
|
|
|
$buffer_size = 0
|
|
|
|
|
|
|
|
$buffer = [IntPtr]::Zero
|
|
|
|
|
|
|
|
$res = [Ansible.WinPowerPlan.NativeMethods]::PowerEnumerate([IntPtr]::Zero, [IntPtr]::Zero, [IntPtr]::Zero,
|
|
|
|
|
|
|
|
[Ansible.WinPowerPlan.AccessFlags]::AccessScheme, $i, $buffer, [ref]$buffer_size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($res -eq 259) {
|
|
|
|
|
|
|
|
# 259 == ERROR_NO_MORE_ITEMS, there are no more power plans to enumerate
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
} elseif ($res -notin @(0, 234)) {
|
|
|
|
|
|
|
|
# 0 == ERROR_SUCCESS and 234 == ERROR_MORE_DATA
|
|
|
|
|
|
|
|
$err_msg = Get-LastWin32ErrorMessage -ErrorCode $res
|
|
|
|
|
|
|
|
Fail-Json -obj $result -message "Failed to get buffer size on local power schemes at index $i - $err_msg"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Else {
|
|
|
|
|
|
|
|
Get-CimInstance -Name root\cimv2\power -Class Win32_PowerPlan -Filter "ElementName = '$PlanName'"
|
|
|
|
$buffer = [System.Runtime.InteropServices.Marshal]::AllocHGlobal($buffer_size)
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$res = [Ansible.WinPowerPlan.NativeMethods]::PowerEnumerate([IntPtr]::Zero, [IntPtr]::Zero, [IntPtr]::Zero,
|
|
|
|
|
|
|
|
[Ansible.WinPowerPlan.AccessFlags]::AccessScheme, $i, $buffer, [ref]$buffer_size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($res -eq 259) {
|
|
|
|
|
|
|
|
# Server 2008 does not return 259 in the first call above so we do an additional check here
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
} elseif ($res -notin @(0, 234, 259)) {
|
|
|
|
|
|
|
|
$err_msg = Get-LastWin32ErrorMessage -ErrorCode $res
|
|
|
|
|
|
|
|
Fail-Json -obj $result -message "Failed to enumerate local power schemes at index $i - $err_msg"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scheme_guid = [System.Runtime.InteropServices.Marshal]::PtrToStructure($buffer, [Type][Guid])
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
[System.Runtime.InteropServices.Marshal]::FreeHGlobal($buffer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scheme_name = Get-PlanName -Plan $scheme_guid
|
|
|
|
|
|
|
|
$plans.$scheme_name = $scheme_guid
|
|
|
|
|
|
|
|
|
|
|
|
#fail if older than 2008r2...need to do it here before Get-PowerPlans function runs further down
|
|
|
|
$i += 1
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
If ([System.Environment]::OSVersion.Version -lt '6.1')
|
|
|
|
return $plans
|
|
|
|
{
|
|
|
|
|
|
|
|
$result = @{
|
|
|
|
|
|
|
|
changed = $false
|
|
|
|
|
|
|
|
power_plan_name = $name
|
|
|
|
|
|
|
|
power_plan_enabled = $null
|
|
|
|
|
|
|
|
all_available_plans = $null
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Fail-Json $result "The win_power_plan Ansible module is only available on Server 2008r2 (6.1) and newer"
|
|
|
|
|
|
|
|
|
|
|
|
Function Get-ActivePowerPlan {
|
|
|
|
|
|
|
|
$buffer = [IntPtr]::Zero
|
|
|
|
|
|
|
|
$res = [Ansible.WinPowerPlan.NativeMethods]::PowerGetActiveScheme([IntPtr]::Zero, [ref]$buffer)
|
|
|
|
|
|
|
|
if ($res -ne 0) {
|
|
|
|
|
|
|
|
$err_msg = Get-LastWin32ErrorMessage -ErrorCode $res
|
|
|
|
|
|
|
|
Fail-Json -obj $result -message "Failed to get the active power plan - $err_msg"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$result = @{
|
|
|
|
try {
|
|
|
|
changed = $false
|
|
|
|
$active_guid = [System.Runtime.InteropServices.Marshal]::PtrToStructure($buffer, [Type][Guid])
|
|
|
|
power_plan_name = $name
|
|
|
|
} finally {
|
|
|
|
power_plan_enabled = (Get-PowerPlans $name).isactive
|
|
|
|
[Ansible.WinPowerPlan.NativeMethods]::LocalFree($buffer) > $null
|
|
|
|
all_available_plans = Get-PowerPlans
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$all_available_plans = Get-PowerPlans
|
|
|
|
return $active_guid
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#Terminate if plan is not found on the system
|
|
|
|
Function Set-ActivePowerPlan {
|
|
|
|
If (! ($all_available_plans.ContainsKey($name)) )
|
|
|
|
[CmdletBinding(SupportsShouldProcess=$true)]
|
|
|
|
{
|
|
|
|
param([Guid]$Plan)
|
|
|
|
Fail-Json $result "Defined power_plan: ($name) is not available"
|
|
|
|
|
|
|
|
|
|
|
|
$res = 0
|
|
|
|
|
|
|
|
if ($PSCmdlet.ShouldProcess($Plan, "Set Power Plan")) {
|
|
|
|
|
|
|
|
$res = [Ansible.WinPowerPlan.NativeMethods]::PowerSetActiveScheme([IntPtr]::Zero, $plan_guid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#If true, means plan is already active and we exit here with changed: false
|
|
|
|
if ($res -ne 0) {
|
|
|
|
#If false, means plan is not active and we move down to enable
|
|
|
|
$err_msg = Get-LastWin32ErrorMessage -ErrorCode $res
|
|
|
|
#Since the results here are the same whether check mode or not, no specific handling is required
|
|
|
|
Fail-Json -obj $result -message "Failed to set the active power plan to $name - $err_msg"
|
|
|
|
#for check mode.
|
|
|
|
|
|
|
|
If ( $all_available_plans.item($name) )
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Exit-Json $result
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Try {
|
|
|
|
|
|
|
|
$Null = Invoke-CimMethod -InputObject (Get-PowerPlans $name) -MethodName Activate -ErrorAction Stop -WhatIf:$check_mode
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Catch {
|
|
|
|
|
|
|
|
$result.power_plan_enabled = (Get-PowerPlans $name).IsActive
|
|
|
|
# Get all local power plans and the current active plan
|
|
|
|
$result.all_available_plans = Get-PowerPlans
|
|
|
|
$plans = Get-PowerPlans
|
|
|
|
Fail-Json $result "Failed to set the new plan: $($_.Exception.Message)"
|
|
|
|
$active_plan = Get-ActivePowerPlan
|
|
|
|
|
|
|
|
$result.all_available_plans = @{}
|
|
|
|
|
|
|
|
foreach ($plan_info in $plans.GetEnumerator()) {
|
|
|
|
|
|
|
|
$result.all_available_plans.($plan_info.Key) = $plan_info.Value -eq $active_plan
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($name -notin $plans.Keys) {
|
|
|
|
|
|
|
|
Fail-Json -obj $result -message "Defined power_plan: ($name) is not available"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$plan_guid = $plans.$name
|
|
|
|
|
|
|
|
$is_active = $active_plan -eq $plans.$name
|
|
|
|
|
|
|
|
$result.power_plan_enabled = $is_active
|
|
|
|
|
|
|
|
|
|
|
|
#set success parameters and exit
|
|
|
|
if (-not $is_active) {
|
|
|
|
|
|
|
|
Set-ActivePowerPlan -Plan $plan_guid -WhatIf:$check_mode
|
|
|
|
$result.changed = $true
|
|
|
|
$result.changed = $true
|
|
|
|
$result.power_plan_enabled = (Get-PowerPlans $name).IsActive
|
|
|
|
$result.power_plan_enabled = $true
|
|
|
|
$result.all_available_plans = Get-PowerPlans
|
|
|
|
foreach ($plan_info in $plans.GetEnumerator()) {
|
|
|
|
Exit-Json $result
|
|
|
|
$is_active = $plan_info.Value -eq $plan_guid
|
|
|
|
|
|
|
|
$result.all_available_plans.($plan_info.Key) = $is_active
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Exit-Json -obj $result
|
|
|
|
|
|
|
|
|
|
|
|