Test/OneGetTestHelper.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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
#
# Copyright (c) Microsoft Corporation.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

<# Run Test cases Pre-Requisite:
  1. After download the OngetGet DSC resources modules, it is expected the following are available under your current directory. For example,
 
    C:\Program Files\WindowsPowerShell\Modules\PackageManagementProviderResource\
         
        DSCResources
        Examples
        Test
        PackageManagementProviderResource.psd1
#>


#Define the variables

$CurrentDirectory            = Split-Path -Path $MyInvocation.MyCommand.Path -Parent

$script:LocalRepositoryPath  = "$CurrentDirectory\LocalRepository"
$script:LocalRepositoryPath1 = "$CurrentDirectory\LocalRepository1"
$script:LocalRepositoryPath2 = "$CurrentDirectory\LocalRepository2"
$script:LocalRepositoryPath3 = "$CurrentDirectory\LocalRepository3"
$script:LocalRepository      = "LocalRepository"
$script:InstallationFolder   = $null
$script:DestinationPath      = $null
$script:Module               = $null



#A DSC configuration for installing Pester
configuration Sample_InstallPester
{
    <#
    .SYNOPSIS
 
    This is a DSC configution that install/uninstall the Pester tool from the nuget.
 
    .PARAMETER DestinationPath
    Provides the file folder where the Pester to be installed.
 
    #>


    param
    (
        #Destination path for the package
        [Parameter(Mandatory)]
        [string]$DestinationPath       
    )

    Import-DscResource -Module PackageManagementProviderResource -ModuleVersion 1.0.3

    Node "localhost"
    {
        
        #register package source
        PackageManagementSource SourceRepository
        {

            Ensure      = "Present"
            Name        = "Mynuget"
            ProviderName= "Nuget" 
            SourceUri   = "http://nuget.org/api/v2/"    
            InstallationPolicy ="Trusted"
        }   
        
        #Install a package from Nuget repository
        NugetPackage Nuget
        {
            Ensure          = "Present"
            Name            = "Pester"
            DestinationPath = $DestinationPath
            DependsOn       = "[PackageManagementSource]SourceRepository"
            InstallationPolicy="Trusted"
        }                              
    } 
}

Function InstallPester
{
    <#
    .SYNOPSIS
 
    This function downloads and installs the pester tool.
 
    #>


    Write-Verbose -Message ("Calling function '$($MyInvocation.mycommand)'") -Verbose

    # Check if the Pester have installed already under Program Files\WindowsPowerShell\Modules\Pester
    $pester = Get-Module -Name "Pester" -ListAvailable | select -first 1

    if ($pester.count -ge 1)
    {
        Write-Verbose -Message "Pester has already installed under $($pester.ModuleBase)" -Verbose

        Import-module -Name "$($pester.ModuleBase)\Pester.psd1"          
    }
    else
    {
        # Get the module path where to be installed
        $module = Get-Module -Name "PackageManagementProviderResource" -ListAvailable

        # Compile it
        Sample_InstallPester -DestinationPath "$($module.ModuleBase)\test"

        # Run it
        Start-DscConfiguration -path .\Sample_InstallPester -wait -Verbose -force 

        $result = Get-DscConfiguration 
    
        #import the Pester tool. Note:$result.Name is something like 'Pester.3.3.5'
        Import-module -Name "$($module.ModuleBase)\test\$($result[1].Name)\tools\Pester.psd1"
    }
 }


