Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
5,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#PSScriptInfo
  2. .VERSION 1.6
  3. .GUID f5187e3f-ed0a-4ce1-b438-d8f421619ca3
  4. .ORIGINAL AUTHOR Jan Van Meirvenne
  5. .MODIFIED BY Sooraj Rajagopalan, Paul Huijbregts & Pieter Wigleven
  6. .COPYRIGHT
  7. .TAGS Azure Intune Bitlocker  
  8. .LICENSEURI  
  9. .PROJECTURI  
  10. .ICONURI  
  11. .EXTERNALMODULEDEPENDENCIES  
  12. .REQUIREDSCRIPTS  
  13. .EXTERNALSCRIPTDEPENDENCIES  
  14. .RELEASENOTES  
  15. #>
  16.  
  17. <#
  18.  
  19. .DESCRIPTION
  20.  Check whether BitLocker is Enabled, if not Enable Bitlocker on AAD Joined devices and store recovery info in AAD
  21. #>
  22. [cmdletbinding()]
  23.     param(
  24.         [Parameter()]
  25.         [ValidateNotNullOrEmpty()]
  26.         [string] $OSDrive = $env:SystemDrive
  27.     )
  28.     [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  29. try
  30. {
  31.         $bdeProtect = Get-BitLockerVolume $OSDrive | select -Property VolumeStatus
  32.  
  33.             if ($bdeProtect.VolumeStatus -eq "FullyDecrypted"){
  34.             # Enable Bitlocker using TPM
  35.             Enable-BitLocker -MountPoint $OSDrive  -TpmProtector -ErrorAction Continue
  36.             Enable-BitLocker -MountPoint $OSDrive  -RecoveryPasswordProtector
  37.             }
  38.  
  39.             #Check if we can use BackupToAAD-BitLockerKeyProtector commandlet
  40.             $cmdName = "BackupToAAD-BitLockerKeyProtector"
  41.             if (Get-Command $cmdName -ErrorAction SilentlyContinue){
  42.                 #BackupToAAD-BitLockerKeyProtector commandlet exists
  43.                 $BLV = Get-BitLockerVolume -MountPoint $OSDrive | select *
  44.                 BackupToAAD-BitLockerKeyProtector -MountPoint $OSDrive -KeyProtectorId $BLV.KeyProtector[1].KeyProtectorId
  45.             } else {
  46.             # BackupToAAD-BitLockerKeyProtector commandlet not available, using other mechanisme  
  47.             # Get the AAD Machine Certificate
  48.             $cert = dir Cert:\LocalMachine\My\ | where { $_.Issuer -match "CN=MS-Organization-Access" }
  49.  
  50.             # Obtain the AAD Device ID from the certificate
  51.             $id = $cert.Subject.Replace("CN=","")
  52.  
  53.             # Get the tenant name from the registry
  54.             $tenant = (Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\CloudDomainJoin\JoinInfo\$($id)).UserEmail.Split('@')[1]
  55.  
  56.             # Generate the body to send to AAD containing the recovery information
  57.             # Get the BitLocker key information from WMI
  58.                 (Get-BitLockerVolume -MountPoint $OSDrive).KeyProtector|?{$_.KeyProtectorType -eq 'RecoveryPassword'}|%{
  59.                 $key = $_
  60.                 write-verbose "kid : $($key.KeyProtectorId) key: $($key.RecoveryPassword)"
  61.                 $body = "{""key"":""$($key.RecoveryPassword)"",""kid"":""$($key.KeyProtectorId.replace('{','').Replace('}',''))"",""vol"":""OSV""}"
  62.            
  63.             # Create the URL to post the data to based on the tenant and device information
  64.                 $url = "https://enterpriseregistration.windows.net/manage/$tenant/device/$($id)?api-version=1.0"
  65.            
  66.             # Post the data to the URL and sign it with the AAD Machine Certificate
  67.                 $req = Invoke-WebRequest -Uri $url -Body $body -UseBasicParsing -Method Post -UseDefaultCredentials -Certificate $cert
  68.                 $req.RawContent
  69.             }
  70.             }
  71.             #>
  72.    
  73.     } catch {
  74.             write-error "Error while setting up AAD Bitlocker, make sure that you are AAD joined and are running the cmdlet as an admin: $_"
  75.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement