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

function Get-TargetResource {
    [CmdletBinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSDSCUseVerboseMessageInDSCResource', '')]
    [OutputType([System.Collections.Hashtable])]
    param (
        [Parameter(Mandatory)] [ValidateNotNullOrEmpty()]
        [System.String] $Name,

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

        $targetResource = @{
            Name = '';
            Ensure = $Ensure;
        }
        $listOfDDCs = GetRegistryValue -Key 'HKLM:\SOFTWARE\Citrix\VirtualDesktopAgent' -Name 'ListOfDDCs';
        if ($listOfDDCs) {
            $targetResource['Name'] = $listOfDDCs.Split(' ');
        }
        return $targetResource;

    } #end process
} #end Get-TargetResource


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

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

        $targetResource = Get-TargetResource @PSBoundParameters;
        if ($Ensure -eq 'Present') {
            ## Ensure that the controller is in the list
            if ($targetResource.Name -notcontains $Name) {
                Write-Verbose ($localizedData.MissingDeliveryController -f $Name);
                $targetResource['Ensure'] = 'Absent';
            }
        }
        else {
            ## Ensure that the controller is NOT in the list
            if ($targetResource.Name -contains $Name) {
                Write-Verbose ($localizedData.AdditionalDeliveryController -f $Name);
                $targetResource['Ensure'] = 'Present';
            }
        }
        if ($targetResource.Ensure -eq $Ensure) {
            Write-Verbose $localizedData.ResourceInDesiredState;
            return $true;
        }
        else {
            Write-Verbose $localizedData.ResourceNotInDesiredState;
            return $false;
        }

    } #end process
} #end Test-TargetResource


function Set-TargetResource {
    [CmdletBinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
    param (
        [Parameter(Mandatory)] [ValidateNotNullOrEmpty()]
        [System.String] $Name,

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

        $listOfDDCs = GetRegistryValue -Key 'HKLM:\SOFTWARE\Citrix\VirtualDesktopAgent' -Name 'ListOfDDCs';
        $ddcs = New-Object -TypeName 'System.Collections.ArrayList' -ArgumentList @();
        if (-not [System.String]::IsNullOrEmpty($listOfDDCs)) {
            $ddcs.AddRange($listOfDDCs.Split(' '));
        }

        ## Ensure that the controller is in the list
        if ($Ensure -eq 'Present') {
            Write-Verbose ($localizedData.AddingDeliveryController -f $Name);
            [ref] $null = $ddcs.Add($Name);
        }
        ## Ensure that the controller is NOT in the list
        if ($Ensure -eq 'Absent') {
            Write-Verbose ($localizedData.RemovingDeliveryController -f $Name);
            [ref] $null = $ddcs.Remove($Name);
        }
        $listOfDDCs = [System.String]::Join(' ', $ddcs.ToArray());
        Write-Verbose ($localizedData.SettingRegSZValue -f 'ListOfDDCs', $listOfDDCs);
        Set-ItemProperty -Path 'HKLM:\SOFTWARE\Citrix\VirtualDesktopAgent' -Name 'ListOfDDCs' -Value $listOfDDCs;
        Write-Verbose ($localizedData.RestartingService -f 'BrokerAgent');
        Restart-Service -Name 'BrokerAgent' -Force;

    } #end process
} #end Set-TargetResource


$moduleRoot = Split-Path -Path $MyInvocation.MyCommand.Path -Parent;

## Import the XD7Common library functions
$moduleParent = Split-Path -Path $moduleRoot -Parent;
Import-Module (Join-Path -Path $moduleParent -ChildPath 'VE_XD7Common');

Export-ModuleMember -Function *-TargetResource;