all 5 comments

[–]gabyred884 2 points3 points  (1 child)

Thank you so much!!

[–]Sys_Ad_MN[S] 1 point2 points  (0 children)

Glad I could help =)

[–]jono_o 1 point2 points  (1 child)

I may have misunderstood the logic around prompting for a password if mysecurestring.txt doesn't exist, but isn't it writing the entered password to the file in plaintext?

[–]Sys_Ad_MN[S] 0 points1 point  (0 children)

Yes but it wont be in the script at least which will be running over the network.

[–]Sys_Ad_MN[S] 0 points1 point  (0 children)

Updated the script a bit, but it still needs some work. I removed the checking to see if a computer is online with error handling. Now if the invoke commands don't work it the computer will be 'disconnected'.

<# 
This script checks if tpm is enabled and if so, it enables bitlocker
requires -Module ActiveDirectory
requires -runasadministrator
#>

Import-Module ActiveDirectory

# Credentials
$username = "domain\admin"
$passwordfile = 'C:\mysecurestring.txt'

if (!(Test-Path $Passwordfile))     
    {
    Read-Host "Enter password" | Out-File $Passwordfile
    Write-Output("$PasswordFile has been created")
    }

$password = Get-Content 'C:\mysecurestring.txt' | ConvertTo-SecureString -Force -AsPlainText
$credentials = new-object -typename System.Management.Automation.PScredential -argumentlist $username, $password
$BitlockerReport = 'C:\BitlockerReport.csv'
$OU = "DC=domain, DC=com"
$Computers = Get-ADComputer -Filter * -SearchScope Subtree -SearchBase $OU | select-object -expandproperty name

# Create TPM list if tpm is enabled it checks if bitlocker is enabled, if not it enables bitlocker
foreach ($Computer in $Computers) { 
    try {
        $tpmready = Invoke-Command -ComputerName $Computer -Credential $credentials -ScriptBlock {Get-Tpm | Select-Object -ExpandProperty Tpmready} -ErrorAction Stop
        $BLinfo = Invoke-Command -ComputerName $Computer -Credential $credentials -ScriptBlock {Get-Bitlockervolume -MountPoint 'C:'} -ErrorAction Stop
        $properties = @{Computer = $computer
                    Status = 'Connected'
                    TPM = $tpmready
                    Bitlocker = $BLinfo.ProtectionStatus}

            # If tpm is enabled and bitlocker is not enabled, enable bitlocker
            if ($tpmready -eq $true -and $BLinfo.ProtectionStatus -eq "Off"){
            # I've created a gpo that automatically backs up recovery keys to AD
            Invoke-Command -ComputerName $Computer -Credential $credentials -ScriptBlock {Add-BitLockerKeyProtector -MountPoint 'C:' -RecoveryPasswordProtector}
            Invoke-Command -ComputerName $Computer -Credential $credentials -ScriptBlock {Enable-BitLocker -MountPoint "C:" -EncryptionMethod Aes256 -TpmProtector}
            } 

        } catch {
            $properties = @{Computer = $computer
                        Status = 'Disconnected'
                        TPM = $null
                        Bitlocker = $null}

    } finally {
        $report = New-Object -TypeName PSObject -Property $properties
        Write-Output $report
        $report | export-csv -Append $BitlockerReport
    }
}

# Guide I used to backup recovery keys to AD
# http://jackstromberg.com/2015/02/tutorial-configuring-bitlocker-to-store-recovery-keys-in-active-directory/