DSCResources/J0F3_cPowerPlan/J0F3_cPowerPlan.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        # Best practice for DSC resource which should be ony once specified in a configuration
        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('Yes')]
        [System.String]
        $IsSingleInstance = 'Yes',

        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('Balanced','High performance','Power saver')]
        [System.String]
        $PowerPlan

    )

    # Get the list of Power Plans
    $PowerCfg = (Invoke-Command { PowerCfg -l })
    $PowerCfg = $PowerCfg[3..$PowerCfg.Count]

    # Locate the name of the active plan
    $ActiveItem = ($PowerCfg | Select-String -Pattern "*" -CaseSensitive:$False -SimpleMatch).ToString()
    $IX1 = $ActiveItem.IndexOf("(")
    $IX2 = $ActiveItem.IndexOf(")")
    $ActivePlan = $ActiveItem.SubString($IX1+1, $IX2-$IX1-1)

    $PlanInfo = @{
        PowerPlan = $ActivePlan
        IsSingleInstance = $IsSingleInstance
    }

    return $PlanInfo
}

function Set-TargetResource
{
    [CmdletBinding()]
    param
    (

        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("Yes")]
        [System.String]
        $IsSingleInstance = 'Yes',

        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('Balanced','High performance','Power saver')]
        [System.String]
        $PowerPlan

    )

    Write-Verbose "Setting desired power plan ($PowerPlan)..."
    # Get GUID for disred plan
    $DesiredPlanGUID = $PowerPlans.Get_Item($PowerPlan)

    Invoke-Command {PowerCfg -s $DesiredPlanGUID}
}

function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("Yes")]
        [System.String]
        $IsSingleInstance = 'Yes',

        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('Balanced','High performance','Power saver')]
        [System.String]
        $PowerPlan

    )

    Write-Verbose -Message "Checking if the correct power plan is set..."

    $current = Get-TargetResource @PSBoundParameters

    Write-Debug "Current PowerPlan: $($current.PowerPlan)"
    Write-Debug "Desired PowerPlan: $PowerPlan"

    # Get GUID of the power plans
    $CurrentPlanGUID = $PowerPlans.Get_Item($($current.PowerPlan))
    $DesiredPlanGUID = $PowerPlans.Get_Item($PowerPlan)
    if($CurrentPlanGUID  -eq $DesiredPlanGUID)
    {
        Write-Verbose "OK. Desired Power plan is set ($($current.PowerPlan))"
        return $true
    }
    else
    {
        return $false
    }
}

# Hashtable to look up the GUID for a powerplan by its name
# The GUIDs are documented at https://msdn.microsoft.com/en-us/library/windows/desktop/aa373177(v=vs.85).aspx
$PowerPlans = @{
    'Balanced' = [GUID]'381b4222-f694-41f0-9685-ff5bb260df2e';
    'High performance' = [GUID]'8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c';
    'Power saver' = [GUID]'a1841308-3541-4fab-bc81-f71556f20b4a';
}

Export-ModuleMember -Function *-TargetResource