functions/Get-DbaRandomizedType.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
function Get-DbaRandomizedType {
    <#
    .SYNOPSIS
        Get the randomized types and sub types
 
    .DESCRIPTION
        Retrieves the types and sub types available
 
    .PARAMETER RandomizedType
        Filter the randomized types
 
    .PARAMETER RandomizedSubType
        Filter the randomized sub types
 
    .PARAMETER Pattern
        Get the types and sub types based on a pattern
 
    .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-DbaRandomizedType
 
        Get all the types and subtypes
 
    .EXAMPLE
        Get-DbaRandomizedType -Pattern "Addr"
 
        Find all the types and sub types based on a pattern
 
    .EXAMPLE
        Get-DbaRandomizedType -RandomizedType Person
 
        Find all the sub types for Person
 
    .EXAMPLE
        Get-DbaRandomizedType -RandomizedSubType LastName
 
        Get all the types and subtypes that known by "LastName"
 
    #>

    [CmdLetBinding()]

    param(
        [string[]]$RandomizedType,
        [string[]]$RandomizedSubType,
        [string]$Pattern,
        [switch]$EnableException
    )

    begin {

        # Get all the random possibilities
        try {
            $randomizerTypes = Import-Csv (Resolve-Path -Path "$script:PSModuleRoot\bin\randomizer\en.randomizertypes.csv")
        } catch {
            Stop-Function -Message "Could not import randomized types" -Continue
        }

    }

    process {

        if (Test-FunctionInterrupt) { return }

        $types = @()

        if ($Pattern) {
            $types += $randomizerTypes | Where-Object Type -match $Pattern
            $types += $randomizerTypes | Where-Object SubType -match $Pattern
        } else {
            $types = $randomizerTypes
        }

        if ($RandomizedType) {
            $types = $types | Where-Object Type -in $RandomizedType
        }

        if ($RandomizedSubType) {
            $types = $types | Where-Object SubType -in $RandomizedSubType
        }

        $types | Select-Object Type, SubType -Unique | Sort-Object Type, SubType

    }

}