[英]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
Invoke-Expression
( iex
) should generally be avoided ;通常应避免Invoke-Expression
( iex
) ; definitely don't use it to invoke an external program or PowerShell script , even if you need to construct its arguments programmatically - the latter is best done via splatting .绝对不要使用它来调用外部程序或 PowerShell 脚本,即使您需要以编程方式构建其 arguments - 后者最好通过splatting完成。
Don't use the automatic $args
variable for custom purposes.不要将自动$args
变量用于自定义目的。
If you do need to embed $
characters verbatim in a expandable (double-quoted) string ( "..."
) , escape them as `$
.如果您确实需要在可扩展(双引号)字符串( "..."
)中逐字嵌入$
字符,请将它们转义为`$
。 If you string doesn't actually require expansion (string interpolation), use a verbatim (single-quoted) string ( '...'
)如果您的字符串实际上不需要扩展(字符串插值),请使用逐字(单引号)字符串( '...'
)
$
characters as `$
in your "..."
string is what caused your problem: $_
was expanded before iex
saw the resulting string, and - outside a pipeline - the automatic $_
variable doesn't have a value.忽略在"..."
字符串中将$
字符转义为`$
是导致问题的原因: $_
在iex
看到结果字符串之前被扩展,并且 - 在管道之外 - 自动$_
变量没有价值。 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.