我在变量 $computers 中有来自 OU 的计算机列表。
到目前为止,我正在使用以下代码,并且每台计算机都收到“获取“在此处插入计算机名称”的恢复信息时出错:未找到目录对象”。 即使它成功地从对象中提取了可分辨名称,所以我不确定为什么它会说“找不到目录对象”。
foreach ($computer in $computers) {
Write-Output "Processing computer: $computer"
try {
$computerObject = Get-ADComputer -Identity $computer -Server "yourdomain.com"
$computerDN = $computerObject.DistinguishedName
Write-Output "Computer DistinguishedName: $computerDN"
$recoveryKeys = Get-ADObject -Filter { objectClass -eq "msFVE-RecoveryInformation" } -SearchBase "CN=Microsoft BitLocker Drive Encryption Recovery Information,$computerDN" -Properties WhenCreated, msFVE-RecoveryPassword
if ($recoveryKeys) {
$latestKey = $recoveryKeys | Sort-Object WhenCreated -Descending | Select-Object -First 1
if ($latestKey.WhenCreated -lt $date) {
Write-Output "Computer: $computer"
Write-Output "Recovery Key Created: $($latestKey.WhenCreated)"
Write-Output "Recovery Key: $($latestKey.'msFVE-RecoveryPassword')"
} else {
Write-Output "No recovery keys newer than $date found for $computer"
}
} else {
Write-Output "No recovery keys found for $computer"
}
} catch {
Write-Output ("Error fetching recovery information for ${computer}: " + $_.Exception.Message)
}
}