I'm wanting to get the BitLocker recovery password via powershell by providing the recovery key ID. I know this is doable via the Active Directory Users and Computers application, which is essentially what I'm trying to reproduce.
My process is currently as follows:
- Prompt the user for the recovery key id
- Get all AD objects where the ObjectClass is msFVE-RecoveryInformation
- Filter those results by the DistinguishedName which should allow me to get the individual record I'm wanting.
- Write out the resulting BitLocker recovery password
The problem I run into is that when using a variable in my Where-Object clause, I get no results. If I hard code in the recovery key ID, it works just fine.
Here's the code I have so far:
$key = (read-host -Prompt "Enter starting portion of recovery key ID (8 Digits)").ToUpper()
$recoveryInformation = Get-ADObject -Filter 'ObjectClass -eq "msFVE-RecoveryInformation"' | Where-Object {$_.DistinguishedName -like "*$key*"}
echo $recoveryInformation
I've tried doing this a couple of different ways and they all ended with the same result where a hard coded value would work and a variable would not. This leads me to believe it is something with the way I am getting the user input, but I've hit a wall. Any help would be greatly appreciated.
End Result
In the end, the problem with my code is that I was using where-object instead of where. Once I made that change, everything worked as I had intended it.
The examples provided by postanote give a much better output and are definitely more robust. The last example is the best one to give the end result I was looking for.