1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Fix OS version check in win_power_plan (#30538)

* Fixed win_power_plan OS version check

* Original error message case
This commit is contained in:
Serge Nikalaichyk 2017-09-21 15:23:02 -05:00 committed by Jordan Borean
parent fb628acb6e
commit dc8aedb274

View file

@ -13,17 +13,19 @@ $name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $tru
Function Get-PowerPlans { Function Get-PowerPlans {
Param ($PlanName) Param ($PlanName)
If (!$PlanName) { If (-not $PlanName) {
Get-CimInstance -Name root\cimv2\power -Class win32_PowerPlan | Select ElementName,IsActive | Get-CimInstance -Name root\cimv2\power -Class Win32_PowerPlan |
Foreach -begin { $ht=@{} } -process { $ht."$($_.ElementName)" = $_.IsActive } -end {$ht} Select-Object -Property ElementName, IsActive |
ForEach-Object -Begin { $ht = @{} } -Process { $ht."$($_.ElementName)" = $_.IsActive } -End { $ht }
} }
Else { Else {
Get-CimInstance -Name root\cimv2\power -Class win32_PowerPlan -Filter "ElementName = '$PlanName'" Get-CimInstance -Name root\cimv2\power -Class Win32_PowerPlan -Filter "ElementName = '$PlanName'"
} }
} }
#fail if older than 2008r2...need to do it here before Get-PowerPlans function runs further down #fail if older than 2008r2...need to do it here before Get-PowerPlans function runs further down
If ( (gcim Win32_OperatingSystem).version -lt 6.1)
If ([System.Environment]::OSVersion.Version -lt '6.1')
{ {
$result = @{ $result = @{
changed = $false changed = $false
@ -60,17 +62,17 @@ If ( $all_available_plans.item($name) )
Else Else
{ {
Try { Try {
$Null = Invoke-CimMethod -InputObject (Get-PowerPlans $name) -MethodName Activate -ea Stop -WhatIf:$check_mode $Null = Invoke-CimMethod -InputObject (Get-PowerPlans $name) -MethodName Activate -ErrorAction Stop -WhatIf:$check_mode
} }
Catch { Catch {
$result.power_plan_enabled = (Get-PowerPlans $name).isactive $result.power_plan_enabled = (Get-PowerPlans $name).IsActive
$result.all_available_plans = Get-PowerPlans $result.all_available_plans = Get-PowerPlans
Fail-Json $result "Failed to set the new plan: $($_.Exception.Message)" Fail-Json $result "Failed to set the new plan: $($_.Exception.Message)"
} }
#set success parameters and exit #set success parameters and exit
$result.changed = $true $result.changed = $true
$result.power_plan_enabled = (Get-PowerPlans $name).isactive $result.power_plan_enabled = (Get-PowerPlans $name).IsActive
$result.all_available_plans = Get-PowerPlans $result.all_available_plans = Get-PowerPlans
Exit-Json $result Exit-Json $result
} }