Function SetupLocalRepository
{
    <#
    .SYNOPSIS
 
    This is a helper function to setup a local repostiory/package resouce to speed up the test execution
 
    .PARAMETER PSModule
    Provides whether you are testing PowerShell Modules or Packages.
 
    #>


    param
    (
        [Switch]$PSModule
    )

    Write-Verbose -Message ("Calling function '$($MyInvocation.mycommand)'") -Verbose
    
    # Create the LocalRepository path if does not exist
    if (-not ( Test-Path -Path $script:LocalRepositoryPath))
    {
        New-Item -Path $script:LocalRepositoryPath -ItemType Directory -Force  
        New-Item -Path $script:LocalRepositoryPath1 -ItemType Directory -Force  
        New-Item -Path $script:LocalRepositoryPath2 -ItemType Directory -Force  
        New-Item -Path $script:LocalRepositoryPath3 -ItemType Directory -Force  
    }

    # UnRegister repository/sources
    UnRegisterAllSource

    # Register the local repository
    RegisterRepository -Name $script:LocalRepository -InstallationPolicy Trusted -Ensure Present

    # Create test modules for the test automation
    if ($PSModule)
    {
        # Set up for PSModule testing
        CreateTestModuleInLocalRepository -ModuleName "MyTestModule"  -ModuleVersion "1.1"    -LocalRepository $script:LocalRepository
        CreateTestModuleInLocalRepository -ModuleName "MyTestModule"  -ModuleVersion "1.1.2"  -LocalRepository $script:LocalRepository
        CreateTestModuleInLocalRepository -ModuleName "MyTestModule"  -ModuleVersion "3.2.1"  -LocalRepository $script:LocalRepository
    }
    else
    {
        #Setup for nuget and others testing
        CreateTestModuleInLocalRepository -ModuleName "MyTestPackage" -ModuleVersion "12.0.1"   -LocalRepository $script:LocalRepository
        CreateTestModuleInLocalRepository -ModuleName "MyTestPackage" -ModuleVersion "12.0.1.1" -LocalRepository $script:LocalRepository
        CreateTestModuleInLocalRepository -ModuleName "MyTestPackage" -ModuleVersion "15.2.1"   -LocalRepository $script:LocalRepository
    }

    # Replica the repository
    Copy-Item -Path "$script:LocalRepositoryPath\*" -Destination $script:LocalRepositoryPath1 -Recurse -force -Verbose
    Copy-Item -Path "$script:LocalRepositoryPath\*" -Destination $script:LocalRepositoryPath2 -Recurse -force -Verbose
    Copy-Item -Path "$script:LocalRepositoryPath\*" -Destination $script:LocalRepositoryPath3 -Recurse -force -Verbose
}

Function SetupPSModuleTest
{
    <#
    .SYNOPSIS
 
    This is a helper function for a PSModule test
 
    #>


    Write-Verbose -Message ("Calling function '$($MyInvocation.mycommand)'") -Verbose

    #Need to import resource MSFT_PSModule.psm1
    Import-ModulesToSetupTest -ModuleChildPath  "MSFT_PSModule\MSFT_PSModule.psm1"  

    SetupLocalRepository -PSModule 

    # Install Pester and import it
    InstallPester      
}

Function SetupNugetTest
{
    <#
    .SYNOPSIS
 
    This is a helper function for a Nuget test
 
    #>

    Write-Verbose -Message ("Calling function '$($MyInvocation.mycommand)'") -Verbose

    #Import MSFT_NugetPackage.psm1 module
    Import-ModulesToSetupTest -ModuleChildPath  "MSFT_NugetPackage\MSFT_NugetPackage.psm1"
    
    $script:DestinationPath = "$CurrentDirectory\TestResult\NugetTest" 

    SetupLocalRepository

    # Install Pester and import it
    InstallPester
 }

Function SetupOneGetSourceTest
{
    <#
    .SYNOPSIS
 
    This is a helper function for a PackageManagementSource test
 
    #>

    Write-Verbose -Message ("Calling function '$($MyInvocation.mycommand)'") -Verbose

    Import-ModulesToSetupTest -ModuleChildPath  "MSFT_PackageManagementSource\MSFT_PackageManagementSource.psm1"

    SetupLocalRepository

    # Install Pester and import it
    InstallPester 
}

function SetupPackageManagementTest
{
    <#
    .SYNOPSIS
 
    This is a helper function for a PackageManagement test
 
    #>

    param([switch]$SetupPSModuleRepository)

    Write-Verbose -Message ("Calling function '$($MyInvocation.mycommand)'") -Verbose

    Import-ModulesToSetupTest -ModuleChildPath  "MSFT_PackageManagement\MSFT_PackageManagement.psm1"

    $script:DestinationPath = "$CurrentDirectory\TestResult\PackageManagementTest" 

    SetupLocalRepository
    if ($SetupPSModuleRepository) 
    {
        SetupLocalRepository -PSModule 
        $script:PSModuleBase = "$env:ProgramFiles\windowspowershell\modules"
    }

    # Install Pester and import it
    InstallPester 

}

