DSCResources/VE_SFCluster/VE_SFCluster.ps1

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
Import-LocalizedData -BindingVariable localizedData -FileName Resources.psd1;

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

        [Parameter()]
        [System.UInt64] $SiteId = 1,

        [Parameter()] [ValidateSet('Present','Absent')]
        [System.String] $Ensure = 'Present'

    )
    process {
        ImportSFModule -Name Citrix.Storefront;
        $stfDeployment = Get-STFDeployment -SiteId $SiteId -WarningAction SilentlyContinue;
        $targetResource = @{
            BaseUrl = $stfDeployment.HostbaseUrl;
            SiteId = $stfDeployment.SiteId;
            Ensure = if ($stfDeployment) { 'Present' } else { 'Absent' };
        }
        return $targetResource;
    } #end process
} #end function Get-TargetResource

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

        [Parameter()]
        [System.UInt64] $SiteId = 1,

        [Parameter()] [ValidateSet('Present','Absent')]
        [System.String] $Ensure = 'Present'
    )
    process {
        $targetResource = Get-TargetResource @PSBoundParameters;
        $inDesiredState = $true;

        if ($Ensure -ne $targetResource.Ensure) {
            Write-Verbose -Message ($localizedData.ClusterPropertyMismatch -f 'Ensure', $Ensure, $targetResource.Ensure);
            $inDesiredState = $false;
        }

        ## Only check all remaing properties if we're setting
        if ($Ensure -eq 'Present') {
            $properties = @('BaseUrl','SiteId');
            foreach ($property in $properties) {
                $propertyValue = (Get-Variable -Name $property).Value;
                if ($targetResource.$property -ne $propertyValue) {
                    Write-Verbose -Message ($localizedData.ClusterPropertyMismatch -f $property, $propertyValue, $targetResource.$property);
                    $inDesiredState = $false;
                }
            }
        }

        if ($inDesiredState) {
            Write-Verbose -Message ($localizedData.ResourceInDesiredState -f $BaseUrl);
        }
        else {
            Write-Verbose -Message ($localizedData.ResourceNotInDesiredState -f $BaseUrl);
        }
        return $inDesiredState;
    } #end process
} #end function Test-TargetResource


function Set-TargetResource {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [System.String] $BaseUrl,

        [Parameter()]
        [System.UInt64] $SiteId = 1,

        [Parameter()] [ValidateSet('Present','Absent')]
        [System.String] $Ensure = 'Present'
    )
    process {
        $targetResource = Get-TargetResource @PSBoundParameters;

        if ($Ensure -eq 'Absent') {
            if ($targetResource.Ensure = 'Present') {
                ## Cluster exists, removing
                Write-Verbose -Message ($localizedData.RemovingStorefrontCluster -f $BaseUrl);
                [ref] $null = Clear-STFDeployment -SiteId $SiteId -Confirm:$false;
            }
        }
        elseif ($Ensure -eq 'Present') {
            if ($targetResource.Ensure -eq 'Present') {
                Write-Verbose -Message ($localizedData.UpdatingStorefrontClusterUrl -f $BaseUrl);
                [ref] $null = Set-STFDeployment -SiteId $SiteId -HostBaseUrl $BaseUrl -Confirm:$false;
            }
            else {
                ## Cluster does not exist, creating
                Write-Verbose -Message ($localizedData.AddingStorefrontCluster -f $BaseUrl);
                [ref] $null = Add-STFDeployment -HostBaseUrl $BaseUrl -SiteId 1 -Confirm:$false;
            }
        }
        #
        #Clear-STFDeployment -SiteId
    } #end process
} #end function Set-TargetResource