diff --git a/CHANGELOG.md b/CHANGELOG.md index 9af46041..892d9e40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Modules/ComputerManagementDsc/DSCResources/MSFT_PowerPlan/MSFT_PowerPlan.psm1 b/Modules/ComputerManagementDsc/DSCResources/MSFT_PowerPlan/MSFT_PowerPlan.psm1 index 00f92f90..1da716ae 100644 --- a/Modules/ComputerManagementDsc/DSCResources/MSFT_PowerPlan/MSFT_PowerPlan.psm1 +++ b/Modules/ComputerManagementDsc/DSCResources/MSFT_PowerPlan/MSFT_PowerPlan.psm1 @@ -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) { @@ -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) } } diff --git a/Modules/ComputerManagementDsc/DSCResources/MSFT_PowerPlan/en-US/MSFT_PowerPlan.strings.psd1 b/Modules/ComputerManagementDsc/DSCResources/MSFT_PowerPlan/en-US/MSFT_PowerPlan.strings.psd1 index 951d8789..137cba74 100644 --- a/Modules/ComputerManagementDsc/DSCResources/MSFT_PowerPlan/en-US/MSFT_PowerPlan.strings.psd1 +++ b/Modules/ComputerManagementDsc/DSCResources/MSFT_PowerPlan/en-US/MSFT_PowerPlan.strings.psd1 @@ -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} '@ diff --git a/Modules/ComputerManagementDsc/Modules/ComputerManagementDsc.Common/ComputerManagementDsc.Common.psm1 b/Modules/ComputerManagementDsc/Modules/ComputerManagementDsc.Common/ComputerManagementDsc.Common.psm1 index 1c3a0a4b..69178526 100644 --- a/Modules/ComputerManagementDsc/Modules/ComputerManagementDsc.Common/ComputerManagementDsc.Common.psm1 +++ b/Modules/ComputerManagementDsc/Modules/ComputerManagementDsc.Common/ComputerManagementDsc.Common.psm1 @@ -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]@{ @@ -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, ` @@ -540,4 +600,5 @@ Export-ModuleMember -Function ` Test-TimeZoneId, ` Set-TimeZoneId, ` Set-TimeZoneUsingDotNet, ` - Get-PowerPlans + Get-PowerPlan, ` + Set-PowerPlan diff --git a/Modules/ComputerManagementDsc/Modules/ComputerManagementDsc.Common/en-us/ComputerManagementDsc.Common.strings.psd1 b/Modules/ComputerManagementDsc/Modules/ComputerManagementDsc.Common/en-us/ComputerManagementDsc.Common.strings.psd1 index d3721f36..a47d45df 100644 --- a/Modules/ComputerManagementDsc/Modules/ComputerManagementDsc.Common/en-us/ComputerManagementDsc.Common.strings.psd1 +++ b/Modules/ComputerManagementDsc/Modules/ComputerManagementDsc.Common/en-us/ComputerManagementDsc.Common.strings.psd1 @@ -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. '@ diff --git a/Tests/Unit/ComputerManagementDsc.Common.Tests.ps1 b/Tests/Unit/ComputerManagementDsc.Common.Tests.ps1 index 651d0317..6921d0ce 100644 --- a/Tests/Unit/ComputerManagementDsc.Common.Tests.ps1 +++ b/Tests/Unit/ComputerManagementDsc.Common.Tests.ps1 @@ -691,74 +691,326 @@ try } } - Describe 'ComputerManagementDsc.Common\Get-PowerPlans' { - Context 'When only the "Balanced" power plan is available (default on Windows 10 for example)' { + Describe 'ComputerManagementDsc.Common\Get-PowerPlan' { + + $testCases =@( + # Power plan as name specified + @{ + Type = 'Friendly Name' + Name = 'High performance' + }, + # Power plan as Guid specified + @{ + Type = 'Guid' + Name = '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' + } + ) + + Context 'When the specified power plan is not available on the computer' { Mock ` - -CommandName Invoke-Expression ` - -MockWith { - return @( - "Existing Power Schemes (* Active)" - "-----------------------------------" - "Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced) *" - ) - } ` - -ParameterFilter {$Command -eq 'powercfg.exe /l'} + -CommandName powercfg.exe ` + -MockWith { + return @( + "Existing Power Schemes (* Active)" + "-----------------------------------" + "Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced) *" + ) + } ` + -Verifiable - It 'Should not throw exception' { - {Get-PowerPlans} | Should -Not -Throw + It 'Should not throw any exception (power plan specified as )' -TestCases $testCases { + + param + ( + [String] + $Name + ) + + {Get-PowerPlan -Name $Name} | Should -Not -Throw } - It 'Should return an object of type PSCustomObject' { - Get-PowerPlans | Should -BeOfType [PSCustomObject] + It 'Should return nothing (power plan specified as )' -TestCases $testCases { + + param + ( + [String] + $Name + ) + + Get-PowerPlan -Name $Name | Should -HaveCount 0 + Get-PowerPlan -Name $Name | Should -Be $null } - It 'Should return only one object' { - Get-PowerPlans | Should -HaveCount 1 + It 'Should call powercfg.exe once (power plan specified as )' -TestCases $testCases { + param + ( + [String] + $Name + ) + + Get-PowerPlan -Name $Name + Assert-MockCalled -CommandName powercfg.exe -Exactly -Times 1 -Scope It } - It 'Should be the active plan (property "IsActive" of the returned object should be $true)' { - (Get-PowerPlans).IsActive | Should -Be $true + Assert-VerifiableMock + } + + Context 'When the specified power plan is available on the computer and not active' { + + Mock ` + -CommandName powercfg.exe ` + -MockWith { + return @( + "Existing Power Schemes (* Active)" + "-----------------------------------" + "Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced) *" + "Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance)" + "Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a (Power saver)" + ) + } ` + -Verifiable + + It 'Should not throw any exception (power plan specified as )' -TestCases $testCases { + + param + ( + [String] + $Name + ) + + {Get-PowerPlan -Name $Name} | Should -Not -Throw } - It 'Should return a Object with the property "Name" with the value "Balanced"' { - (Get-PowerPlans).Name | Should -Be 'Balanced' + It 'Should return one object of type PSCustomObject (power plan specified as )' -TestCases $testCases { + + param + ( + [String] + $Name + ) + + $powerPlan = Get-PowerPlan -Name $Name + $powerPlan | Should -BeOfType [PSCustomObject] + $powerPlan | Should -HaveCount 1 } + + It 'Should not be the active plan (power plan specified as )' -TestCases $testCases { + + param + ( + [String] + $Name + ) + + $powerPlan = Get-PowerPlan -Name $Name + $powerPlan.IsActive | Should -Be $false + } + + It 'Should call powercfg.exe once (power plan specified as )' -TestCases $testCases { + param + ( + [String] + $Name + ) + + Get-PowerPlan -Name $Name + Assert-MockCalled -CommandName powercfg.exe -Exactly -Times 1 -Scope It + } + + Assert-VerifiableMock } - Context 'When three power plans are available (default on server OS)' { + Context 'When the specified power plan is available on the computer and is active' { + Mock ` - -CommandName Invoke-Expression ` - -MockWith { - return @( - "Existing Power Schemes (* Active)" - "-----------------------------------" - "Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced) *" - "Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance)" - "Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a (Power saver)" - ) - } ` - -ParameterFilter {$Command -eq 'powercfg.exe /l'} + -CommandName powercfg.exe ` + -MockWith { + return @( + "Existing Power Schemes (* Active)" + "-----------------------------------" + "Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)" + "Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance) *" + "Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a (Power saver)" + ) + } ` + -Verifiable - It 'Should not throw exception' { - {Get-PowerPlans} | Should -Not -Throw + It 'Should not throw any exception (power plan specified as )' -TestCases $testCases { + + param + ( + [String] + $Name + ) + + {Get-PowerPlan -Name $Name} | Should -Not -Throw + } + + It 'Should return one object of type PSCustomObject (power plan specified as )' -TestCases $testCases { + + param + ( + [String] + $Name + ) + + $powerPlan = Get-PowerPlan -Name $Name + $powerPlan | Should -BeOfType [PSCustomObject] + $powerPlan | Should -HaveCount 1 + } + + It 'Should not be the active plan (power plan specified as )' -TestCases $testCases { + + param + ( + [String] + $Name + ) + + $powerPlan = Get-PowerPlan -Name $Name + $powerPlan.IsActive | Should -Be $true } - It 'Should return an object of type PSCustomObject' { - Get-PowerPlans | Should -BeOfType [PSCustomObject] + It 'Should call powercfg.exe once (power plan specified as )' -TestCases $testCases { + param + ( + [String] + $Name + ) + + Get-PowerPlan -Name $Name + Assert-MockCalled -CommandName powercfg.exe -Exactly -Times 1 -Scope It } - It 'Should return three objects' { - Get-PowerPlans | Should -HaveCount 3 + Assert-VerifiableMock + } + + Context 'When the powercfg.exe throws an invalid parameters error' { + + $errorRecord = [System.Management.Automation.ErrorRecord]::new( + [System.Management.Automation.RemoteException]::new('Invalid Parameters -- try "/?" for help'), + 'NativeCommandError', + 'NotSpecified', + 'Invalid Parameters -- try "/?" for help' + ) + + Mock ` + -CommandName powercfg.exe ` + -MockWith { + throw $errorRecord + } ` + -Verifiable + + It 'Should throw the correct error (power plan specified as )' -TestCases $testCases { + + param + ( + [String] + $Name + ) + + $invalidOperationRecord = Get-InvalidOperationRecord ` + -ErrorRecord $errorRecord + + {Get-PowerPlan -Name $Name} | Should -Throw $invalidOperationRecord } - It 'Should be only have one plan marked as active' { - Get-PowerPlans | Where-Object{$_.IsActive -eq $true} | Should -HaveCount 1 + Assert-VerifiableMock + } + + Context 'When powercfg.exe /L returns nothing' { + + Mock ` + -CommandName powercfg.exe ` + -Verifiable + + It 'Should throw the correct error (power plan specified as )' -TestCases $testCases { + + param + ( + [String] + $Name + ) + + $invalidOperationRecord = Get-InvalidOperationRecord ` + -Message $localizedData.UnableToGetPowerPlans + + {Get-PowerPlan -Name $Name} | Should -Throw $invalidOperationRecord } - It 'Should be have the plan "Balanced" set as active' { - (Get-PowerPlans | Where-Object{$_.IsActive -eq $true}).Name | Should -Be 'Balanced' + Assert-VerifiableMock + } + } + + Describe 'ComputerManagementDsc.Common\Set-PowerPlan' { + Context 'When the specified power plan cloud be activated successfully' { + + Mock ` + -CommandName powercfg.exe ` + -Verifiable + + [Guid]$powerPlanGuid = '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' + + It 'Should not throw any exception (power plan specified as )' -TestCases $testCases { + + param + ( + [String] + $Name + ) + + {Set-PowerPlan -Guid $powerPlanGuid} | Should -Not -Throw } + + It 'Should call powercfg.exe once (power plan specified as )' -TestCases $testCases { + + param + ( + [String] + $Name + ) + + Set-PowerPlan -Guid $powerPlanGuid + Assert-MockCalled -CommandName powercfg.exe -Exactly -Times 1 -Scope It + } + + Assert-VerifiableMock } + + Context 'When the powercfg.exe throws an error while setting the specified power plan' { + $errorRecord = [System.Management.Automation.ErrorRecord]::new( + [System.Management.Automation.RemoteException]::new('Invalid Parameters -- try "/?" for help'), + 'NativeCommandError', + 'NotSpecified', + 'Invalid Parameters -- try "/?" for help' + ) + + [Guid]$powerPlanGuid = '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' + + Mock ` + -CommandName powercfg.exe ` + -MockWith { + throw $errorRecord + } ` + -Verifiable + + It 'Should throw the correct error (power plan specified as )' -TestCases $testCases { + + param + ( + [String] + $Name + ) + + $invalidOperationRecord = Get-InvalidOperationRecord ` + -ErrorRecord $errorRecord + + {Set-PowerPlan -Guid $powerPlanGuid} | Should -Throw $invalidOperationRecord + } + + Assert-VerifiableMock + } + } } } diff --git a/Tests/Unit/MSFT_PowerPlan.Tests.ps1 b/Tests/Unit/MSFT_PowerPlan.Tests.ps1 index 22ca674f..464a812d 100644 --- a/Tests/Unit/MSFT_PowerPlan.Tests.ps1 +++ b/Tests/Unit/MSFT_PowerPlan.Tests.ps1 @@ -33,13 +33,13 @@ try } $testCases =@( - #Power plan as name specified + # Power plan as name specified @{ Type = 'Name' Name = 'High performance' }, - #Power plan as Guid specified + # Power plan as Guid specified @{ Type = 'Guid' Name = '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' @@ -50,23 +50,13 @@ try Context 'When the system is in the desired present state' { BeforeEach { Mock ` - -CommandName Get-PowerPlans ` + -CommandName Get-PowerPlan ` -MockWith { return @( [PSCustomObject]@{ Name = 'High performance' Guid = [Guid]'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' IsActive = $true - }, - [PSCustomObject]@{ - Name = 'Balanced' - Guid = [Guid]'381b4222-f694-41f0-9685-ff5bb260df2e' - IsActive = $false - }, - [PSCustomObject]@{ - Name = 'Power saver' - Guid = [Guid]'a1841308-3541-4fab-bc81-f71556f20b4a' - IsActive = $false } ) } ` @@ -76,7 +66,8 @@ try It 'Should return the same values as passed as parameters (power plan specified as )' -TestCases $testCases { - Param( + param + ( [String] $Name ) @@ -91,23 +82,13 @@ try Context 'When the system is not in the desired present state' { BeforeEach { Mock ` - -CommandName Get-PowerPlans ` + -CommandName Get-PowerPlan ` -MockWith { return @( [PSCustomObject]@{ Name = 'High performance' Guid = [Guid]'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' IsActive = $false - }, - [PSCustomObject]@{ - Name = 'Balanced' - Guid = [Guid]'381b4222-f694-41f0-9685-ff5bb260df2e' - IsActive = $false - }, - [PSCustomObject]@{ - Name = 'Power saver' - Guid = [Guid]'a1841308-3541-4fab-bc81-f71556f20b4a' - IsActive = $true } ) } ` @@ -117,7 +98,8 @@ try It 'Should return an inactive plan (power plan specified as )' -TestCases $testCases { - Param( + param + ( [String] $Name ) @@ -132,21 +114,15 @@ try Context 'When the preferred plan does not exist' { BeforeEach { Mock ` - -CommandName Get-PowerPlans ` - -MockWith { - return [PSCustomObject]@{ - Name = 'Balanced' - Guid = [Guid]'381b4222-f694-41f0-9685-ff5bb260df2e' - IsActive = $false - } - } ` + -CommandName Get-PowerPlan ` -ModuleName $script:DSCResourceName ` -Verifiable } It 'Should throw the expected error (power plan specified as )' -TestCases $testCases { - Param( + param + ( [String] $Name ) @@ -156,31 +132,20 @@ try { Get-TargetResource -Name $Name -IsSingleInstance 'Yes' -Verbose } | Should -Throw $errorRecord } - } Assert-VerifiableMock } - + } Describe "$($script:DSCResourceName)\Set-TargetResource" { BeforeEach { Mock ` - -CommandName Get-PowerPlans ` + -CommandName Get-PowerPlan ` -MockWith { return @( [PSCustomObject]@{ Name = 'High performance' Guid = [Guid]'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' IsActive = $false - }, - [PSCustomObject]@{ - Name = 'Balanced' - Guid = [Guid]'381b4222-f694-41f0-9685-ff5bb260df2e' - IsActive = $false - }, - [PSCustomObject]@{ - Name = 'Power saver' - Guid = [Guid]'a1841308-3541-4fab-bc81-f71556f20b4a' - IsActive = $true } ) } ` @@ -188,44 +153,38 @@ try -Verifiable Mock ` - -CommandName Invoke-Expression ` - -ParameterFilter {$Command -eq 'powercfg.exe /S 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c'} ` + -CommandName Set-PowerPlan ` -ModuleName $script:DSCResourceName ` -Verifiable } Context 'When the system is not in the desired present state' { - It 'Should call powercfg.exe /S only once (power plan specified as )' -TestCases $testCases { + It 'Should call Set-PowerPlan once (power plan specified as )' -TestCases $testCases { - Param( + param + ( [String] $Name ) Set-TargetResource -Name $Name -IsSingleInstance 'Yes' -Verbose - Assert-MockCalled -CommandName Invoke-Expression -Exactly -Times 1 -Scope It -ModuleName $script:DSCResourceName + Assert-MockCalled -CommandName Set-PowerPlan -Exactly -Times 1 -Scope It -ModuleName $script:DSCResourceName } } Context 'When the preferred plan does not exist' { BeforeEach { Mock ` - -CommandName Get-PowerPlans ` - -MockWith { - return [PSCustomObject]@{ - Name = 'Balanced' - Guid = [Guid]'381b4222-f694-41f0-9685-ff5bb260df2e' - IsActive = $false - } - } ` + -CommandName Get-PowerPlan ` -ModuleName $script:DSCResourceName ` -Verifiable } It 'Should throw the expected error (power plan specified as )' -TestCases $testCases { - Param( + param + ( [String] $Name ) @@ -237,52 +196,19 @@ try } } - Context 'When the powercfg.exe throws an invalid parameters error' { - It 'Should catch the correct error thrown (power plan specified as )' -TestCases $testCases { - - Param( - [String] - $Name - ) - - Mock ` - -CommandName Invoke-Expression ` - -MockWith { Throw 'powercfg : Invalid Parameters -- try "/?" for help' } ` - -ParameterFilter {$Command -like 'powercfg.exe /S *'} ` - -ModuleName $script:DSCResourceName ` - -Verifiable - - $errorRecord = Get-InvalidOperationRecord ` - -Message ($LocalizedData.PowerPlanWasUnableToBeSet -f $Name, 'powercfg : Invalid Parameters -- try "/?" for help') - - { Set-TargetResource -Name $Name -IsSingleInstance 'Yes' -Verbose} | Should -Throw $errorRecord - } - } - Assert-VerifiableMock } - Describe "$($script:DSCResourceName)\Test-TargetResource" { Context 'When the system is in the desired present state' { BeforeEach { Mock ` - -CommandName Get-PowerPlans ` + -CommandName Get-PowerPlan ` -MockWith { return @( [PSCustomObject]@{ Name = 'High performance' Guid = [Guid]'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' IsActive = $true - }, - [PSCustomObject]@{ - Name = 'Balanced' - Guid = [Guid]'381b4222-f694-41f0-9685-ff5bb260df2e' - IsActive = $false - }, - [PSCustomObject]@{ - Name = 'Power saver' - Guid = [Guid]'a1841308-3541-4fab-bc81-f71556f20b4a' - IsActive = $false } ) } ` @@ -292,7 +218,8 @@ try It 'Should return the the state as present ($true) (power plan specified as )' -TestCases $testCases { - Param( + param + ( [String] $Name ) @@ -304,23 +231,13 @@ try Context 'When the system is not in the desired state' { BeforeEach { Mock ` - -CommandName Get-PowerPlans ` + -CommandName Get-PowerPlan ` -MockWith { return @( [PSCustomObject]@{ Name = 'High performance' Guid = [Guid]'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' IsActive = $false - }, - [PSCustomObject]@{ - Name = 'Balanced' - Guid = [Guid]'381b4222-f694-41f0-9685-ff5bb260df2e' - IsActive = $false - }, - [PSCustomObject]@{ - Name = 'Power saver' - Guid = [Guid]'a1841308-3541-4fab-bc81-f71556f20b4a' - IsActive = $true } ) } ` @@ -330,7 +247,8 @@ try It 'Should return the the state as absent ($false) (power plan specified as )' -TestCases $testCases { - Param( + param + ( [String] $Name )