我正在 PowerShell 中執行 IF 陳述句,有時我會這樣做:
(Get-BitlockerVolume -MountPoint "C:").KeyProtector.keyprotectortype
這給了我這種格式的結果,彼此疊加

我想撰寫我的 IF 陳述句來檢查上述命令的輸出是否同時包含“TpmPin”和“RecoveryPassword”,但不確定正確的語法是什么。
我嘗試了這樣的事情,但它沒有按預期作業,即使它應該是錯誤的,結果也總是正確的。
if ((Get-BitlockerVolume -MountPoint "C:").KeyProtector.keyprotectortype -contains "tpmpin" && "RecoveryPassword")
這也不起作用:
if ((Get-BitlockerVolume -MountPoint "C:").KeyProtector.keyprotectortype -contains "tpmpinRecoveryPassword")
ps 我不想做嵌套的 IF 陳述句,因為我已經做了多個。
uj5u.com熱心網友回復:
在陳述句Get-BitLockerVolume之前呼叫,將結果存盤在變數中,然后使用運算子確保兩者都找到:if-and
$KeyProtectors = Get-BitlockerVolume -MountPoint "C:" |ForEach-Object KeyProtector
if($KeyProtectors.KeyProtectorType -contains 'TpmPin' -and $KeyProtectors.KeyProtectorType -contains 'RecoveryPassword'){
# ... both types were present
}
如果您有任意數量的值要測驗是否存在,另一種方法是測驗它們都不存在:
$KeyProtectors = Get-BitlockerVolume -MountPoint "C:" |ForEach-Object KeyProtector
$mustBePresent = @('TpmPin', 'RecoveryPassword')
if($mustBePresent.Where({$KeyProtectors.KeyProtectorType -notcontains $_}, 'First').Count -eq 0){
# ... all types were present
}
uj5u.com熱心網友回復:
如果檢查如下所示,您可以撰寫一個 powershell:
if((((Get-BitlockerVolume -MountPoint "C:").KeyProtector.keyprotectortype) -join ",") -eq "Tpm,RecoveryPassword")
{
write-host "matches"
}
else
{
write-host "does not match"
}
火柴
警告:正如@MathiasR.Jessen 所說,此解決方案假定值的順序。因此,如果值順序發生變化,上述解決方案將不起作用。我們需要遵循@MathiasR.Jessen 提供的解決方案
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/524880.html
標籤:电源外壳windows-11PowerShell-7.2
下一篇:如何在SQL中計算上標值
