DSCResources/VE_SFCommon/VE_SFCommon.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
Import-LocalizedData -BindingVariable localizedData -FileName Resources.psd1;

#region Private Functions

function ImportSFModule {
<#
    .SYNOPSIS
        Imports a Storefront Powershell module/snapin and throws an error if it's not available.
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [System.String[]] $Name,

        [Parameter()]
        [System.Management.Automation.SwitchParameter] $IsSnapin
    )
    process {
        foreach ($moduleName in $Name) {
            if (-not (TestSFModule -Name $moduleName -IsSnapin:$IsSnapin)) {
                ThrowInvalidProgramException -ErrorId $Name -ErrorMessage $localizedData.StorefrontSDKNotFoundError;
            }
            elseif ($IsSnapin) {
                Add-PSSnapin -Name $moduleName -Verbose:$false;
            }
            else {
                Import-Module -Name $moduleName -Force -Global -Verbose:$false;
            }
        } #end foreach module
    } #end process
} #end function FindModule

function TestSFModule {
<#
    .SYNOPSIS
        Tests whether a Storefront Powershell module/snapin is available/registered.
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [System.String[]] $Name,

        [Parameter()]
        [System.Management.Automation.SwitchParameter] $IsSnapin
    )
    process {
        $isCompliant = $true;
        foreach ($moduleName in $Name) {
            if ($IsSnapin) {
                if (-not (Get-PSSnapin -Name $moduleName -Registered)) {
                    $isCompliant = $false;
                }
            }
            else {
                if (-not (Get-Module -Name $moduleName -ListAvailable)) {
                    $isCompliant = $false;
                }
            }
        } #end foreach module
        return $isCompliant;
    } #end process
} #end TestModule

function RemoveDuplicateArrayMembers {
    [CmdletBinding()]
    [OutputType([System.String[]])]
    param (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [System.String[]] $Members
    )
    process {
        $destIndex = 0;
        for([int] $sourceIndex = 0 ; $sourceIndex -lt $Members.Count; $sourceIndex++) {
            $matchFound = $false;
            for([int] $matchIndex = 0; $matchIndex -lt $destIndex; $matchIndex++) {
                if($Members[$sourceIndex] -eq $Members[$matchIndex]) {
                    # A duplicate is found. Discard the duplicate.
                    Write-Verbose -Message ($localizedData.RemovingDuplicateEntry -f $Members[$sourceIndex]);
                    $matchFound = $true;
                    continue;
                }
            }

            if(-not $matchFound) {
                $Members[$destIndex++] = $Members[$sourceIndex].ToLowerInvariant();
            }
        }

        # Create the output array.
        $destination = New-Object -TypeName System.String[] -ArgumentList $destIndex;

        # Copy only distinct elements from the original array to the destination array.
        [System.Array]::Copy($Members, $destination, $destIndex);

        return $destination;
    } #end process
} #end function RemoveDuplicateMembers

function TestStringArrayEqual {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [System.String[]] $Expected,

        [Parameter(Mandatory)]
        [AllowNull()]
        [AllowEmptyString()]
        [System.String[]] $Actual
    )
    process {
        $inDesiredState = $true;
        ## Got to compare the arrays contents
        foreach ($value in $Expected) {
            if ($value -notin $Actual) {
                $inDesiredState = $false;
                break;
            }
        }
        if ($null -eq $Actual) {
            ## $Expected cannot be $null so if $Actual is $null..
            $inDesiredState = $false;
        }
        else {
            foreach ($value in $Actual) {
                if ($Expected -notcontains $value) {
                    $inDesiredState = $false;
                    break;
                }
            }
        }
        return $inDesiredState;
    } #end process
} #end function TestArray

function GetStoreFarm {
<#
    .SYNOPSIS
        Returns Storefront farm by virtual path and farm name
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [System.String] $StoreVirtualPath,

        [Parameter(Mandatory)]
        [System.String] $FarmName,

        [Parameter(ValueFromRemainingArguments)]
        $RemainingArguments
    )
    process {
        $store = GetStoreService -VirtualPath $StoreVirtualPath -ThrowIfNull;
        return (Get-STFStoreFarm -StoreService $store -FarmName $FarmName -ErrorAction SilentlyContinue);
    } #end process
} #end function GetStoreFarm

function GetWebReceiverService {
<#
    .SYNOPSIS
        Returns Storefront WebReceiver store by virtual path and site Id (if supplied)
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [System.String] $VirtualPath,

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

        [Parameter(ValueFromRemainingArguments)]
        $RemainingArguments
    )
    process {
        $getSTFWebReceiverServiceParams = @{
            VirtualPath = $VirtualPath;
        }
        if ($PSBoundParameters.ContainsKey('SiteId')) {
            $getSTFWebReceiverServiceParams['SiteId'] = $SiteId;
        }
        return (Get-STFWebReceiverService @getSTFWebReceiverServiceParams);
    } #end process
} #end function GetWebReceiverService

