functions/Get-DbaRandomizedDatasetTemplate.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
function Get-DbaRandomizedDatasetTemplate {
    <#
    .SYNOPSIS
        Gets the dataset templates
 
    .DESCRIPTION
        Retrieves the templates from the default directory and if assigned custom directories
 
    .PARAMETER Template
        The name of the template to use.
        It will go through the default templates to see if it's present
 
    .PARAMETER Path
        Path or paths that contain template files
 
    .PARAMETER ExcludeDefault
        Exclude the default templates
 
    .PARAMETER WhatIf
        If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.
 
    .PARAMETER Confirm
        If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.
 
    .PARAMETER EnableException
        By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
        This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
        Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
 
    .NOTES
        Tags: DataGeneration
        Author: Sander Stad (@sqlstad, sqlstad.nl)
 
        Website: https://dbatools.io
        Copyright: (c) 2018 by dbatools, licensed under MIT
        License: MIT https://opensource.org/licenses/MIT
 
    .LINK
        https://dbatools.io/Get-DbaRandomizedDatasetTemplate
 
    .EXAMPLE
        Get-DbaRandomizedDatasetTemplate
 
        Get the templates from the default directory
 
    .EXAMPLE
        Get-DbaRandomizedDatasetTemplate -Template Personaldata, Test
 
        Get the templates from thedefault directory and filter on PersonalData and Test
 
    .EXAMPLE
        Get-DbaRandomizedDatasetTemplate -Path C:\DatasetTemplates
 
        Get the templates from a custom directory
 
    #>

    [CmdLetBinding()]
    param(
        [string[]]$Template,
        [string[]]$Path,
        [switch]$ExcludeDefault,
        [switch]$EnableException
    )

    begin {
        $templates = @()

        # Get all the default templates
        if (-not $ExcludeDefault) {
            $templates += Get-ChildItem (Resolve-Path -Path "$script:PSModuleRoot\bin\randomizer\templates\*.json")
        }
    }

    process {
        if (Test-FunctionInterrupt) { return}

        # Get the templates from the file path
        foreach ($p in $Path) {
            $templates += Get-ChildItem (Resolve-Path -Path "$Path\*.json")
        }

        # Filter the template if neccesary
        if ($Template) {
            $templates = $templates | Where-Object BaseName -in $Template
        }

        $templates | Select-Object BaseName, FullName
    }
}