为什么不直接使用专门用于获取此信息的内置 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
}