DSCResources/VE_vIPAddress/VE_vIPAddress.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
119
120
data localized {
    # culture="en-US"
    ConvertFrom-StringData -StringData @'
        GettingIPAddress = Getting the IP address for network adapter '{0}'.
        SettingIPAddress = Setting IP address '{0}' for network adapter '{1}'.
        ResourcePropertyMismatch = Property '{0}' does not match the desired state; expected '{1}', actual '{2}'.
        ResourceInDesiredState = Resource '{0}' is in the desired state.
        ResourceNotInDesiredState = Resource '{0}' is NOT in the desired state.
'@

}

function Get-TargetResource {
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param (
        [Parameter(Mandatory)]
        [String] $IPAddress,

        [Parameter(Mandatory)]
        [String] $InterfaceAlias,

        [Parameter(Mandatory)]
        [UInt32] $SubnetMask,

        [Parameter()] [ValidateSet('IPv4')]
        [String] $AddressFamily = 'IPv4'
    )
        
    Write-Verbose -Message ($localized.GettingIPAddress -f $InterfaceAlias);
    $configuration = Get-LegacyNetAdapterConfiguration -Name $InterfaceAlias;

    $IPAddresses = @();
    for ($i = 0; $i -lt $configuration.IPAddress.Count; $i++) { 
        if ($configuration.IPAddress[$i] -match '\.') {
            $IPAddresses += $configuration.IPAddress[$i];
            $subnetCIDR = ConvertTo-CIDR -SubnetMask $configuration.IPSubnet[$i];
        }
    }

    $targetResource = @{
        IPAddress = $IPAddresses -join ',';
        InterfaceAlias = $InterfaceAlias;
        SubnetMask = $subnetCIDR;
        AddressFamily = $AddressFamily;
    }

    return $targetResource;

} #end function Get-TargetResource

function Test-TargetResource {
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param (
        [Parameter(Mandatory)]
        [String] $IPAddress,

        [Parameter(Mandatory)]
        [String] $InterfaceAlias,

        [Parameter(Mandatory)]
        [UInt32] $SubnetMask,

        [Parameter()] [ValidateSet('IPv4')]
        [String] $AddressFamily = 'IPv4'
    )

    $targetResource = Get-TargetResource @PSBoundParameters;
    $inDesiredState = $true;

    if ($targetResource.IPAddress -notcontains $IPAddress) {
        Write-Verbose -Message ($localized.ResourcePropertyMismatch -f 'IPAddress', $IPAddress, $targetResource.IPAddress);
        $inDesiredState = $false;   
    }
    elseif ($targetResource.SubnetMask -ne $SubnetMask) {
        Write-Verbose -Message ($localized.ResourcePropertyMismatch -f 'SubnetMask', $SubnetMask, $targetResource.SubnetMask);
        $inDesiredState = $false;   
    }

    if ($inDesiredState) {
        Write-Verbose -Message ($localized.ResourceInDesiredState -f $InterfaceAlias);
        return $true;
    }
    else {
        Write-Verbose -Message ($localized.ResourceNotInDesiredState -f $InterfaceAlias);
        return $false;
    }

} #end function Test-TargetResource

function Set-TargetResource {
    param (
        [Parameter(Mandatory)]
        [String] $IPAddress,

        [Parameter(Mandatory)]
        [String] $InterfaceAlias,

        [Parameter(Mandatory)]
        [UInt32] $SubnetMask,

        [Parameter()] [ValidateSet('IPv4')]
        [String] $AddressFamily = 'IPv4'
    )

    $configuration = Get-LegacyNetAdapterConfiguration -Name $InterfaceAlias;
    $subnetMaskString = ConvertFrom-CIDR -CIDR $SubnetMask;
    $ipAddressString = '{0}/{1}' -f $IPAddress, $subnetMaskString;
    Write-Verbose -Message ($localized.SettingIPAddress -f $ipAddressString, $InterfaceAlias);
    [ref] $null = $configuration.EnableStatic($IPAddress, $subnetMaskString);

} #end function Set-TargetResource

## Import the private functions into this module's scope
Get-ChildItem -Path $PSScriptRoot\..\..\Lib -Filter '*.ps1' | ForEach-Object {
    . $_.FullName;
}

Export-ModuleMember -Function *-TargetResource;