【问题标题】:Powershell bitlocker checkPowershell bitlocker 检查
【发布时间】:2017-10-26 11:44:25
【问题描述】:

您好,我的脚本无法正常运行: 即使 powershell 版本高于4,它在第一次写入输出时仍然失败。它仅在我删除 And $winver -eq $os1 -or $os2 -or $os3 时有效。

否则它一直告诉我我的 powershell 版本需要升级。我目前在 V5 上,$PSVersionTable.PSVersion.Major 确实说过5。 我做错了什么?

    $winver = (Get-WmiObject -class Win32_OperatingSystem).Caption
$powershellversion = $PSVersionTable.PSVersion.Major
$os1 = "Microsoft Windows 7 Professional"
$os2 = "Microsoft Windows 10 Pro"
$os3 = "Microsoft Windows 10 Enterprise"

if($winver -ne ($os1, $os2, $os3) -contains $winver){
    Write-Host "Bitlocker not supported on $winver"
    Exit 0
}

if($powershellversion -lt 4){
    Write-Host "Upgrade Powershell Version"
    Exit 1010
}
else
{

$bitlockerkey = (Get-BitLockerVolume -MountPoint C).KeyProtector.RecoveryPassword
$pcsystemtype = (Get-WmiObject -Class Win32_ComputerSystem).PCSystemType
if ($pcsystemtype -eq "2"){
$setsystemtype = "Laptop"
}
else {
$setsystemtype = "Desktop"
}

if ($setsystemtype -eq "laptop" -And $bitlockerkey -eq $null  -and ($os1, $os2, $os3) -contains $winver){
Write-Host "$setsystemtype without bitlocker"
Exit 1010
}

if ($setsystemtype -eq "desktop" -And $bitlockerkey -eq $null  -and ($os1, $os2, $os3) -contains $winver){
Write-Host "$setsystemtype without bitlocker"
Exit 0
}

if ($winver -eq ($os1, $os2, $os3) -contains $winver){
Write-Host "$bitlockerkey"
Exit 0
}
}

【问题讨论】:

  • ` if ($powershellversion -lt 4 -And $winver -eq $os1 -or $os2 -or $os3){` 如果是Windows上的PoSh4呢8? ;) 没有?比你为什么要同时检查操作系统和 PowreShell 版本?
  • 尝试嵌套一些 if 语句并考虑使用Switch
  • 哦,下一个:不是proffesional,是professional。但是为什么你会认为这有什么不同吗?
  • Windows 8只有企业版有bitlocker,我们没有win 8企业的客户,所以我们可以跳过。除了 os 1 2 和 3 之外的任何东西都应该在最新的行中输出。 Powershell 版本检查应该在 powershell 3 之前进行,没有 get bilickervolume 命令。我会研究开关,但我必须为此重新学习很多东西。
  • 只需检查 BitLocker 和它的 CmdLets 是否可用...比如 if (Get-Command -Name "Get-BitLockerVolume" -ErrorAction SilentlyContinue) {} 然后如果可用,请使用 Get-BitLockerVolume 检查是否有加密卷。

标签: powershell if-statement operator-precedence bitlocker


【解决方案1】:

让我们看看这实际上做了什么:

if ($powershellversion -lt 4 -And $winver -eq $os1 -or $os2 -or $os3) { ... }
  • 如果你的powershell版本小于4,和Win版本相等 到os1,然后继续
  • 如果os2有值,则继续
  • 如果 os3 有值,则继续

这里的主题是Operator Precedence,具体来说,在评估一行代码时首先会发生什么,第二个、第三个会发生什么,等等。就像在代数数学中一样,在公式的一部分周围添加括号会改变您阅读它的顺序。

因此,您可以使用括号来使您的逻辑正常工作:

if($powershellversion -lt 4 -and ( ($winver -eq $os1) -or ($winver -eq $os2) -or ($winver -eq $os3) ))

换句话说

  • 判断PS版本是否$powershellversion -lt 4),-and
  • 评估 winver 是 os1、os2 还是 os3:( ($winver -eq $os1) -or ($winver -eq $os2) -or ($winver -eq $os3) )

或者,您可以通过将您的 os 变量放入一个数组中来重新排列您的逻辑,并查看$winver 是否在其中:

if($powershellversion -lt 4 -and $winver -in ($os1, $os2, $os3)) { ... }

编辑:或

if($powershellversion -lt 4 -and ($os1, $os2, $os3) -contains $winver) { ... }

为了向后兼容 v2.0。

【讨论】:

  • 非常感谢您提供这些信息!它工作得很好,我一定会看看运算符优先级。
  • 这个脚本还有什么我可以改进的吗?
  • 一见钟情这是完美的,但是例如 powershell 2 不支持运算符优先级,那么还有其他方法可以使这个脚本工作吗?谢谢
  • 这可能是因为-in 不是 Powershell 2 中的有效运算符。请尝试使用此方法以实现向后兼容性:if($powershellversion -lt 4 -and ($os1, $os2, $os3) -contains $winver) { ... }
  • 我分离了 winver 和 powershell v 检查的部分,它说 bitlocker 在 ms w 10 企业上不支持,应该没问题并继续我已经更新了 OP 中的代码 sry 打扰
猜你喜欢
  • 2021-05-31
  • 2023-04-05
  • 1970-01-01
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多