Invoke-Install.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
function Invoke-Install {
     <#
        .SYNOPSIS
            Gets all *.install.ps1 and executes them
        
        .DESCRIPTION
            Gets all *.install.ps1 files and will execute them one by one

        .PARAMETER Path
            Path to the *.install.ps1 file/files

        .PARAMETER Filter
            Filter for which scripts Invoke-Install should look for to execute them

        .EXAMPLE
            Invoke-Install HelloWorld.install.ps1

        .EXAMPLE
            Invoke-Install . -Filter ".*\.myfilter\.ps1"
    #>

    param (
        [Parameter(Position=0, Mandatory=$true, HelpMessage="Path to the *.install.ps1")]
        [string] $Path,

        [string] $Filter =  ".*\.install\.ps1"
    )

    $InstallScripts = Get-InstallScripts -Path $Path -Filter $Filter

    $Count = 0

    foreach ($Script in $InstallScripts) {
        . $Script
        $Count += 1
    }

    return $Count
}

function Get-InstallScripts {
         <#
        .SYNOPSIS
            Gets all scripts within a specific folder which match the filter
        
        .DESCRIPTION
            Gets all scripts within a specific folder which match the filter

        .PARAMETER Path
            Path to the files

        .PARAMETER Filter
            Filter for which scripts should be looked for

        .EXAMPLE
            Get-InstallScripts .

        .EXAMPLE
            Get-InstallScripts . -Filter ".*\.filter\.ps1"
    #>

    param(
        [Parameter(Position = 0, Mandatory = $true, HelpMessage = "Path to the *.install.ps1")]
        [string] $Path, 

        [string] $Filter
    )    

    $InstallScripts = @()
    
    if ((Get-Item $Path) -is [System.IO.DirectoryInfo]) {
        $FoundInstallScripts = Get-ChildItem $Path | Where-Object {$_.Name -match  $Filter }
        if ($FoundInstallScripts.Length -gt 0) {            
            $FoundInstallScripts | ForEach-Object {
                $InstallScripts += $_.FullName
            }
        }
    } else {
        if ($Path -match $Filter) {
            $InstallScripts += (Get-Item $Path)    
        }        
    }
    
   if ($InstallScripts.Length -lt 1) {
        Write-Host "No File found witch matches the Filter $Filter to run Invoke-Install on it"
    } else {
        return $InstallScripts
    }

}