0

我想通过提供恢复密钥 ID 通过 powershell 获取 BitLocker 恢复密码。我知道这可以通过 Active Directory 用户和计算机应用程序来实现,这基本上就是我想要重现的内容。

我的流程目前如下:

  • 提示用户输入恢复密钥 ID
  • 获取 ObjectClass 为 msFVE-RecoveryInformation 的所有 AD 对象
  • 通过 DistinguishedName 过滤这些结果,这应该允许我获得我想要的个人记录。
  • 写出生成的 BitLocker 恢复密码

我遇到的问题是,在 Where-Object 子句中使用变量时,我没有得到任何结果。如果我在恢复密钥 ID 中硬编码,它就可以正常工作。

这是我到目前为止的代码:

$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

我尝试了几种不同的方法,它们都以相同的结果结束,其中硬编码值可以工作而变量不能。这让我相信这与我获取用户输入的方式有关,但我碰壁了。任何帮助将不胜感激。


最终结果

最后,我的代码的问题是我使用了 where-object 而不是 where。一旦我做出改变,一切都按我的预期进行。

postanote提供的示例提供了更好的输出,并且肯定更健壮。最后一个示例是给出我正在寻找的最终结果的最佳示例。

4

1 回答 1

1

为什么不直接使用专门用于获取此信息的内置 PowerShell cmdlet?

这里有一些可以直接使用或调整您的用例的东西。请参见示例 #5。

使用 PowerShell 从 AD 获取 BitLocker 恢复信息

# Example Commands

# 1. Get BitLocker recovery information for a single computer:

Get-BitLockerRecovery computer1

# 2. Get BitLocker recovery information for a list of computers:
Get-BitLockerRecovery "computer1","computer2"

# or

"computer1","computer2" | Get-BitLockerRecovery

# 3. Get BitLocker recovery information for computers in an OU:
Get-ADComputer -Filter { name -like "*" } `
  -SearchBase "OU=Sales,DC=fabrikam,DC=com" |
  Get-BitLockerRecovery

# 4. Get the BitLocker recovery information for a specific password ID:
Get-BitLockerRecovery -PasswordID B1FED823

# 5. Get BitLocker recovery information for all msFVE-RecoveryInformation objects in the current domain:
$filter = "(objectClass=msFVE-RecoveryInformation)"
Get-ADObject -LDAPFilter $filter | ForEach-Object {
  Get-ADPathname (Get-ADPathname $_.DistinguishedName `
  -Format X500Parent) -Format Leaf -ValuesOnly |
  Get-BitLockerRecovery
}

或者在测试您的变量方法时不使用用户传入的密钥字符串...

# First ask for a computername
$usrInput = Read-Host "Type in name of computer you want to retrieve the BitLocker recovery information"

# Get the computer object from Active Directory
$objComputer = Get-ADComputer $usrInput

# Find the AD object which match the computername and is of the class "msFVE-RecoveryInformation"
$objADObject = get-adobject -Filter * | Where-Object {$_.DistinguishedName -match $objComputer.Name -and $_.ObjectClass -eq "msFVE-RecoveryInformation"}

# Filter the result so you'll get only the recovery key
(($objADObject.DistinguishedName.Split(",")[0]).split("{")[1]).Substring(0,$trimming.Length-1)

或者这种方法...

$computers = get-adobject -Filter * | Where-Object {$_.ObjectClass -eq "msFVE-RecoveryInformation"}

$key = (read-host -Prompt "Enter starting portion of recovery key ID").ToUpper()
$records = $computers | where {$_.DistinguishedName -like "*$key*"}

foreach ($rec in $records) {
    $computer = get-adcomputer -identity ($records.DistinguishedName.Split(",")[1]).split("=")[1]
    $recoveryPass = Get-ADObject -Filter {objectclass -eq 'msFVE-RecoveryInformation'} -SearchBase $computer.DistinguishedName -Properties 'msFVE-RecoveryPassword'
    [pscustomobject][ordered]@{
        Computer = $computer
        'Recovery Key ID' = $rec.Name.Split("{")[1].split("}")[0]
        'Recovery Password' = $recoveryPass.'msFVE-RecoveryPassword'
    } | Format-List
}
于 2018-10-29T00:34:25.153 回答