function GetStoreService {
<#
    .SYNOPSIS
        Returns Storefront store by virtual path and site Id (if supplied)
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [System.String] $VirtualPath,

        ## Store virtual path, e.g. /Citrix/Store
        [Parameter()]
        [System.String] $StoreVirtualPath,

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

        [Parameter()]
        [System.Management.Automation.SwitchParameter] $ThrowIfNull,

        [Parameter(ValueFromRemainingArguments = $true)]
        $RemainingArguments

    )
    process {
        $getSTFStoreServiceParams = @{
            VirtualPath = $VirtualPath;
        }
        if ($PSBoundParameters.ContainsKey('StoreVirtualPath')) {
            ## Override the VirtualPath with the more explicit StoreVirtualPath
            $getSTFStoreServiceParams['VirtualPath'] = $StoreVirtualPath;
        }
        if ($PSBoundParameters.ContainsKey('SiteId')) {
            $getSTFStoreServiceParams['SiteId'] = $SiteId;
        }
        $store = Get-STFStoreService @getSTFStoreServiceParams;
        if ($ThrowIfNull -and ($null -eq $store)) {
            $errorMessage = $localizedData.InvalidStorefrontStoreError -f $getSTFStoreServiceParams.VirtualPath;
            ThrowOperationCanceledException -ErrorId InvalidStore -ErrorMessage $errorMessage;
        }
        return $store;
    } #end process
} #end function GetStoreService

function GetAuthenticationService {
<#
    .SYNOPSIS
    Returns Storefront authentication service by virtual path and site Id (if supplied)
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [System.String] $VirtualPath,

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

        [Parameter()]
        [System.Management.Automation.SwitchParameter] $ThrowIfNull,

        [Parameter(ValueFromRemainingArguments)]
        $RemainingArguments
    )
    process {
        $getSTFAuthenticationServiceParams = @{
            VirtualPath = $VirtualPath;
        }
        if ($PSBoundParameters.ContainsKey('SiteId')) {
            $getSTFAuthenticationServiceParams['SiteId'] = $SiteId;
        }
        $authenticationService =Get-STFAuthenticationService @getSTFAuthenticationServiceParams;
        if ($ThrowIfNull -and ($null -eq $authenticationService)) {
            $errorMessage = $localizedData.InvalidStorefrontAuthenticationServiceError -f $VirtualPath;
            ThrowOperationCanceledException -ErrorId InvalidAuthenticationService -ErrorMessage $errorMessage;
        }
        return $authenticationService;
    } #end process
} #end function GetAuthenticationService

function GetAuthenticationServiceMethods {
<#
    .SYNOPSIS
    Returns Storefront authentication service methods by virtual path
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [System.String] $VirtualPath,

        [Parameter(ValueFromRemainingArguments)]
        $RemainingArguments
    )
    process {
        $authenticationService = GetAuthenticationService -VirtualPath $VirtualPath -ThrowIfNull;
        $authenticationMethods = Get-STFAuthenticationServiceProtocol -AuthenticationService $authenticationService |
            Where-Object { $_.Enabled -eq $true } |
                Select-Object -ExpandProperty Name;
        return $authenticationMethods;
    } #end process
} #end function GetAuthenticationServiceMethods

function AddInvokeScriptBlockCredentials {
    <#
    .SYNOPSIS
        Adds the required Invoke-Command parameters for loopback processing with CredSSP.
    #>

    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param (
        [Parameter(Mandatory)]
        [System.Collections.Hashtable] $Hashtable,

        [Parameter(Mandatory)]
        [System.Management.Automation.PSCredential] $Credential
    )
    process {
        $Hashtable['ComputerName'] = $env:COMPUTERNAME;
        $Hashtable['Credential'] = $Credential;
        $Hashtable['Authentication'] = 'Credssp';
    }
} #end function AddInvokeScriptBlockCredentials

function GetHostname {
    [CmdletBinding()]
    [OutputType([System.String])]
    param ( )
    process {
        $globalIpProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties();
        if ($globalIpProperties.DomainName) {
            return '{0}.{1}' -f $globalIpProperties.HostName, $globalIpProperties.DomainName;
        }
        else {
            return $globalIpProperties.HostName;
        }
    } #end process
} #end function GetHostname

function GetRegistryValue {
    <#
    .SYNOPSIS
        Returns a registry string value.
    .NOTES
        This is an internal function and shouldn't be called from outside.
        This function enables registry calls to be unit tested.
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param (
        # Registry key name/path to query.
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [Alias('Path')]
        [System.String] $Key,

        # Registry value to return.
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [System.String] $Name
    )
    process {
        $itemProperty = Get-ItemProperty -Path $Key -Name $Name -ErrorAction SilentlyContinue;
        if ($itemProperty.$Name) {
            return $itemProperty.$Name;
        }
        return '';
    }
} #end function GetRegistryValue

