0

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. It only works when I remove And $winver -eq $os1 -or $os2 -or $os3.

Otherwise it keeps telling me my powershell version needs to be upgraded. Im on V5 currently and $PSVersionTable.PSVersion.Major does says it 5 indeed. 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
}
}
IIIdefconIII
  • 353
  • 2
  • 5
  • 13
  • _` if ($powershellversion -lt 4 -And $winver -eq $os1 -or $os2 -or $os3){`_ And what's if it is PoSh4 on Windows 8? ;) Nothing? Than why are you checking both - the os and the PowreShell Version? – Clijsters Oct 26 '17 at 11:48
  • Try to nest some of your if statements and consider using [Switch](https://technet.microsoft.com/en-us/library/ff730937.aspx?f=255&MSPPError=-2147217396) – Clijsters Oct 26 '17 at 11:51
  • Oh the next one: It's not `proffesional`, it's `professional`. But however __why__ would you think does it make any difference? – Clijsters Oct 26 '17 at 11:53
  • Windows 8 only the enterprise version has bitlocker, we have no customers with win 8 enterprise so we can skip them. Anything else then os 1 2 and 3 should wirte outbut in the latest line. Powershell version check should be in cause up till powershell 3 there is nog get bilickervolume command. I will look into switch but then i have to relearn alot of things just for this. – IIIdefconIII Oct 26 '17 at 11:54
  • 1
    Just check if BitLocker and it's CmdLets are available... like `if (Get-Command -Name "Get-BitLockerVolume" -ErrorAction SilentlyContinue) {}` Then if available use `Get-BitLockerVolume` to check whether if there is an encrypted volume. – Clijsters Oct 26 '17 at 11:59
  • This is something that i have thinked of, but it fails cause the powershell version is not correct. and then we still get no notification that we need to upgrade the powershell versin (using max remote) – IIIdefconIII Oct 26 '17 at 12:00
  • Then check the PoSh version and then check if BitLocker is available. Where's the problem? – Clijsters Oct 26 '17 at 12:03

1 Answers1

2

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
  • If os2 has a value, then proceed
  • If os3 has a value, then proceed

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
  • evaluate if winver is either os1, os2 or 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:

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.

mtnielsen
  • 187
  • 8
  • Thank you so much for this information! it worked immidiatly and i will defintly take a look at operator precedence. – IIIdefconIII Oct 26 '17 at 12:04
  • Is there anything else i can improve on this script? – IIIdefconIII Oct 26 '17 at 12:05
  • ad first sight this was perfect, but powershell 2 for example doesnt support Operator Precedence, so is there any other way to make this script work? Thank You – IIIdefconIII Oct 26 '17 at 16:25
  • That could be because `-in` is not a valid operator in Powershell 2. Try this instead for backwards-compatibility: `if($powershellversion -lt 4 -and ($os1, $os2, $os3) -contains $winver) { ... }` – mtnielsen Oct 26 '17 at 16:47
  • i seperated the part of winver and powershell v check, it says bitlocker not support on ms w 10 enterprise, it should be ok and continue i have updated the code in OP sry to bother – IIIdefconIII Oct 26 '17 at 17:03
  • if($winver -contains ($os1, $os2, $os3)) i think this did the job, nope now it skips the first section, notcontains also not working as i wish – IIIdefconIII Oct 26 '17 at 17:07
  • got it: if(($os1, $os2, $os3) -notcontains $winver){ – IIIdefconIII Oct 26 '17 at 17:13