Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- This script encrypt local disk and send encryption key to the specified mail address
- .DESCRIPTION
- Create a file containing timestamp, hostname, hostid and a random bitlocker password
- Send this file to the user mail address and ISD sympa list then start encrypting the disk
- Abort if necessary
- #>
- #Function to generate a Bitlocker key
- Function recoveryKeyGen
- {
- $recoveryKey = ""
- for($i=0; $i -lt 8; $i++)
- {
- $completed = $null
- DO
- {
- $recoveryKeyPart = (Get-Random -Minimum 100000 -Maximum 720895)
- if($recoveryKeyPart %11 -eq 0)
- {
- $recoveryKey = $recoveryKey + $recoveryKeyPart
- $completed = $true
- }
- } While (-not $completed)
- if ($i -ne 7)
- {
- $recoveryKey = $recoveryKey + "-"
- }
- }
- $recoveryKey
- }
- #Prepare file path
- if(-Not (Test-Path C:\Temp))
- {
- New-Item -ItemType Directory -Force -Path C:\Temp
- }
- $recoveryKeyPath = "C:\Temp\" + $env:COMPUTERNAME + "_" + $env:USERNAME + "_recoveryKey.txt"
- if(Test-Path $recoveryKeyPath)
- {
- Remove-Item $recoveryKeyPath
- }
- #Get mail address
- Add-Type -AssemblyName System.Windows.Forms
- Add-Type -AssemblyName System.Drawing
- $form = New-Object System.Windows.Forms.Form
- $form.Text = 'Data Entry Form'
- $form.Size = New-Object System.Drawing.Size(300,200)
- $form.StartPosition = 'CenterScreen'
- $okButton = New-Object System.Windows.Forms.Button
- $okButton.Location = New-Object System.Drawing.Point(75,120)
- $okButton.Size = New-Object System.Drawing.Size(75,23)
- $okButton.Text = 'OK'
- $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
- $form.AcceptButton = $okButton
- $form.Controls.Add($okButton)
- $cancelButton = New-Object System.Windows.Forms.Button
- $cancelButton.Location = New-Object System.Drawing.Point(150,120)
- $cancelButton.Size = New-Object System.Drawing.Size(75,23)
- $cancelButton.Text = 'Cancel'
- $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
- $form.CancelButton = $cancelButton
- $form.Controls.Add($cancelButton)
- $label = New-Object System.Windows.Forms.Label
- $label.Location = New-Object System.Drawing.Point(10,20)
- $label.Size = New-Object System.Drawing.Size(280,20)
- $label.Text = 'Please enter your professional mail address :'
- $form.Controls.Add($label)
- $textBox = New-Object System.Windows.Forms.TextBox
- $textBox.Location = New-Object System.Drawing.Point(10,40)
- $textBox.Size = New-Object System.Drawing.Size(260,20)
- $form.Controls.Add($textBox)
- $form.Topmost = $true
- $form.Add_Shown({$textBox.Select()})
- $result = $form.ShowDialog()
- if ($result -eq [System.Windows.Forms.DialogResult]::OK)
- {
- $userMail = $textBox.Text
- $userMail
- }
- else
- {
- Write-Host Mail address is mandatory save encryption key. Aborting.
- exit
- }
- $timeStamp = "[{0:dd/MM/yy} {0:HH:mm:ss}]" -f (Get-Date)
- Add-Content $recoveryKeyPath $timeStamp
- $computerName = "COMPUTERNAME : " + $env:COMPUTERNAME
- Add-Content $recoveryKeyPath $computerName
- #Get IP
- $ipv4 = "IP : "
- $ipv4B = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DefaultIPGateway -ne $null}).IPAddress | select-object -first 1
- $ipv4 = $ipv4 + $ipv4B
- Add-Content $recoveryKeyPath $ipv4
- #Get hostID
- $macAddress = "HOSTID : "
- $computerSystem = (Get-WmiObject -Class:Win32_ComputerSystem)
- if ( $computerSystem.Manufacturer -like "Hewlett*" )
- {
- $macAddressB = Get-WmiObject win32_networkadapterconfiguration | select description, macaddress | ? description -like "*Network Connection" | select macaddress -ExpandProperty macaddress
- }
- elseif( $computerSystem.Manufacturer -like "Dell*" )
- {
- $macAddressB = Get-WmiObject win32_networkadapterconfiguration | select description, macaddress | ? description -like "Realtek*" | select macaddress -ExpandProperty macaddress
- }
- $macAddress = $macAddress + $macAddressB
- Add-Content $recoveryKeyPath $macAddress
- #Create recovery key
- $recoveryKey = "RECOVERYKEY : "
- $recoveryKeyB = recoveryKeyGen
- $recoveryKey = $recoveryKey + $recoveryKeyB
- Add-Content $recoveryKeyPath $recoveryKey
- #Send encryption info via mail
- $smtpServer = "smtp.domain.fr"
- $smtpFromA = ($userMail | Out-String)
- $smtpFromB = "windows-escrow@domain.fr"
- $smtpToA = "windows-escrow@domain.fr"
- $smtpToB = ($userMail | Out-String)
- $messageSubject = "$env:COMPUTERNAME BitLocker Recovery Password"
- $messageBody = $timeStamp + "`n" + $computerName + "`n" + $ipv4 + "`n" + $macAddress + "`n" + $recoveryKey + "`n"
- $smtpA = New-Object Net.Mail.SmtpClient($smtpServer)
- $smtpB = New-Object Net.Mail.SmtpClient($smtpServer)
- $smtpA.Send($smtpFromA,$smtpToA,$messagesubject,$messagebody)
- $smtpB.Send($smtpFromB,$smtpToB,$messagesubject,$messagebody)
- Write-Host "`n" $timeStamp "`n" $computerName "`n" $ipv4 "`n" $macAddress "`n" $recoveryKey "`n`n"
- Write-Host "Encryption informations has been sent to the specified addresse, starting the disk encryption"
- #Start encryption
- manage-bde -protectors -add c: -RecoveryPassword $recoveryKeyB
- manage-bde -on c:
- Restart-Computer -Force
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement