Backup-BitlockerKeys.psm1

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
function BackupBitlockerKey($letter, $destinationPath)
{
    $volumeDetails=Get-BitLockerVolume -MountPoint $letter
    if ($(Test-Path $destinationPath) -eq $false)
    {
        New-Item $destinationPath -ItemType Directory | Out-Null
    }
    $fileDestination=Join-Path $destinationPath "$letter.bitlocker"
    $volumeDetails.KeyProtector | Out-File $fileDestination
    Write-Verbose "Recovery key saved in $fileDestination"
}


function Backup-BitlockerKeys {
    [cmdletbinding()]
    param ([string]$Destination, [switch]$ToDateDirectory, [string]$DateNamePrefix, [string]$DateNameSuffix,
    [switch]$ToPersonalOneDrive, [switch]$ToBusinessOneDrive)    

    if (($Destination -eq $null -or $Destination -eq "") -and ($ToPersonalOneDrive.IsPresent -eq $favoritesPath -and $ToBusinessOneDrive.IsPresent -eq $false) )
    {
        throw [System.Exception] "Destination directory is required"
    }

    if ($ToPersonalOneDrive.IsPresent)
    {
        $oneDriveDir=Get-OneDriveDirectory -Personal -JustDirectory
        $Destination=Join-Path $oneDriveDir $Destination
    }

    if ($ToBusinessOneDrive.IsPresent)
    {
        $oneDriveDir=Get-OneDriveDirectory -Business -JustDirectory
        $Destination=Join-Path $oneDriveDir $Destination
    }

    if ($ToDateDirectory.IsPresent)
    {
        [string]$dateName=Get-DateName -Prefix $DateNamePrefix -Suffix $DateNameSuffix
        $Destination=Join-Path $Destination $dateName
        Write-Verbose "Destination directory with date directory: $destinationDirectory"
    }
    

    Write-Verbose "Destination directory: $Destination"

    $volumes=Get-BitLockerVolume
    foreach($volume in $volumes)
    {
        if ($volume.ProtectionStatus -eq "On")
        {
            $letter=$volume.MountPoint.Trim(':')
            Write-Verbose "Perform backup bitlocker keys for volume $letter"
            BackupBitlockerKey $letter $Destination
        }
    }
}

Export-ModuleMember Backup-BitlockerKeys