Update enable-bitlocker.ps1
This commit is contained in:
1 parent
9343b687a7
commit
85e76f2122
1 file changed
+51
-44
+51
-44
@@ -1,38 +1,43 @@
|
|||||||
# Check if script is running with admin privileges
|
# Script block that contains all the functionality
|
||||||
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
$scriptBlock = {
|
||||||
Write-Error "Please run this script as Administrator"
|
# Check if script is running with admin privileges
|
||||||
exit 1
|
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
||||||
}
|
Write-Error "Please run this script as Administrator"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
# Check if BitLocker is already enabled on C:
|
# Check if BitLocker is already enabled on C:
|
||||||
$bitlockerStatus = Get-BitLockerVolume -MountPoint "C:"
|
$bitlockerStatus = Get-BitLockerVolume -MountPoint "C:"
|
||||||
if ($bitlockerStatus.ProtectionStatus -eq "On") {
|
if ($bitlockerStatus.ProtectionStatus -eq "On") {
|
||||||
Write-Warning "BitLocker is already enabled on C: drive"
|
Write-Warning "BitLocker is already enabled on C: drive"
|
||||||
exit 0
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
# Prompt user to select save location for recovery key
|
# Add Windows Forms assembly
|
||||||
$saveDialog = New-Object System.Windows.Forms.SaveFileDialog
|
Add-Type -AssemblyName System.Windows.Forms
|
||||||
$saveDialog.Filter = "Text Files (*.txt)|*.txt"
|
|
||||||
$saveDialog.Title = "Select location to save BitLocker recovery key"
|
|
||||||
$saveDialog.FileName = "BitLocker-$(Get-Date -Format 'yyyy-MM-dd').txt"
|
|
||||||
|
|
||||||
if ($saveDialog.ShowDialog() -eq 'OK') {
|
# Prompt user to select save location for recovery key
|
||||||
$keyPath = $saveDialog.FileName
|
$saveDialog = [System.Windows.Forms.SaveFileDialog]::new()
|
||||||
|
$saveDialog.Filter = "Text Files (*.txt)|*.txt"
|
||||||
try {
|
$saveDialog.Title = "Select location to save BitLocker recovery key"
|
||||||
Write-Host "Enabling BitLocker on C: drive..." -ForegroundColor Yellow
|
$saveDialog.FileName = "BitLocker-RecoveryKey-$env:COMPUTERNAME-$(Get-Date -Format 'yyyy-MM-dd').txt"
|
||||||
|
|
||||||
|
if ($saveDialog.ShowDialog() -eq 'OK') {
|
||||||
|
$keyPath = $saveDialog.FileName
|
||||||
|
|
||||||
# Enable BitLocker with TPM
|
try {
|
||||||
Enable-BitLocker -MountPoint "C:" -TpmProtector -SkipHardwareTest -ErrorAction Stop
|
Write-Host "Enabling BitLocker on C: drive..." -ForegroundColor Yellow
|
||||||
|
|
||||||
# Get recovery key and save to file
|
# Enable BitLocker with TPM
|
||||||
$recoveryKey = (Get-BitLockerVolume -MountPoint "C:").KeyProtector |
|
Enable-BitLocker -MountPoint "C:" -TpmProtector -SkipHardwareTest -ErrorAction Stop
|
||||||
Where-Object { $_.KeyProtectorType -eq 'RecoveryPassword' } |
|
|
||||||
Select-Object -ExpandProperty RecoveryPassword
|
# Get recovery key and save to file
|
||||||
|
$recoveryKey = (Get-BitLockerVolume -MountPoint "C:").KeyProtector |
|
||||||
|
Where-Object { $_.KeyProtectorType -eq 'RecoveryPassword' } |
|
||||||
|
Select-Object -ExpandProperty RecoveryPassword
|
||||||
|
|
||||||
# Save key to file
|
# Save key to file
|
||||||
$content = @"
|
$content = @"
|
||||||
BitLocker Recovery Key for C: drive
|
BitLocker Recovery Key for C: drive
|
||||||
Generated on: $(Get-Date)
|
Generated on: $(Get-Date)
|
||||||
Computer Name: $env:COMPUTERNAME
|
Computer Name: $env:COMPUTERNAME
|
||||||
@@ -41,18 +46,20 @@ Recovery Key: $recoveryKey
|
|||||||
|
|
||||||
Please keep this file in a safe place. You will need this key to recover your data if BitLocker enters recovery mode.
|
Please keep this file in a safe place. You will need this key to recover your data if BitLocker enters recovery mode.
|
||||||
"@
|
"@
|
||||||
|
|
||||||
$content | Out-File -FilePath $keyPath -Force
|
$content | Out-File -FilePath $keyPath -Force
|
||||||
|
|
||||||
Write-Host "`nBitLocker has been successfully enabled on C: drive" -ForegroundColor Green
|
Write-Host "`nBitLocker has been successfully enabled on C: drive" -ForegroundColor Green
|
||||||
Write-Host "Recovery key has been saved to: $keyPath" -ForegroundColor Green
|
Write-Host "Recovery key has been saved to: $keyPath" -ForegroundColor Green
|
||||||
Write-Host "IMPORTANT: Please store this file in a safe location!" -ForegroundColor Yellow
|
Write-Host "IMPORTANT: Please store this file in a safe location!" -ForegroundColor Yellow
|
||||||
|
|
||||||
} catch {
|
} catch {
|
||||||
Write-Error "Failed to enable BitLocker: $_"
|
Write-Error "Failed to enable BitLocker: $_"
|
||||||
exit 1
|
}
|
||||||
|
} else {
|
||||||
|
Write-Warning "Operation cancelled by user"
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
Write-Warning "Operation cancelled by user"
|
|
||||||
exit 0
|
# Execute the script block
|
||||||
}
|
& $scriptBlock
|
||||||
Reference in new issue
Block a user