Public/Get-DotsScript.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
function Get-DotsScript {
    <#
    .SYNOPSIS
        Get Dots scripts

    .DESCRIPTION
        Get Dots scripts

    .PARAMETER DataSource
        Limit which scripts are returned by data source: ExternalSources, or DotsSources

    .PARAMETER Include
        Limit which scripts are returned to this whitelist. Accepts wildcards

    .PARAMETER Exclude
        Limit which scripts are returned by ignoring these blacklisted scripts. Accepts wildcards

    .PARAMETER Dependencies
        Identify and sort which scripts must run before other scripts via this hash table
        Using the DotsConfig -ScriptOrder is preferable

        Key is script that has dependencies. Value is an array of scripts the Key script depends on

        Example ensuring DataSourceFirst1 and DataSourceFirst2 run before DataSource1:
        @{
            DataSource1 = 'DataSourceFirst1', 'DataSourceFirst2'
        }

    .PARAMETER ScriptsPath
        Path to look for scripts in
        Must include ExternalSources and DotsSources subfolder for -DataSource to work

        If more than one ScriptsPath is specified and duplicate script names are found,
        we pick the first script found

    .EXAMPLE
        Get-DotsScript
        # Show all Dots scripts

    .EXAMPLE
        Get-DotsScript -Include *AD*
        # Show Dots scripts with a name like *AD*

    .FUNCTIONALITY
        Dots
    #>

    [cmdletbinding()]
    param(
        [validateset('ExternalSources', 'DotsSources')]
        [string]$DataSource,
        [string[]]$Include,
        [string[]]$Exclude,
        [hashtable]$Dependencies,
        [string[]]$ScriptsPath = $Script:ScriptsPath,
        [bool]$IncludeDotsScripts = $Script:IncludeDotsScripts
    )
    if($IncludeDotsScripts) {
        $ScriptsPath += Join-Path $ModuleRoot Scripts | Select-Object -Unique
    }

    # This bit will ensure one script per base name, with priority based on ScriptsPath
    # (i.e. First name found based on ScriptsPath wins)
    $ScriptsMap = [ordered]@{}
    foreach($Path in $ScriptsPath) {
        $Scripts = Get-ChildItem $ScriptsPath -Recurse -Include *.ps1
        foreach($Script in $Scripts) {
            if($ScriptsMap.Keys -notcontains $Script.BaseName){
                $ScriptsMap.add($Script.BaseName, $Script)
            }
        }
    }
    $Scripts = $ScriptsMap.Values
    Write-Verbose "Found scripts $($Scripts.FullName | Out-String)"
    if($Script:ScriptsToRun.count -gt 0) {
        $Scripts = foreach($Script in $Scripts) {
            foreach($Name in $Script:ScriptsToRun) {
                if($Script.BaseName -Like $Name) {
                    $Script
                    break
                }
            }
        }
        Write-Verbose "Including only ScriptsToRun (See Get-DotsConfig) scripts: $($Scripts.FullName | Out-String)"
    }
    if($Script:ScriptsToIgnore.count -gt 0) {
        $ResolvedExcludes = foreach($Script in $Scripts) {
            foreach($Name in $Script:ScriptsToIgnore) {
                if($Script.BaseName -like $Name) {
                    $Script.FullName
                    break
                }
            }
        }
        $Scripts = $Scripts | Where-Object {$ResolvedExcludes -NotContains $_}
        Write-Verbose "Excluding ScriptsToIgnore (see Get-DotsConfig) scripts: $($Scripts.FullName | Out-String)"
    }
    if($DataSource -eq 'ExternalSources') {
        $Scripts = $Scripts | Where-Object {$_.FullName -like "*ExternalSources\*.ps1"}
        Write-Verbose "Including only ExternalSources scripts: $($Scripts.FullName | Out-String)"
    }
    if($DataSource -eq 'DotsSources') {
        $Scripts = $Scripts | Where-Object {$_.FullName -like "*DotsSources\*.ps1" }
        Write-Verbose "Including only DotsSources scripts: $($Scripts.FullName | Out-String)"
    }
    if($Include) {
        $Scripts = foreach($Script in $Scripts) {
            foreach($Name in $Include) {
                if($Script.BaseName -Like $Name) {
                    $Script
                    break
                }
            }
        }
        Write-Verbose "Including only -include scripts: $($Scripts.FullName | Out-String)"
    }
    if($Exclude) {
        $ResolvedExcludes = foreach($Script in $Scripts) {
            foreach($Name in $Exclude) {
                if($Script.BaseName -like $Name) {
                    $Script.FullName
                    break
                }
            }
        }
        $Scripts = $Scripts | Where-Object {$ResolvedExcludes -NotContains $_}
        Write-Verbose "Excluding specified scripts: $($Scripts.FullName | Out-String)"
    }
    # Sort by file first, then dependencies (dependencies should override)
    if($Script:ScriptOrder) {
        $Scripts = $Scripts | Sort-CustomList -List $Script:ScriptOrder -SortOnProperty BaseName
        Write-Verbose "Sorting scripts with order [$Script:ScriptOrder]"
    }
    if($Dependencies) {
        $DependencyOrder = Get-TopologicalSort $Dependencies
        $Scripts = Sort-ObjectWithCustomList -InputObject $Scripts -Property BaseName -CustomList $DependencyOrder
        Write-Verbose "Sorting scripts with dependencies [$($Dependencies| Out-String)]"
    }
    $Scripts
}