Function Import-ModulesToSetupTest
{
    <#
    .SYNOPSIS
 
    This is a helper function to import modules
     
    .PARAMETER ModuleChildPath
    Provides the child path of the module. The parent path should be the same as the DSC resource.
    #>


    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $ModuleChildPath

    )
  
    Write-Verbose -Message ("Calling function '$($MyInvocation.mycommand)'") -Verbose

    $moduleChildPath="DSCResources\$($ModuleChildPath)"

    $script:Module = Get-Module -Name "PackageManagementProviderResource" -ListAvailable

    $modulePath = Microsoft.PowerShell.Management\Join-Path -Path $script:Module.ModuleBase -ChildPath $moduleChildPath

    # Using -Force to reload the module (while writing tests..it is common to change product code)
    Import-Module -Name "$($modulePath)"  -Force
    
    #c:\Program Files\WindowsPowerShell\Modules
    $script:InstallationFolder = "$($script:Module.ModuleBase)" 
 }

function RegisterRepository
{
    <#
    .SYNOPSIS
 
    This is a helper function to register/unregister the PowerShell repository
 
    .PARAMETER Name
    Provides the repository Name.
 
    .PARAMETER SourceLocation
    Provides the source location.
 
    .PARAMETER PublishLocation
    Provides the publish location.
 
    .PARAMETER InstallationPolicy
    Determines whether you trust the source repository.
 
    .PARAMETER Ensure
    Determines whether the repository to be registered or unregistered.
    #>


    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $Name,

        [System.String]
        $SourceLocation=$script:LocalRepositoryPath,
   
           [System.String]
        $PublishLocation=$script:LocalRepositoryPath,

        [ValidateSet("Trusted","Untrusted")]
        [System.String]
        $InstallationPolicy="Trusted",

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

    Write-Verbose -Message "RegisterRepository called" -Verbose

    # Calling the following to trigger Bootstrap provider for the first time use PackageManagement
    Get-PackageSource -ProviderName Nuget -ForceBootstrap -WarningAction Ignore 

    $psrepositories = PowerShellGet\get-PSRepository
    $registeredRepository = $null
    $isRegistered = $false

    #Check if the repository has been registered already
    foreach ($repository in $psrepositories)
    {
        # The PSRepository is considered as "exists" if either the Name or Source Location are in used
        $isRegistered = ($repository.SourceLocation -ieq $SourceLocation) -or ($repository.Name -ieq $Name) 

        if ($isRegistered)
        {
            $registeredRepository = $repository
            break;
        }
    }

    if($Ensure -ieq "Present")
    {       
        # If the repository has already been registered, unregister it.
        if ($isRegistered -and ($null -ne $registeredRepository))
        {
            Unregister-PSRepository -Name $registeredRepository.Name
        }       

        PowerShellGet\Register-PSRepository -Name $Name -SourceLocation $SourceLocation -PublishLocation $PublishLocation -InstallationPolicy $InstallationPolicy
    }
    else
    {
        # The repository has already been registered
        if (-not $isRegistered)
        {
            return
        }

        PowerShellGet\UnRegister-PSRepository -Name $Name
    }            
}

function RestoreRepository
{
    <#
    .SYNOPSIS
 
    This is a helper function to reset back your test environment.
 
    .PARAMETER RepositoryInfo
    Provides the hashtable containing the repository information used for regsitering the repositories.
    #>


    param
    (
        [parameter(Mandatory = $true)]
        [Hashtable]
        $RepositoryInfo
    )

    Write-Verbose -Message "RestoreRepository called"  -Verbose
       
    foreach ($repository in $RepositoryInfo.Keys)
    {
        try
        {
            $null = PowerShellGet\Register-PSRepository -Name $RepositoryInfo[$repository].Name `
                                            -SourceLocation $RepositoryInfo[$repository].SourceLocation `
                                            -PublishLocation $RepositoryInfo[$repository].PublishLocation `
                                            -InstallationPolicy $RepositoryInfo[$repository].InstallationPolicy `
                                            -ErrorAction SilentlyContinue 
        }
        #Ignore if the repository already registered
        catch
        {
            if ($_.FullyQualifiedErrorId -ine "PackageSourceExists")
            {
                throw
            }
        }                                    
    }   
}

