如何从 powershell 中的一行获取特定值

How do I get a specific value from a line in powershell

我正在尝试从 manage-bde -status C: 命令中获取特定值,return 如下: BitLocker 驱动器加密:配置工具版本 10.0.18362 版权所有 (C) 2013 Microsoft Corporation。保留所有权利。

C 卷:[] [OS 卷]

Size:                 237.29 GB
BitLocker Version:    None
Conversion Status:    Fully Decrypted
Percentage Encrypted: 0.0%
Encryption Method:    None
Protection Status:    Protection Off
Lock Status:          Unlocked
Identification Field: None
Key Protectors:       None Found

我正在尝试获取标记为 Protection Status 和 return Off[=12= 的行的结尾]

如果我没理解错的话,你想看看保护状态下是否匹配关闭? 如果是这样,这是一段丑陋的代码,我很快就完成了,但可以得到你想要的:

$status = manage-bde -status C: | Select-String 'Protection'
if ($status -match 'Off'){
Write-Output $true
} else {
Write-Host $false
}

根据我的评论,我会使用 Get-BitLockerVolume 代替,因为它 returns 是一个更容易查询的对象:

Get-BitLockerVolume -MountPoint C: | Select-Object -ExpandProperty ProtectionStatus

我使用了类似于上面 post 的方法来确定是否已在制造商的驱动器上启用了 BitLocker,该驱动器的标识字段中始终为未知或 none。

# Check for OEM configuration of BitLocker

$blidfield = manage-bde -status C: | Select-String 'Identification Field'
$bloemencrypted = manage-bde -status C: | Select-String 'Conversion Status'
if ($blidfield -match 'None' -or $blidfield -match 'Unknown' -and ($bloemencrypted -match 'Fully Encrypted' -or $bloemencrypted -match 'Used Space Only Encrypted')){
    Write-Log "BitLocker appears to be configured with OEM configuration, Starting to decrypt."
    manage-bde -off C:
    exit
} else {
    Write-Log "BitLocker doesn't appear to be configured with OEM configuration"
}

请注意,行 'manage-bde -off C:' 将解密 OS 驱动器。