Powershell bitlocker检查

Powershell bitlocker检查,powershell,if-statement,operator-precedence,bitlocker,Powershell,If Statement,Operator Precedence,Bitlocker,嗨,我有不同的想法来让我的脚本工作: 即使powershell版本高于4,它在第一次写入输出时仍会失败。它只在我删除和$winver-eq$os1-或$os2-或$os3时起作用 否则它会一直告诉我我的powershell版本需要升级。我现在在V5上,$PSVersionTable.PSVersion.Major确实说了5。 我做错了什么 $winver = (Get-WmiObject -class Win32_OperatingSystem).Caption $powershellv

嗨,我有不同的想法来让我的脚本工作: 即使powershell版本高于
4
,它在第一次写入输出时仍会失败。它只在我删除
和$winver-eq$os1-或$os2-或$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) { ... }
  • 如果您的powershell版本小于4,而Win版本等于 到os1,然后继续
  • 如果os2有一个值,则继续
  • 如果os3有一个值,则继续
这里的主题是,具体地说,当计算一行代码时,首先发生什么,第二次、第三次发生什么,等等。就像在代数数学中一样,在公式的一部分加上括号会改变你阅读的顺序

所以,你可以和帕伦斯混在一起,让你的逻辑发挥作用:

if($powershellversion -lt 4 -and ( ($winver -eq $os1) -or ($winver -eq $os2) -or ($winver -eq $os3) ))
换句话说

  • 评估PS版本是否为<4(
    $powershell版本-lt 4
    )、
    -和
  • 评估winver是os1、os2还是os3:
    ($winver-eq$os1)-还是($winver-eq$os2)-或($winver-eq$os3))
或者,您可以将操作系统变量放入数组,然后查看
$winver
是否在其中,从而稍微重新排列逻辑:

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

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

关于与v2.0的向后兼容性。

让我们看看它的实际功能:

if ($powershellversion -lt 4 -And $winver -eq $os1 -or $os2 -or $os3) { ... }
  • 如果您的powershell版本小于4,而Win版本等于 到os1,然后继续
  • 如果os2有一个值,则继续
  • 如果os3有一个值,则继续
这里的主题是,具体地说,当计算一行代码时,首先发生什么,第二次、第三次发生什么,等等。就像在代数数学中一样,在公式的一部分加上括号会改变你阅读的顺序

所以,你可以和帕伦斯混在一起,让你的逻辑发挥作用:

if($powershellversion -lt 4 -and ( ($winver -eq $os1) -or ($winver -eq $os2) -or ($winver -eq $os3) ))
换句话说

  • 评估PS版本是否为<4(
    $powershell版本-lt 4
    )、
    -和
  • 评估winver是os1、os2还是os3:
    ($winver-eq$os1)-还是($winver-eq$os2)-或($winver-eq$os3))
或者,您可以将操作系统变量放入数组,然后查看
$winver
是否在其中,从而稍微重新排列逻辑:

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

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


用于与v2.0向后兼容。

`if($powershellversion-lt 4-和$winver-eq$os1-或$os2-或$os3){‘为什么在Windows 8上是POSH4??)没有什么?为什么要检查OS和POWRESHEL版本?尝试嵌套你的if语句并考虑使用下一个语句:它不是代码> PrimeSosis<代码>,它是代码>专业< /代码>。但是为什么你认为它有什么区别?Windows 8只有输入。prise版本有bitlocker,我们没有win 8 enterprise的客户,所以我们可以跳过他们。除os 1 2和3之外的任何其他内容都应该在最新一行中出现。Powershell版本检查应该在Powershell 3之前进行,直到Powershell 3没有get bilickervolume命令。我将查看交换机,但接下来我必须重新学习很多东西,只为了是。只需检查BitLocker及其cmdlet是否可用…如
if(Get命令-Name“Get BitLockerVolume”-ErrorAction SilentlyContinue){}
然后如果可用,使用
Get BitLockerVolume
检查是否存在加密卷。`if($powershell版本-lt 4-和$winver-eq$os1-或$os2-或$os3){‘为什么在Windows 8上是POSH4??)没有什么?为什么要检查OS和POWRESHEL版本?尝试嵌套你的if语句并考虑使用下一个语句:它不是代码> PrimeSosis<代码>,它是代码>专业< /代码>。但是为什么你认为它有什么区别?Windows 8只有输入。prise版本有bitlocker,我们没有win 8 enterprise的客户,所以我们可以跳过他们。除os 1 2和3之外的任何其他内容都应该在最新一行中出现。Powershell版本检查应该在Powershell 3之前进行,直到Powershell 3没有get bilickervolume命令。我将查看交换机,但接下来我必须重新学习很多东西,只为了是。只需检查BitLocker及其cmdlet是否可用…如
if(Get命令-Name“Get BitLockerVolume”-ErrorAction SilentlyContinue){}
然后,如果可用,请使用
获取BitLockerVolume
检查是否存在加密卷。非常感谢您提供的这些信息!它很快就起作用了,我将明确查看运算符优先级。这个脚本还有什么可以改进的地方吗?乍一看,这很完美,但powershell 2就不是了支持运算符优先级,那么是否有其他方法使此脚本工作?谢谢,这可能是因为中的
-in在Powershell 2中不是有效的运算符。请尝试此操作以实现向后兼容性:
如果($powershellversion-lt 4-和($os1,$os2,$os3)-包含$winver){…}
我分离了winver和powershell v检查的部分,它说bitlocker在ms w 10 enterprise上不支持,应该可以继续了。我已经在OP sry中更新了代码,非常感谢您提供的这些信息!它很快就起作用了,我将明确查看操作员优先级。还有什么我可以改进的吗脚本?乍一看,这是完美的,但powershell 2不支持运算符优先级,因此是否有其他方法使此脚本工作?感谢您,这可能是因为
中的
-in在powershell 2中不是有效的运算符。请尝试此操作以实现向后兼容性:
如果($powershellversion-lt 4-和($os1,$os2,$os3)-包含$winver){…}
我将winver和powershell v检查的部分分开,它说bitlocker在ms w 10 enterprise上不支持,应该可以继续我已更新