【问题标题】:Issue with Get-ADObject and Where-Object Using a VariableGet-ADObject 和 Where-Object 使用变量的问题
【发布时间】:2018-10-28 20:28:43
【问题描述】:

我想通过提供恢复密钥 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 提供的示例提供了更好的输出,并且肯定更健壮。最后一个示例是给出我正在寻找的最终结果的最佳示例。

【问题讨论】:

    标签: powershell powershell-5.0 bitlocker


    【解决方案1】:

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

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

    Get BitLocker Recovery Information from AD Using PowerShell

    # 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
    }
    

    【讨论】:

    • 首先,非常感谢您的详尽。你的例子效果很好。在更仔细地查看示例时,我还能够在我的代码中找到错误。而不是使用 where-object 我应该只使用 where。一旦我这样做了,我就能让我的代码按照我的预期工作。不过,您的示例提供了更强大的输出,因此我将最终将您的示例与我的示例结合起来。再次感谢您!
    • 不用担心,很高兴他们为您提供所需的东西。
    猜你喜欢
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2012-01-30
    相关资源
    最近更新 更多