简体   繁体   English

Powershell Bitlocker检查

[英]Powershell bitlocker check

Hi im having diffeculties to get my script working: It keeps failing on the first write output, even when the powershell version is higher then 4 . 大家好,我的脚本无法正常工作:即使powershell版本高于4 ,它也始终在第一个写入输出中失败。 It only works when I remove And $winver -eq $os1 -or $os2 -or $os3 . 它仅在删除And $winver -eq $os1 -or $os2 -or $os3

Otherwise it keeps telling me my powershell version needs to be upgraded. 否则,它总是告诉我我的Powershell版本需要升级。 Im on V5 currently and $PSVersionTable.PSVersion.Major does says it 5 indeed. 我目前在V5上, $PSVersionTable.PSVersion.Major确实说了5 What am i doing wrong? 我究竟做错了什么?

    $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
}
}

Let's see what this actually does: 让我们看看这实际上是做什么的:

if ($powershellversion -lt 4 -And $winver -eq $os1 -or $os2 -or $os3) { ... }
  • If your powershell version is less than 4, and Win version is equal to os1, then proceed 如果您的Powershell版本小于4,并且Win版本等于os1,则继续
  • If os2 has a value, then proceed 如果os2有一个值,则继续
  • If os3 has a value, then proceed 如果os3有一个值,则继续

The topic here is Operator Precedence , specifically, what happens first when evaluating a line of code, what happens second, third, and so on. 这里的主题是运算符优先级 ,具体地说,是在评估一行代码时首先发生的事情,其次是第二,第三次发生的事情等等。 Just like in algebra math, adding parens around part of the formula changes the order in whcih you read it. 就像在代数数学中一样,在部分公式周围添加括号会改变您阅读公式的顺序。

So, you can mess around with parens to get your logic to work: 因此,您可以使用括号来使逻辑起作用:

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

In other words 换一种说法

  • evaluate if PS version is < 4 ( $powershellversion -lt 4 ), -and 评估PS版本是否<4( $powershellversion -lt 4 ), -and
  • evaluate if winver is either os1, os2 or os3: ( ($winver -eq $os1) -or ($winver -eq $os2) -or ($winver -eq $os3) ) . 评估winver是os1,os2还是os3: ( ($winver -eq $os1) -or ($winver -eq $os2) -or ($winver -eq $os3) )

Or, you can rearrange your logic a bit by putting your os variables into an array, and seeing if $winver is in there: 或者,您可以通过将os变量放入数组中并查看$winver是否在其中来稍微重新排列逻辑:

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

Edit: Or 编辑:或

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

for backwards compatibility with v2.0. 与v2.0向后兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM