Classes/BitlockerManager.ps1

class BitlockerManager {
    [LibraryContext]$Context

    BitlockerManager([LibraryContext]$context) {
        $this.Context = $context
    }

    # currently not in use -- also not tested!
    [void]DecryptUndesired([string]$desiredEncMethod) {
        if ($null -eq $desiredEncMethod) {
            $desiredEncMethod = "XtsAes256"
        }

        $clearedAutoUnlock = $false
        
        $this.Context.LogManager.Log("Getting internal volumes...")
        $internalVolumes = Get-Volume | Where-Object {$_.DriveType -eq 'Fixed'} | Select-Object -ExpandProperty DriveLetter    
    
        # Move C to the end, because C: cannot be decrypted if other auto unlock drives are present
        $internalVolumes = [System.Collections.ArrayList]@($internalVolumes | Where-Object { $_ -ne 'C' })
        $null = $internalVolumes.Add('C')
        
        # go though volumes
        foreach ($internalVolume in $internalVolumes) {
            $this.Context.LogManager.Log("Checking internal volume ${internalVolume}:\ ...")

            $bitlockerDrive = (Get-BitLockerVolume | Where-Object { $internalVolume -contains $_.MountPoint.TrimEnd(':') })
            $encMethod = $bitlockerDrive.EncryptionMethod
            $mountPoint = $bitlockerDrive.MountPoint
            if ($encMethod -ne $desiredEncMethod) {
                $this.Context.LogManager.Log("METHOD $encMethod DETECTED FOR INTERNAL DRIVE ${mountPoint}\!")

                # auto unlock needs to be cleared otherwise encrypt command leads to error!
                if ($clearedAutoUnlock -ne $true) {
                    $this.Context.LogManager.Log("CLEARING AUTO UNLOCKS...")
                    Clear-BitLockerAutoUnlock
                    $this.Context.LogManager.Log("AUTO UNLOCKS CLEARED...")
                    $clearedAutoUnlock = $true
                }
                
                # DECRYPTION
                Disable-Bitlocker -MountPoint $mountPoint
                $this.Context.LogManager.Log("BITLOCKER DECRYPTION FOR ${mountPoint}\ STARTED!")
                
                # Wait for decryption to finish
                $Loop = $true
                while($Loop){
                    $DecryptStatus = Get-BitlockerVolume -MountPoint $mountPoint | Select -expandproperty VolumeStatus
                    $DecryptPercentage = Get-BitlockerVolume -MountPoint $mountPoint | Select -expandproperty EncryptionPercentage
                    if($DecryptStatus -eq "FullyDecrypted") {
                        $this.Context.LogManager.Log("Volume ${mountPoint}\ has been fully decrypted!")
                        $Loop = $false
                    }
                    Else {
                        $this.Context.LogManager.Log("Waiting for decryption of ${mountPoint}\ - Current encryption percentage is $DecryptPercentage")
                        Start-Sleep -Seconds 15
                    }
                }
                
                $this.Context.LogManager.Log("BITLOCKER DECRYPTION FOR ${mountPoint}\ DONE!")
            } else {
                $this.Context.LogManager.Log("${mountPoint}\ already has desired encryption method $desiredEncMethod!")
            }
        }
    }
}