function CleanupRepository
{
    <#
    .SYNOPSIS
 
    This is a helper function for the test setp. Sometimes tests require no other repositories
    are registered, this function helps to do so
 
    #>


    Write-Verbose -Message "CleanupRepository called" -Verbose

    $returnVal = @{}
    $psrepositories = PowerShellGet\get-PSRepository

    foreach ($repository in $psrepositories)
    {
        #Save the info for later restore process
        $repositoryInfo = @{"Name"=$repository.Name; `
                            "SourceLocation"=$repository.SourceLocation; `
                            "PublishLocation"=$repository.PublishLocation;`
                            "InstallationPolicy"=$repository.InstallationPolicy}

        $returnVal.Add($repository.Name, $repositoryInfo);

        try
        {
            $null = Unregister-PSRepository -Name $repository.Name -ErrorAction SilentlyContinue 
        }
        catch
        {
            if ($_.FullyQualifiedErrorId -ine "RepositoryCannotBeUnregistered")
            {
                throw
            }
        }         
    }   
    
    Return $returnVal   
}

function RegisterPackageSource
{
    <#
    .SYNOPSIS
 
    This is a helper function to register/unregister the package source
 
    .PARAMETER Name
    Provides the package source Name.
 
    .PARAMETER SourceUri
    Provides the source location.
 
    .PARAMETER PublishLocation
    Provides the publish location.
 
    .PARAMETER Credential
    Provides the access to the package on a remote source.
 
    .PARAMETER InstallationPolicy
    Determines whether you trust the source repository.
 
    .PARAMETER ProviderName
    Provides the package provider name.
 
    .PARAMETER Ensure
    Determines whether the package source to be registered or unregistered.
    #>


    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $Name,

        #Source location. It can be source name or uri
        [System.String]
        $SourceUri,

        [System.Management.Automation.PSCredential]
        $Credential,
    
        [System.String]
        [ValidateSet("Trusted","Untrusted")]
        $InstallationPolicy ="Untrusted",

        [System.String]
        $ProviderName="Nuget",

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

    Write-Verbose -Message "Calling RegisterPackageSource" -Verbose

    #import the OngetSource module
    Import-ModulesToSetupTest -ModuleChildPath  "MSFT_PackageManagementSource\MSFT_PackageManagementSource.psm1"
    
    if($Ensure -ieq "Present")
    {       
        # If the repository has already been registered, unregister it.
        UnRegisterSource -Name $Name -ProviderName $ProviderName -SourceUri $SourceUri       

        MSFT_PackageManagementSource\Set-TargetResource -Name $name `
                                             -providerName $ProviderName `
                                             -SourceUri $SourceUri `
                                             -SourceCredential $Credential `
                                             -InstallationPolicy $InstallationPolicy `
                                             -Verbose `
                                             -Ensure Present
    }
    else
    {
        # The repository has already been registered
        UnRegisterSource -Name $Name -ProviderName $ProviderName -SourceUri $SourceUri
    } 
    
    # remove the OngetSource module, after we complete the register/unregister task
    Remove-Module -Name  "MSFT_PackageManagementSource"  -Force -ErrorAction SilentlyContinue         
}

Function UnRegisterSource
{
    <#
    .SYNOPSIS
 
    This is a helper function to unregister a particular package source
 
    .PARAMETER Name
    Provides the package source Name.
 
    .PARAMETER SourceUri
    Provides the source location.
 
    .PARAMETER ProviderName
    Provides the package provider name.
    #>


    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $Name,

        [System.String]
        $SourceUri,

        [System.String]
        $ProviderName="Nuget"
    )

    Write-Verbose -Message ("Calling function '$($MyInvocation.mycommand)'") -Verbose

    $getResult = MSFT_PackageManagementSource\Get-TargetResource -Name $name -providerName $ProviderName -SourceUri $SourceUri -Verbose

    if ($getResult.Ensure -ieq "Present")
    {
        #Unregister it
        MSFT_PackageManagementSource\Set-TargetResource -Name $name -providerName $ProviderName -SourceUri $SourceUri -Verbose -Ensure Absent               
    }
}

Function UnRegisterAllSource
{
    <#
    .SYNOPSIS
 
    This is a helper function to unregister all the package source on the machine
 
    #>


    Write-Verbose -Message ("Calling function '$($MyInvocation.mycommand)'") -Verbose

    $sources = PackageManagement\Get-PackageSource

    foreach ($source in $sources)
    {
        try
        {
            #Unregister whatever can be unregistered
            PackageManagement\Unregister-PackageSource -Name $source.Name -providerName $source.ProviderName -ErrorAction SilentlyContinue  2>&1   
        }
        catch
        {
            if ($_.FullyQualifiedErrorId -ine "RepositoryCannotBeUnregistered")
            {
                throw
            }
        }         
    }
}

function CreateCredObject
{
    <#
    .SYNOPSIS
 
    This is a helper function for the cmdlets testing where requires PSCredential
 
    #>


    param(           
        [System.String]
        $Name,
        
        [System.String]
        $PSCode
        )


    Write-Verbose -Message ("Calling function '$($MyInvocation.mycommand)'") -Verbose

    $secCode = ConvertTo-SecureString -String $PSCode -AsPlainText -Force
    $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($Name, $secCode)
    return $cred
}

function CreateTestModuleInLocalRepository
{
    <#
    .SYNOPSIS
 
    This is a helper function that generates test packages/modules and publishes them to a local repository.
    Please note that it only generates manifest files just for testing purpose.
 
    .PARAMETER ModuleName
    Provides the module Name to be generated.
 
    .PARAMETER ModuleVersion
    Provides the module version to be generated.
 
    .PARAMETER LocalRepository
    Provides the local repository Name.
    #>


    param(
        [System.String]
        $ModuleName, 

        [System.String]
        $ModuleVersion,

        [System.String]
        $LocalRepository
    )

    Write-Verbose -Message ("Calling function '$($MyInvocation.mycommand)'") -Verbose

    # Return if the package already exists
    if (Test-path -path "$($script:Module.ModuleBase)\test\$($LocalRepository)\$($ModuleName).$($ModuleVersion).nupkg")
    {
        return
    }

    # Get the parent 'PackageManagementProviderResource' module path
    $parentModulePath = Microsoft.PowerShell.Management\Split-Path -Path $script:Module.ModuleBase -Parent

    $modulePath = Microsoft.PowerShell.Management\Join-Path -Path $parentModulePath -ChildPath "$ModuleName"

    New-Item -Path $modulePath -ItemType Directory -Force

    $modulePSD1Path = "$modulePath\$ModuleName.psd1"

    # Create the module manifest
    Microsoft.PowerShell.Core\New-ModuleManifest -Path $modulePSD1Path -Description "$ModuleName" -ModuleVersion $ModuleVersion

    try
    {
        # Publish the module to your local repository
        PowerShellGet\Publish-Module -Path $modulePath -NuGetApiKey "Local-Repository-NuGet-ApiKey" -Repository $LocalRepository -Verbose -ErrorAction SilentlyContinue         
    }
    catch
    { 
        # Ignore the particular error
        if ($_.FullyQualifiedErrorId -ine "ModuleVersionShouldBeGreaterThanGalleryVersion,Publish-Module")
        {
            throw
        }               
    }

    # Remove the module under modulepath once we published it to the local repository
    Microsoft.PowerShell.Management\Remove-item -Path $modulePath -Recurse -Force -ErrorAction SilentlyContinue
}

function ConvertHashtableToArryCimInstance
{
  <#
    .SYNOPSIS
 
    This helper function is mainly used to convert AdditionalParameters of PackageMangement DSC resource
    to Microsoft.Management.Infrastructure.CimInstance[]. This will enable writing DRTs for Get/Set/Test
    methods.
 
    #>

    [OutputType([Microsoft.Management.Infrastructure.CimInstance[]])]
    param([Hashtable] $AdditionalParameters = $(throw "AdditionalParameters cannot be null."))

    [Microsoft.Management.Infrastructure.CimInstance[]] $result = [Microsoft.Management.Infrastructure.CimInstance[]]::new($AdditionalParameters.Count)

    $index = 0
    $AdditionalParameters.Keys | % {
        $instance = New-CimInstance -ClassName MSFT_KeyValuePair -Namespace root/microsoft/Windows/DesiredStateConfiguration -Property @{
            Key = $_
            Value = $AdditionalParameters[$_]
            } -ClientOnly
        $result[$index] = $instance
        $index++
    }

    $result
}

function IsAdmin
{
    <#
    .SYNOPSIS
        Checks whether the current session is Elevated. Used for test suites which has this
        requirement
    #>

    [OutputType([bool])]
    
    param()
        try {
        $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
        $principal = New-Object Security.Principal.WindowsPrincipal -ArgumentList $identity
        return $principal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator )
    } catch {
    }

    return $false
}