Fix OS version check in win_power_plan (#30538)

* Fixed win_power_plan OS version check

* Original error message case

(cherry picked from commit dc8aedb274)
pull/30734/head
Serge Nikalaichyk 7 years ago committed by jborean93
parent 70c1320c08
commit 22f352490d

@ -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
} }

Loading…
Cancel
Save