使用Powershell挂起Bitlocker-出现错误

时间:2018-08-02 19:09:12

标签: powershell bitlocker

我正在部署一个用于挂起Bitlocker的软件包,然后在许多HP系统上应用BIOS更新。该脚本在大约90-95%的系统上都可以正常工作,但是大约有5-10%的系统无法运行。

这是我正在使用的脚本:

#Create Variable of Bitlocker Status
$Volume = Get-WmiObject -Namespace root\cimv2\security\microsoftvolumeencryption -Query "select * from win32_encryptablevolume where DriveLetter = 'C:'"
$Status = $Volume.GetProtectionStatus()
$BitLockerStatus = $status.ProtectionStatus


#Check if Bilocker enabled, then suspend.
If ($BitlockerStatus -eq '1'){$Volume.DisableKeyProtectors()}
$Status = $Volume.GetProtectionStatus()
$BitLockerStatus = $status.ProtectionStatus
If($BitLockerStatus -eq '1'){
    mofcomp.exe c:\windows\system32\wbem\win32_encryptablevolume.mof
    Manage-bde.exe -protectors -disable c:
}

#Update Variable of Bitlocker Status
$BitLockerStatus = $status.ProtectionStatus

这是错误:

Message        : You cannot call a method on a null-valued expression.
InnerException : 

FullyQualifiedErrorId : InvokeMethodOnNull
ScriptStackTrace      : at <ScriptBlock>, 
                        C:\Windows\ccmcache\75\Deploy-Application.ps1: line 129
                        at <ScriptBlock>, <No file>: line 1
                        at <ScriptBlock>, <No file>: line 1

``PositionMessage : At C:\Windows\ccmcache\75\Deploy-Application.ps1:129 char:9
                  +         $Status = $Volume.GetProtectionStatus()
                  +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我明白了错误的含义,但令我感到困惑的是,为什么只有少数几套系统会失败。

1 个答案:

答案 0 :(得分:0)

只需测试$Volume是否不是$null(无论如何都是最佳实践)。 WMI查询出于各种原因将返回$null,例如从不兼容的Windows版本到没有有效的C:可加密卷等,因此导致您的错误。

#Create Variable of Bitlocker Status
$Volume = Get-WmiObject -Namespace root\cimv2\security\microsoftvolumeencryption -Query "select * from win32_encryptablevolume where DriveLetter = 'C:'"

if($Volume)
{

    $Status = $Volume.GetProtectionStatus()
    $BitLockerStatus = $status.ProtectionStatus


    #Check if Bilocker enabled, then suspend.
    If ($BitlockerStatus -eq '1'){$Volume.DisableKeyProtectors()}
    $Status = $Volume.GetProtectionStatus()
    $BitLockerStatus = $status.ProtectionStatus
    If($BitLockerStatus -eq '1'){
        mofcomp.exe c:\windows\system32\wbem\win32_encryptablevolume.mof
        Manage-bde.exe -protectors -disable c:
    }

    #Update Variable of Bitlocker Status
    $BitLockerStatus = $status.ProtectionStatus

}

唯一需要添加的内容是标记WMI查询失败的机器,以便技术在需要/需要时进一步跟进。

相关问题