|
| 1 | +Import-Module -Name (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) ` |
| 2 | + -ChildPath 'CommonResourceHelper.psm1') |
| 3 | +$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_xPowerPlan' |
| 4 | + |
| 5 | +<# |
| 6 | + .SYNOPSIS |
| 7 | + Returns the current state of the power plan. |
| 8 | +
|
| 9 | + .PARAMETER IsSingleInstance |
| 10 | + Specifies the resource is a single instance, the value must be 'Yes'. |
| 11 | +
|
| 12 | + .PARAMETER Name |
| 13 | + Specifies the name of the power plan to assign to the node. |
| 14 | +
|
| 15 | + .EXAMPLE |
| 16 | + Get-TargetResource -IsSingleInstance 'Yes' -Name 'High performance' |
| 17 | +#> |
| 18 | +function Get-TargetResource |
| 19 | +{ |
| 20 | + [CmdletBinding()] |
| 21 | + [OutputType([System.Collections.Hashtable])] |
| 22 | + param |
| 23 | + ( |
| 24 | + # This is best practice when writing a single-instance DSC resource. |
| 25 | + [Parameter(Mandatory = $true)] |
| 26 | + [ValidateSet('Yes')] |
| 27 | + [System.String] |
| 28 | + $IsSingleInstance, |
| 29 | + |
| 30 | + [Parameter(Mandatory = $true)] |
| 31 | + [ValidateNotNullOrEmpty()] |
| 32 | + [System.String] |
| 33 | + $Name |
| 34 | + ) |
| 35 | + |
| 36 | + $arguments = @{ |
| 37 | + Name = 'root\cimv2\power' |
| 38 | + Class = 'Win32_PowerPlan' |
| 39 | + Filter = "ElementName = '$Name'" |
| 40 | + } |
| 41 | + |
| 42 | + try |
| 43 | + { |
| 44 | + $plan = Get-CimInstance @arguments |
| 45 | + } |
| 46 | + catch |
| 47 | + { |
| 48 | + throw ($script:localizedData.PowerPlanCIMError -f $($arguments.Class) ) |
| 49 | + } |
| 50 | + |
| 51 | + if ($plan) |
| 52 | + { |
| 53 | + if ($plan.IsActive) |
| 54 | + { |
| 55 | + Write-Verbose -Message ($script:localizedData.PowerPlanIsActive -f $Name) |
| 56 | + $activePlanName = $Name |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + Write-Verbose -Message ($script:localizedData.PowerPlanIsNotActive -f $Name) |
| 61 | + $activePlanName = $null |
| 62 | + } |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + throw ($script:localizedData.PowerPlanNotFound -f $Name) |
| 67 | + } |
| 68 | + |
| 69 | + return @{ |
| 70 | + IsSingleInstance = $IsSingleInstance |
| 71 | + Name = $activePlanName |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +<# |
| 76 | + .SYNOPSIS |
| 77 | + Assign the power plan to the node. |
| 78 | +
|
| 79 | + .PARAMETER IsSingleInstance |
| 80 | + Specifies the resource is a single instance, the value must be 'Yes'. |
| 81 | +
|
| 82 | + .PARAMETER Name |
| 83 | + Specifies the name of the power plan to assign to the node. |
| 84 | +
|
| 85 | + .EXAMPLE |
| 86 | + Set-TargetResource -IsSingleInstance 'Yes' -Name 'High performance' |
| 87 | +#> |
| 88 | +function Set-TargetResource |
| 89 | +{ |
| 90 | + [CmdletBinding()] |
| 91 | + param |
| 92 | + ( |
| 93 | + # This is best practice when writing a single-instance DSC resource. |
| 94 | + [Parameter(Mandatory = $true)] |
| 95 | + [ValidateSet('Yes')] |
| 96 | + [System.String] |
| 97 | + $IsSingleInstance, |
| 98 | + |
| 99 | + [Parameter(Mandatory = $true)] |
| 100 | + [ValidateNotNullOrEmpty()] |
| 101 | + [System.String] |
| 102 | + $Name |
| 103 | + ) |
| 104 | + |
| 105 | + Write-Verbose -Message ($script:localizedData.PowerPlanIsBeingActivated -f $Name) |
| 106 | + |
| 107 | + $arguments = @{ |
| 108 | + Name = 'root\cimv2\power' |
| 109 | + Class = 'Win32_PowerPlan' |
| 110 | + Filter = "ElementName = '$Name'" |
| 111 | + } |
| 112 | + |
| 113 | + try |
| 114 | + { |
| 115 | + $plan = Get-CimInstance @arguments |
| 116 | + } |
| 117 | + catch |
| 118 | + { |
| 119 | + throw ($script:localizedData.PowerPlanCIMError -f $($arguments.Class) ) |
| 120 | + } |
| 121 | + |
| 122 | + try |
| 123 | + { |
| 124 | + $plan | Invoke-CimMethod -MethodName Activate |
| 125 | + } |
| 126 | + catch |
| 127 | + { |
| 128 | + throw ($script:localizedData.PowerPlanWasUnableToBeSet -f $Name, $($_.Exception.Message)) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +<# |
| 133 | + .SYNOPSIS |
| 134 | + Tests if the power plan is assigned to the node. |
| 135 | +
|
| 136 | + .PARAMETER IsSingleInstance |
| 137 | + Specifies the resource is a single instance, the value must be 'Yes'. |
| 138 | +
|
| 139 | + .PARAMETER Name |
| 140 | + Specifies the name of the power plan to assign to the node. |
| 141 | +
|
| 142 | + .EXAMPLE |
| 143 | + Test-TargetResource -IsSingleInstance 'Yes' -Name 'High performance' |
| 144 | +#> |
| 145 | +function Test-TargetResource |
| 146 | +{ |
| 147 | + [CmdletBinding()] |
| 148 | + [OutputType([System.Boolean])] |
| 149 | + param |
| 150 | + ( |
| 151 | + # This is best practice when writing a single-instance DSC resource. |
| 152 | + [Parameter(Mandatory = $true)] |
| 153 | + [ValidateSet('Yes')] |
| 154 | + [System.String] |
| 155 | + $IsSingleInstance, |
| 156 | + |
| 157 | + [Parameter(Mandatory = $true)] |
| 158 | + [ValidateNotNullOrEmpty()] |
| 159 | + [System.String] |
| 160 | + $Name |
| 161 | + ) |
| 162 | + |
| 163 | + $returnValue = $false |
| 164 | + |
| 165 | + Write-Verbose -Message ($script:localizedData.PowerPlanIsBeingValidated -f $Name) |
| 166 | + |
| 167 | + $getTargetResourceResult = Get-TargetResource -IsSingleInstance $IsSingleInstance -Name $Name |
| 168 | + if ($getTargetResourceResult.Name -eq $Name) |
| 169 | + { |
| 170 | + $returnValue = $true |
| 171 | + } |
| 172 | + |
| 173 | + return $returnValue |
| 174 | +} |
| 175 | + |
| 176 | +Export-ModuleMember -Function *-TargetResource |
0 commit comments