AJJaxNet

BitLocker: Deploy and Backup Key to Syncro

Sep 23rd, 2021 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [cmdletbinding()]
  2.   param(
  3.     [Parameter()]
  4.     [ValidateNotNullOrEmpty()]
  5.     [string] $OSDrive = $env:SystemDrive
  6.   )
  7.  
  8. Import-Module $env:SyncroModule
  9.  
  10. $BitlockerEnabled = $false
  11.  
  12. function Get-Bitlocker-RecoveryPasswords {
  13.     param(
  14.         [Parameter(Mandatory=$true)]
  15.         [Boolean]
  16.         $SaveToSyncro
  17.     )
  18.    
  19.     $textOutput = ""
  20.    
  21.     # Identify all the Bitlocker volumes.
  22.     $BitlockerVolumes = Get-BitLockerVolume
  23.    
  24.     # For each volume, get the RecoveryPassword and display it.
  25.     $BitlockerVolumes |
  26.         ForEach-Object {
  27.             $MountPoint = $_.MountPoint
  28.             $RecoveryKey = [string]($_.KeyProtector).RecoveryPassword
  29.             if ($RecoveryKey.Length -gt 5) {
  30.                 Write-Output ("The drive $MountPoint has a recovery key $RecoveryKey.")
  31.                 $textOutput += "$MountPoint/ $RecoveryKey"
  32.                 $textOutput += "`r`n"
  33.             }        
  34.         }
  35.        
  36.     if ($saveToSyncro -eq $true) {
  37.         Set-Asset-Field -Name "Bitlocker Backup Key" -Value $textOutput
  38.     }
  39. }
  40.  
  41. try {
  42.   $ErrorActionPreference = "stop"
  43.  
  44.   Write-Host "Enabling BitLocker with TPM."
  45.    
  46.   # Enable Bitlocker using TPM
  47.   Enable-BitLocker -MountPoint $OSDrive -UsedSpaceOnly -TpmProtector -ErrorAction Continue
  48.  
  49.   # Only add RecoveryPassword if none is already defined
  50.   if (((Get-BitLockerVolume -MountPoint $OSDrive).KeyProtector|?{$_.KeyProtectorType -eq 'RecoveryPassword'} | measure).Count -eq 0) {
  51.     Write-Host "Adding a recovery password..."
  52.     Enable-BitLocker -MountPoint $OSDrive -UsedSpaceOnly -RecoveryPasswordProtector
  53.     $BitlockerEnabled = $true
  54.   }
  55.   else {
  56.     Write-Host "A recovery password is already defined, skipping creation."
  57.   }
  58.  
  59.   Start-Sleep -Seconds 30
  60.  
  61.   #$key = (Get-BitLockerVolume -MountPoint $OSDrive).KeyProtector|?{$_.KeyProtectorType -eq 'RecoveryPassword'}
  62.   #$keyPass = [String]$key.RecoveryPassword
  63.   #Write-Host "Recovery key: $keyPass"
  64.  
  65.   Get-Bitlocker-RecoveryPasswords -SaveToSyncro $true
  66. }
  67.  
  68. catch {
  69.   Write-Host "Error while setting up Bitlocker, make sure that you are running the cmdlet as an admin: $_"
  70.   Create-Syncro-Ticket -Subject "BitLocker Deployment Issue" -IssueType "PC Issue" -Status "New"
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment