Dictionaries/Dict.OS/Includes/Bootloader/Includes/Bootloader.BCD.psm1

<#
.SYNOPSIS
Search for a bcd configuration
 
.DESCRIPTION
Search into local partition for a bcd.cfg file.
Default search for a "bcd.cfg" file. If config name is overriden, use -Filename to search for custom filename.
 
.EXAMPLE
Find-OSBootloaderBCDCfg -Path "/boot"
 
.EXAMPLE
Find-OSBootloaderBCDCfg -Path "/boot" -Filename "mybcd.cfg"
 
.NOTES
General notes
#>

function Find-OSBootloaderBCDCfg {
    [CmdletBinding()]
    [OutputType([String])]
    Param (
        # Path to start search
        [Parameter(Mandatory = $true, ValueFromPipeLine = $true)][string]$Path,
        # Custom filename to search for
        [Parameter(Mandatory = $false, ValueFromPipeLine = $false)][string]$Filename = "BCD",
        # Search as far as MaxDepth deep
        [Parameter(Mandatory = $false, ValueFromPipeLine = $false)][uint32]$MaxDepth = 3

    )
    Begin {
        Write-PwShFwOSEnterFunction
    }

    Process {
        $bcdcfg = Get-ChildItem -Path $Path/EFI/Microsoft/Boot $Filename -Recurse -Depth $MaxDepth -ErrorAction SilentlyContinue
        return ${bcdcfg}?.fullname
    }

    End {
        Write-PwShFwOSLeaveFunction
    }
}

function Set-OSBootloaderBCDDefaultBoot {
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    Param (
        [Alias('ConfigFile')]
        [Parameter(Mandatory = $false, ValueFromPipeLine = $false)][string]$BCDCfg = (Find-OSBootloaderbcdCfg -Path "/EFI"),
        [Parameter(Mandatory = $true, ValueFromPipeLine = $false)][string]$label
    )
    Begin {
        Write-PwShFwOSEnterFunction
    }

    Process {
        $bcd = Get-BCD
        $bcdEntry = $bcd | Where-Object { $_.label -eq "$label" }
        Set-BCDDisplayOrder -id $bcdEntry.id -AddFirst
        return $?
    }

    End {
        Write-PwShFwOSLeaveFunction
    }
}

function Get-OSBootloaderBCDBootEntries {
    [CmdletBinding()]
    [OutputType([String])]
    Param (
        # Custom filename to search on
        [Parameter(Mandatory = $false, ValueFromPipeLine = $false)][string]$Filename,

        # Show all entries label
        [Parameter(Mandatory = $false, ParameterSetName = 'ALL')][switch]$All
    )
    Begin {
        Write-PwShFwOSEnterFunction
        $bcd = Get-BCD -Filename $Filename
    }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'ID' {
                return ($bcd | Where-Object { $_.id -eq $Id })
            }
            'UEFILABEL' {
                # $id = ($bcd.keys | Where-Object { $bcd.$_.description -eq $UefiLabel })
                # return $bcd.$id
                return ($bcd | Where-Object { $_.label -eq $UefiLabel })
            }
            'KEYVALUE' {
                # $id = ($bcd.keys | Where-Object { $bcd.$_.$Key -eq $Value })
                # return $bcd.$id
                return ($bcd | Where-Object { $_.$Key -eq $Value })
            }
            default {
                # return only UEFI entries, not bootloader entries
                $bootmgr = Get-BCDEntryDetails -Id "{bootmgr}" -AsGUID
                return ($bcd | Where-Object { $_.id -in $bootmgr.displayorder })
            }
        }
    }

    End {
        Write-PwShFwOSLeaveFunction
    }
}