Libraries/Bootloader/Bootloader.psm1

function Find-OSBootloaders {
    [CmdletBinding()]
    [OutputType([hashtable])]
    Param (
        # Parition UUID to search
        [Parameter(Mandatory = $false, ParameterSetName = 'PARTUUID')]
        [string]$PartUUID,
        # Label of a partition
        [Alias('Label', 'PartitionLabel')]
        [Parameter(Mandatory = $false, ParameterSetName = 'PARTLABEL')]
        [string]$PartLabel,
        # MountPoint of a currently mounted volume or partition
        [Alias('Volume', 'Letter', 'DriveLetter')]
        [Parameter(Mandatory = $false, ParameterSetName = 'MOUNTPOINT')]
        [string]$MountPoint,
        # disk device name
        [AllowNull]
        [Parameter(Mandatory = $false, ParameterSetName = 'DISKPART')]
        [string]$Disk,
        # partition number
        [Parameter(Mandatory = $false, ParameterSetName = 'DISKPART')]
        [string]$PartNum,
        # full device address
        [Parameter(Mandatory = $false, ParameterSetName = 'DEVICE')]
        [string]$Device
    )
    Begin {
        Write-EnterFunction
    }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'MOUNTPOINT' {
            }
            default {
                $MountPoint = Mount-ComputerDiskPartition @PSBoundParameters
            }
        }
        $bootloaders = @{}
        # looking for bcd
        $bcdcfg = Find-OSBootloaderbcdCfg -Path $MountPoint
        if ($bcdcfg) {
            Write-Debug "Found bcd bootloader configuration file."
            $bootloaders.Add("bcd", @{
                name = "bcd"
                cfgFullname = $bcdcfg.FullName
                cfg = $bcdcfg.FullName -replace "$Mountpoint"
            })
        }
        # looking for grub
        $grubcfg = Find-OSBootloaderGrub2Cfg -Path $MountPoint
        if ($grubcfg) {
            Write-Debug "Found grub bootloader configuration file."
            $bootloaders.Add("grub2", @{
                name = "grub2"
                cfgFullname = $grubcfg.FullName
                cfg = $grubcfg.FullName -replace "$Mountpoint"
            })
        }
        # looking for refind
        $refindcfg = Find-OSBootloaderRefindCfg -Path $MountPoint
        if ($refindcfg) {
            Write-Debug "Found refind bootloader configuration file."
            $bootloaders.Add("refind", @{
                name = "refind"
                cfgFullname = $refindcfg.FullName
                cfg = $refindcfg.FullName -replace "$Mountpoint"
            })
        }
        # looking for syslinux
        $syslinuxcfg = Find-OSBootloadersyslinuxCfg -Path $MountPoint
        if ($syslinuxcfg) {
            Write-Debug "Found syslinux bootloader configuration file."
            $bootloaders.Add("syslinux", @{
                name = "syslinux"
                cfgFullname = $syslinuxcfg.FullName
                cfg = $syslinuxcfg.FullName -replace "$Mountpoint"
            })
        }
        return $bootloaders
    }

    End {
        Write-LeaveFunction
    }
}

function Set-OSBootLoaderDefaultBoot {
    [CmdletBinding()]
    [OutputType([void])]
    Param (
        # Label of the bootloader to boot
        [Alias('Label', 'PartitionLabel')]
        [string]$BootLoaderLabel,

        # Type of the bootloader
        [Alias('BL', 'BootLoader')]
        [ValidateSet('auto', 'bcd', 'grub2', 'refind', 'syslinux')]
        [string]$BootLoaderType = 'auto',

        [string]$Mountpoint
    )
    Begin {
        Write-EnterFunction
        $bootloaders = Find-OSBootloaders -MountPoint $Mountpoint
    }

    Process {
        switch ($BootLaderType) {
            'all' {
                foreach ($bl in $bootloaders) {
                    Set-OSBootLoaderDefaultBoot -Mountpoint $Mountpoint -OSBootloaderType $bl.name -OSBootloaderLabel $BootLoaderLabel
                }
            }
            'bcd' {
                $bl = $bootloaders | Where-Object { $_.name -eq $BootLoaderType }
                Set-OSBootloaderBCDDefaultBoot -ConfigFile $bl.cfgFullname -Label $BootLoaderLabel
            }
            'grub2' {
                $bl = $bootloaders | Where-Object { $_.name -eq $BootLoaderType }
                Set-OSBootloaderGrub2DefaultBoot -ConfigFile $bl.cfgFullname -Label $BootLoaderLabel
            }
            'refind' {
                $bl = $bootloaders | Where-Object { $_.name -eq $BootLoaderType }
                Set-OSBootloaderRefindDefaultBoot -ConfigFile $bl.cfgFullname -Label $BootLoaderLabel
            }
            'syslinux' {
                $bl = $bootloaders | Where-Object { $_.name -eq $BootLoaderType }
                Set-OSBootloaderSyslinuxDefaultBoot -ConfigFile $bl.cfgFullname -Label $BootLoaderLabel
            }
            default {
                Write-Error "Bootloader $BootLaderType is not yet supported."
            }
        }
    }

    End {
        Write-LeaveFunction
    }
}