Update enable-bitlocker.ps1
This commit is contained in:
1 parent
1733ba61ce
commit
a6a6d673a8
1 file changed
+57
-57
+57
-57
@@ -1,58 +1,58 @@
|
||||
# Check if script is running with admin privileges
|
||||
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
||||
Write-Error "Please run this script as Administrator"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if BitLocker is already enabled on C:
|
||||
$bitlockerStatus = Get-BitLockerVolume -MountPoint "C:"
|
||||
if ($bitlockerStatus.ProtectionStatus -eq "On") {
|
||||
Write-Warning "BitLocker is already enabled on C: drive"
|
||||
exit 0
|
||||
}
|
||||
|
||||
# Prompt user to select save location for recovery key
|
||||
$saveDialog = New-Object System.Windows.Forms.SaveFileDialog
|
||||
$saveDialog.Filter = "Text Files (*.txt)|*.txt"
|
||||
$saveDialog.Title = "Select location to save BitLocker recovery key"
|
||||
$saveDialog.FileName = "BitLocker-RecoveryKey-$(Get-Date -Format 'yyyy-MM-dd').txt"
|
||||
|
||||
if ($saveDialog.ShowDialog() -eq 'OK') {
|
||||
$keyPath = $saveDialog.FileName
|
||||
|
||||
try {
|
||||
Write-Host "Enabling BitLocker on C: drive..." -ForegroundColor Yellow
|
||||
|
||||
# Enable BitLocker with TPM
|
||||
Enable-BitLocker -MountPoint "C:" -TpmProtector -SkipHardwareTest -ErrorAction Stop
|
||||
|
||||
# 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
|
||||
$content = @"
|
||||
BitLocker Recovery Key for C: drive
|
||||
Generated on: $(Get-Date)
|
||||
Computer Name: $env:COMPUTERNAME
|
||||
|
||||
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.
|
||||
"@
|
||||
|
||||
$content | Out-File -FilePath $keyPath -Force
|
||||
|
||||
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 "IMPORTANT: Please store this file in a safe location!" -ForegroundColor Yellow
|
||||
|
||||
} catch {
|
||||
Write-Error "Failed to enable BitLocker: $_"
|
||||
exit 1
|
||||
}
|
||||
} else {
|
||||
Write-Warning "Operation cancelled by user"
|
||||
exit 0
|
||||
# Check if script is running with admin privileges
|
||||
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
||||
Write-Error "Please run this script as Administrator"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if BitLocker is already enabled on C:
|
||||
$bitlockerStatus = Get-BitLockerVolume -MountPoint "C:"
|
||||
if ($bitlockerStatus.ProtectionStatus -eq "On") {
|
||||
Write-Warning "BitLocker is already enabled on C: drive"
|
||||
exit 0
|
||||
}
|
||||
|
||||
# Prompt user to select save location for recovery key
|
||||
$saveDialog = New-Object System.Windows.Forms.SaveFileDialog
|
||||
$saveDialog.Filter = "Text Files (*.txt)|*.txt"
|
||||
$saveDialog.Title = "Select location to save BitLocker recovery key"
|
||||
$saveDialog.FileName = "BitLocker-$COMPUTERNAME-$(Get-Date -Format 'yyyy-MM-dd').txt"
|
||||
|
||||
if ($saveDialog.ShowDialog() -eq 'OK') {
|
||||
$keyPath = $saveDialog.FileName
|
||||
|
||||
try {
|
||||
Write-Host "Enabling BitLocker on C: drive..." -ForegroundColor Yellow
|
||||
|
||||
# Enable BitLocker with TPM
|
||||
Enable-BitLocker -MountPoint "C:" -TpmProtector -SkipHardwareTest -ErrorAction Stop
|
||||
|
||||
# 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
|
||||
$content = @"
|
||||
BitLocker Recovery Key for C: drive
|
||||
Generated on: $(Get-Date)
|
||||
Computer Name: $env:COMPUTERNAME
|
||||
|
||||
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.
|
||||
"@
|
||||
|
||||
$content | Out-File -FilePath $keyPath -Force
|
||||
|
||||
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 "IMPORTANT: Please store this file in a safe location!" -ForegroundColor Yellow
|
||||
|
||||
} catch {
|
||||
Write-Error "Failed to enable BitLocker: $_"
|
||||
exit 1
|
||||
}
|
||||
} else {
|
||||
Write-Warning "Operation cancelled by user"
|
||||
exit 0
|
||||
}
|
||||
Reference in new issue
Block a user