src/dynamicparams/GetImportNameParameterSet.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
using module .\New-DynamicParam.ps1

function GetImportNameParameterSet {
    [CmdletBinding(PositionalBinding = $false)]
    Param(
        [Parameter(Mandatory = $true)]
        [String]$ProfilePath,

        [Parameter(Mandatory = $true)]
        [ValidateSet("Uncomment", "Comment")]
        [String]
        $LineStatus,

        [Parameter(Mandatory = $false)]
        [int]$Position = 0,
        
        [switch]$ByPassForDocumentation,

        [switch]$Mandatory
    )

    $ProfileRaw = Get-Content -Path $ProfilePath -Raw
    if ($ProfileRaw) {
        if ($LineStatus -eq "Uncomment") {
            $ModuleBases = [regex]::Matches($ProfileRaw, "^\s*(?<!\#)\s*(?:Import-Module).*[\\|\/](?<ModuleName>\S*)", [System.Text.RegularExpressions.RegexOptions]::Multiline) | `
                ForEach-Object {$_.Groups['ModuleName'].Value}
        }
        else {
            $ModuleBases = [regex]::Matches($ProfileRaw, "[\#]+\s*(?:Import-Module).*[\\|\/](?<ModuleName>\S*)") | `
                ForEach-Object {$_.Groups['ModuleName'].Value}
        }
    }

    New-DynamicParam -Name 'Name' -Position $Position -ValidateSet $ModuleBases -Mandatory:$Mandatory.IsPresent
}