This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathBitLocker.psm1
219 lines (167 loc) · 8.8 KB
/
BitLocker.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#requires -RunAsAdministrator
#requires -version 3
Set-StrictMode -Version 3
Function Get-BitLockerStatus() {
<#
.SYNOPSIS
Starts the BitLocker encryption process for a drive.
.DESCRIPTION
Starts the BitLocker encryption process for a drive.
.PARAMETER Drive
The drive letter, including : character, to enable BitLocker on.
.EXAMPLE
Get-BitLockerStatus -Drive $env:SYSTEMDRIVE
#>
[CmdletBinding()]
#[OutputType([FveApi.FVE_STATUS])] # throws an error since the type isn't added until the function has executed
Param(
[Parameter(Mandatory=$true, HelpMessage='The drive letter, including : character, to get the BitLocker status for')]
[ValidateNotNullOrEmpty()]
[ValidatePattern('^[A-Z]:$')]
[string]$Drive
)
Begin {
$type = @'
using System.Runtime.InteropServices;
using System;
namespace FveApi {
[StructLayout(LayoutKind.Sequential)]
public struct FVE_STATUS {
public uint Size;
public uint Version;
public uint Flags;
public double ConversionPercent;
public long ConversionStatus;
}
public class NativeMethods {
[DllImport("fveapi.dll", CharSet=CharSet.Unicode)]
public static extern int FveGetStatusW(String volume, ref FVE_STATUS status);
}
}
'@
Add-Type $type
}
Process {
$bitlockerDrives = [System.IO.DriveInfo[]]@([System.IO.DriveInfo]::GetDrives()| Where-Object { $_.DriveType -eq [System.IO.DriveType]::Fixed -or $_.DriveType -eq [System.IO.DriveType]::Removable })
if("$Drive\" -in @($bitlockerDrives | ForEach-Object { $_.Name })) {
throw "Cannot get BitLocker status for $Drive"
}
[FveApi.FVE_STATUS]$status = New-Object FveApi.FVE_STATUS
$status.Size = [System.Runtime.InteropServices.Marshal]::SizeOf($status)
$status.Version = 1;
$value = [FveApi.NativeMethods]::FveGetStatusW("\\.\$Drive", [ref] $status)
if(0 -ne $value) {
throw ('Retrieving BitLocker status failed with error 0x{0:X8}' -f $value)
}
return $status
}
}
Function Start-BitLockerEncryption() {
<#
.SYNOPSIS
Starts the BitLocker encryption process for a drive.
.DESCRIPTION
Starts the BitLocker encryption process for a drive.
.PARAMETER Drive
The drive letter, including : character, to enable BitLocker on.
.PARAMETER UsePin
Specifies to use a PIN along with a TPM.
.PARAMETER Pin
Specifies the PIN rather than being prompted for it.
.PARAMETER RecoveryPath
The path of a folder to store recovery password information.
.PARAMETER Restart
Specifies to restart the system, if needed, so the BitLocker encryption process can start.
.EXAMPLE
Start-BitLockerEncryption -Drive $env:SYSTEMDRIVE -RecoveryPath ($env:USERPROFILE,'Desktop' -join '\')
.EXAMPLE
Start-BitLockerEncryption -Drive $env:SYSTEMDRIVE -RecoveryPath ($env:USERPROFILE,'Desktop' -join '\') -UsePin
.EXAMPLE
Start-BitLockerEncryption -Drive $env:SYSTEMDRIVE -RecoveryPath ($env:USERPROFILE,'Desktop' -join '\') -UsePin -Pin ('12345678' | ConvertTo-SecureString -AsPlainText -Force)
.EXAMPLE
Start-BitLockerEncryption -Drive $env:SYSTEMDRIVE -RecoveryPath ($env:USERPROFILE,'Desktop' -join '\') -UsePin -Pin ('12345678' | ConvertTo-SecureString -AsPlainText -Force) -UseActiveDirectory -Restart
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWMICmdlet', '', Scope='Function')]
[CmdletBinding()]
[OutputType([System.Version])]
Param(
[Parameter(Mandatory=$true, HelpMessage='The drive letter, including : character, to enable BitLocker on')]
[ValidateNotNullOrEmpty()]
[ValidatePattern('^[A-Z]:$')]
[string]$Drive,
[Parameter(Mandatory=$false, HelpMessage='Specifies to use a PIN along with a TPM')]
[switch]$UsePin,
[Parameter(Mandatory=$false, HelpMessage='Specifies the PIN rather than being prompted for it')]
[ValidateNotNullOrEmpty()]
[System.Security.SecureString]$Pin,
[Parameter(Mandatory=$false, HelpMessage='The path of a folder to store recovery password information')]
[ValidateNotNullOrEmpty()]
[System.IO.DirectoryInfo]$RecoveryPath,
[Parameter(Mandatory=$false, HelpMessage='Specifies to restart the system so the BitLocker encryption process can start')]
[switch]$Restart
)
$bitlockerDrives = [System.IO.DriveInfo[]]@([System.IO.DriveInfo]::GetDrives()| Where-Object { $_.DriveType -eq [System.IO.DriveType]::Fixed -or $_.DriveType -eq [System.IO.DriveType]::Removable })
if("$Drive\" -in @($bitlockerDrives | ForEach-Object { $_.Name })) {
throw "$Drive cannot be encrypted by BitLocker"
}
$tpm = Get-WmiObject -Class 'Win32_Tpm' -Namespace 'root\CIMV2\Security\MicrosoftTpm'
#if(-not($tpm.IsReady().IsReady)) {
# $readyBitmask = $tpm.IsReadyInformation().Information
# $message = 'TPM is not ready for use by BitLocker. TPM must be provisioned. ReadyInformation bitmask: 0x{0:X8} See https://msdn.microsoft.com/en-us/library/windows/desktop/jj660284(v=vs.85).aspx for more information.' -f $readyBitmask
# throw $message
#}
if ($RecoveryPath -ne $null) {
if (-not(Test-Path -Path $RecoveryPath.FullName -PathType Container)) {
throw "$RecoveryPath not found"
}
}
$isDomainJoined = (Get-WmiObject -Class 'Win32_ComputerSystem').PartOfDomain
$volume = Get-BitLockerVolume -MountPoint $Drive
$volumeDetails = Get-WmiObject -Class 'Win32_EncryptableVolume' -Namespace 'root\cimv2\Security\MicrosoftVolumeEncryption' -Filter "DriveLetter='$Drive'"
if ($volume.ProtectionStatus -eq [Microsoft.BitLocker.Structures.BitLockerVolumeProtectionStatus]::Off -and -not($volumeDetails.IsVolumeInitializedForProtection)) {
if ($UsePin) {
if ($Pin -eq $null) {
$bitlockerPin = Read-Host -AsSecureString -Prompt 'Enter BitLocker PIN'
} else {
$bitlockerPin = $Pin
}
try {
$volume = Enable-BitLocker -MountPoint $Drive -PIN $bitlockerPin -TpmAndPinProtector -ErrorAction Stop -Verbose:$false # 4>$null
} catch [System.Runtime.InteropServices.COMException] {
$errorNumber = $_.Exception.HResult
$message = $_.Exception.Message
$fix = ''
switch ($errorNumber) {
0x8031005B { $fix = "Change the 'Require additional authentication at startup' policy to configure all 4 dropdown menu options to 'Allow' OR set 1 option to 'Require' and the other 3 options to 'Do not allow'" ; break }
0x803100B5 { $fix = "Set the 'Enable use of BitLocker authentication requiring preboot keyboard input on slates' policy to Enabled"; break }
default {}
}
throw ($message,$fix -join ([System.Environment]::NewLine))
}
$bitlockerPin.Dispose()
$Pin.Dispose()
} else {
$volume = Enable-BitLocker -MountPoint $Drive -TpmProtector -ErrorAction Stop -Verbose:$false # 4>$null
}
$volume = Add-BitLockerKeyProtector -MountPoint $Drive -RecoveryPasswordProtector -ErrorAction Stop -Verbose:$false # 4>$null
}
if($VerbosePreference -ne [System.Management.Automation.ActionPreference]::SilentlyContinue) {
$volume.KeyProtector | ForEach-Object {
Write-Verbose -Message ('Protector Type: {0} Protector ID: {1} Protector Password: {2}' -f $_.KeyProtectorType,$_.KeyProtectorId,$_.RecoveryPassword)
}
}
$recoveryPasswordProtector = $volume.KeyProtector | Where-Object { $_.KeyProtectorType -eq [Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtectorType]::RecoveryPassword }
if ($RecoveryPath -ne $null) {
$recoveryFile = '{0}\{1}_bitlocker_{2}.txt' -f $RecoveryPath.FullName,$env:COMPUTERNAME,$recoveryPasswordProtector.KeyProtectorId
$volume.KeyProtector | ForEach-Object { 'Protector Type: {0} Protector ID: {1} Protector Password: {2}' -f $_.KeyProtectorType,$_.KeyProtectorId,$_.RecoveryPassword } | Out-File -FilePath $RecoveryFile -NoNewLine -Force
}
if ($UseActiveDirectory -and $isDomainJoined) {
$volume = Backup-BitLockerKeyProtector -MountPoint $Drive -KeyProtectorId $recoveryPasswordProtector.KeyProtectorId -ErrorAction Stop -Verbose:$false
# TODO: test that the recovery password was successfully written to AD
}
$status = Get-BitLockerStatus -Drive $Drive
$needsReboot = $status.Flags -band 0x2 -eq 0x2
if ($Restart -and $needsReboot) {
Restart-Computer -Force
}
}