简体   繁体   English

Powershell Where-Object 和 BitLocker

[英]Powershell Where-Object and BitLocker

I've got a powershell script where the end goal is to just print the BitLocker Recovery Key to a text file, but I want to use the Where-Object to get only the RecoveryPassword, as opposed to the TPM information.我有一个 powershell 脚本,最终目标是将 BitLocker 恢复密钥打印到文本文件,但我想使用 Where-Object 仅获取 RecoveryPassword,而不是 TPM 信息。

If I run the last line, it works fine.如果我运行最后一行,它工作正常。 If I try to run the command passed to a variable, I get an error stating ".KeyProtectorType: The term '.KeyProtectorType' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."如果我尝试运行传递给变量的命令,我会收到一条错误消息,指出“.KeyProtectorType: The term '.KeyProtectorType' is not Recognized as the name of a cmdlet, function, script file, or operable program. 检查拼写名称,或者如果包含路径,请验证路径是否正确,然后重试。”

I think it's some issue with directory changing because it's being passed in a variable, but I do not know how to work around it.我认为这是目录更改的一些问题,因为它是在变量中传递的,但我不知道如何解决它。

$location = "$env:UserProfile\Desktop\BitLockerRecoveryKey.txt"

$args = "Get-BitLockerVolume -MountPoint C:| Select-Object -ExpandProperty KeyProtector | Where-Object{$_.KeyProtectorType -eq 'RecoveryPassword'}| Select-Object KeyProtectorID,RecoveryPassword >"  + $location
# Get the ID and security principal of the current user account

iex $args
Get-BitLockerVolume -MountPoint C:| Select-Object -ExpandProperty KeyProtector | Where-Object{$_.KeyProtectorType -eq 'RecoveryPassword'}| Select-Object KeyProtectorID,RecoveryPassword


In your case, you can use the value of variable $location directly to pass the output file path to > , the redirection operator在您的情况下,您可以直接使用变量$location的值将 output 文件路径传递给> 重定向运算符

$location = "$env:UserProfile\Desktop\BitLockerRecoveryKey.txt"

Get-BitLockerVolume -MountPoint C:|
  Select-Object -ExpandProperty KeyProtector |
  Where-Object KeyProtectorType -eq RecoveryPassword | 
  Select-Object KeyProtectorID, RecoveryPassword > $location

Note that I've used simplified syntax in the Where-Object call above.请注意,我在上面的Where-Object调用中使用了简化的语法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM