使用Powershell挂起Bitlocker-获取错误



我正在部署一个程序包来挂起bitlocker,然后在我们的许多HP系统上应用BIOS更新。对于大约90-95%的系统来说,该脚本可以正常工作,但大约有5-10%的系统出现故障

这是我正在使用的脚本:

#Create Variable of Bitlocker Status
$Volume = Get-WmiObject -Namespace rootcimv2securitymicrosoftvolumeencryption -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:windowssystem32wbemwin32_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:Windowsccmcache75Deploy-Application.ps1: line 129
at <ScriptBlock>, <No file>: line 1
at <ScriptBlock>, <No file>: line 1
``PositionMessage : At C:Windowsccmcache75Deploy-Application.ps1:129 char:9
+         $Status = $Volume.GetProtectionStatus()
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我明白了错误的含义,但我感到困惑的是,为什么只有少数一小部分系统出现故障。

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

#Create Variable of Bitlocker Status
$Volume = Get-WmiObject -Namespace rootcimv2securitymicrosoftvolumeencryption -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:windowssystem32wbemwin32_encryptablevolume.mof
Manage-bde.exe -protectors -disable c:
}
#Update Variable of Bitlocker Status
$BitLockerStatus = $status.ProtectionStatus
}

如果需要,唯一的附加功能是标记WMI查询失败的机器,以便技术人员在需要/需要时进一步跟进。

最新更新