function StartWaitProcess {
    <#
    .SYNOPSIS
        Starts and waits for a process to exit.
    .NOTES
        This is an internal function and shouldn't be called from outside.
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([System.Int32])]
    param (
        # Path to process to start.
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [System.String] $FilePath,

        # Arguments (if any) to apply to the process.
        [Parameter()]
        [AllowNull()]
        [System.String[]] $ArgumentList,

        # Credential to start the process as.
        [Parameter()]
        [AllowNull()]
        [System.Management.Automation.PSCredential] $Credential,

        # Working directory
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [System.String] $WorkingDirectory = (Split-Path -Path $FilePath -Parent)
    )
    process {
        $startProcessParams = @{
            FilePath = $FilePath;
            WorkingDirectory = $WorkingDirectory;
            NoNewWindow = $true;
            PassThru = $true;
        };
        $displayParams = '<None>';
        if ($ArgumentList) {
            $displayParams = [System.String]::Join(' ', $ArgumentList);
            $startProcessParams['ArgumentList'] = $ArgumentList;
        }
        Write-Verbose ($localizedData.StartingProcess -f $FilePath, $displayParams);
        if ($Credential) {
            Write-Verbose ($localizedData.StartingProcessAs -f $Credential.UserName);
            $startProcessParams['Credential'] = $Credential;
        }
        if ($PSCmdlet.ShouldProcess($FilePath, 'Start Process')) {
            $process = Start-Process @startProcessParams -ErrorAction Stop;
        }
        if ($PSCmdlet.ShouldProcess($FilePath, 'Wait Process')) {
            Write-Verbose ($localizedData.ProcessLaunched -f $process.Id);
            Write-Verbose ($localizedData.WaitingForProcessToExit -f $process.Id);
            $process.WaitForExit();
            $exitCode = [System.Convert]::ToInt32($process.ExitCode);
            Write-Verbose ($localizedData.ProcessExited -f $process.Id, $exitCode);
        }
        return $exitCode;
    } #end process
} #end function StartWaitProcess



function ThrowInvalidArgumentError {
    <#
    .SYNOPSIS
        Throws terminating error of category InvalidArgument with specified errorId and errorMessage.
    #>

    param(
        [Parameter(Mandatory)]
        [System.String] $ErrorId,

        [Parameter(Mandatory)]
        [System.String] $ErrorMessage
    )
    $errorCategory = [System.Management.Automation.ErrorCategory]::InvalidArgument;
    $exception = New-Object -TypeName 'System.ArgumentException' -ArgumentList $ErrorMessage;
    $errorRecord = New-Object -TypeName 'System.Management.Automation.ErrorRecord' -ArgumentList $exception, $ErrorId, $errorCategory, $null;
    throw $errorRecord;
} #end function ThrowInvalidArgumentError

function ThrowInvalidOperationException {
    <#
    .SYNOPSIS
        Throws terminating error of category InvalidOperation with specified errorId and errorMessage.
    #>

    param(
        [Parameter(Mandatory)]
        [System.String] $ErrorId,

        [Parameter(Mandatory)]
        [System.String] $ErrorMessage
    )
    $errorCategory = [System.Management.Automation.ErrorCategory]::InvalidOperation;
    $exception = New-Object -TypeName 'System.InvalidOperationException' -ArgumentList $ErrorMessage;
    $errorRecord = New-Object -TypeName 'System.Management.Automation.ErrorRecord' -ArgumentList $exception, $ErrorId, $errorCategory, $null;
    throw $errorRecord;
} #end function ThrowInvalidOperationException

function ThrowInvalidProgramException {
    <#
    .SYNOPSIS
        Throws terminating error of category NotInstalled with specified errorId and errorMessage.
    #>

    param(
        [Parameter(Mandatory)]
        [System.String] $ErrorId,

        [Parameter(Mandatory)]
        [System.String] $ErrorMessage
    )
    $errorCategory = [System.Management.Automation.ErrorCategory]::NotInstalled;
    $exception = New-Object -TypeName 'System.InvalidProgramException' -ArgumentList $ErrorMessage;
    $errorRecord = New-Object -TypeName 'System.Management.Automation.ErrorRecord' -ArgumentList $exception, $ErrorId, $errorCategory, $null;
    throw $errorRecord;
} #end function ThrowInvalidProgramException

function ThrowOperationCanceledException {
    <#
    .SYNOPSIS
        Throws terminating error of category InvalidOperation with specified errorId and errorMessage.
    #>

    param(
        [Parameter(Mandatory)]
        [System.String] $ErrorId,

        [Parameter(Mandatory)]
        [System.String] $ErrorMessage
    )
    $errorCategory = [System.Management.Automation.ErrorCategory]::InvalidOperation;
    $exception = New-Object -TypeName 'System.OperationCanceledException' -ArgumentList $ErrorMessage;
    $errorRecord = New-Object -TypeName 'System.Management.Automation.ErrorRecord' -ArgumentList $exception, $ErrorId, $errorCategory, $null;
    throw $errorRecord;
} #end function ThrowOperationCanceledException

#endregion Private Functions