IF this -and / -or this do this

Welcome Forums General PowerShell Q&A IF this -and / -or this do this

This topic contains 13 replies, has 6 voices, and was last updated by

js
 
Participant
1 month, 2 weeks ago.

  • Author
    Posts
  • #119199

    Participant
    Points: 63
    Rank: Member

    Quick question hopefully I'm in the right section. Having a tough time getting proper results with the following quick example.

    $osnumber is getting populated from and outside conf file (XML format). If I only use the code up to the first closed parenthesis It works its when I add the second clause it fails. A ny help with this would greatly be appreciated.

    $OSversion = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name "ReleaseID").releaseID
    If (!($OSversion -eq $osnumber) -and (!(Test-Path "$env:SystemDrive\_SMSTaskSequence")))
    
    {
    Write-Host "True"
    break
    }
    Else
    {
    
    Write-host "False"
    
    }
  • #119211

    Participant
    Points: 832
    Helping Hand
    Rank: Major Contributor

    When you say Fails, does it throw any exception ? or it is entering to the else statement ?

  • #119221

    Participant
    Points: 112
    Helping Hand
    Rank: Participant

    The "else" statement is not tied to the "if" statement when pasting this in the shell. It will only work in a script. You can get around this by placing the "else" statement on the same line as the closing curly braces, like this:

    $OSversion = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name "ReleaseID").releaseID
    if (!($OSversion -eq $osnumber) -and (!(Test-Path "$env:SystemDrive\_SMSTaskSequence"))) {
        "True"
    } else {
        "False"
    }
    
  • #119227

    Participant
    Points: 63
    Rank: Member

    Thanks for the responses. I should have been more clear my fault on that.

    Lets say osnumber equals 1607 and my machine  = 1709

    And the folder does exist, i would expect it return true but its returning false. If i change the variable to match my machine 1709 and the folder does not exist  i would expect the same behavior.  But the later is occurring.

     

    Thanks

    Oscar

  • #119229

    Participant
    Points: 112
    Helping Hand
    Rank: Participant

    Ah, thanks for your response!

    In that case, it is failing because you are testing if the folder does not exist (hence the '!', which equals '-not').

    If you change it as follows it should return "True":

    if (!($OSversion -eq $osnumber) -and (Test-Path "$env:SystemDrive\_SMSTaskSequence")) {
  • #119230

    Participant
    Points: 35
    Rank: Member

    Based on your last post Oscar, it sounds like you need an "-or" operator, not an "-and" operator.  To meet your requirements, then you would have to structure your conditions as follows:

    If (($OSversion -eq $osnumber) -or (Test-Path "$env:SystemDrive\_SMSTaskSequence"))

    Unless I am misunderstanding something!  🙂

  • #119233

    Participant
    Points: 33
    Rank: Member

    I didn't notice what John did, in regard to the if and else not being tied to one another. They were both logically and actually "tied" in my opinion, even if they weren't where I would locate them, according to how I format my code. As best as I can see, everything in my below examples work as they should. That said, I did do my preferred code formatting, as well as change a few things.

    One thing worth mentioning, is removing the not operator (!) from your first condition. Using that operator and -eq is going to hurt your brain; it did mine anyway. Remove the -Not (you used !) and change -eq to -ne. Your future self will thank you. Anyway, the below code includes four examples and each of them appear to do exactly what you want, with little real change to your example. Do let us know if you're still seeing an problems going forward.

    ## Version: Want different for $true.
    ## Folder : Want non-existant for $true.
    ### On my computer, as I have 1803, this _only_ happens in the first below example, as it should.
    
    # Diff version and no folder: $true
    $OsNumber = '1809'
    $OSversion = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name "ReleaseID").releaseID
    If (($OSversion -ne $osnumber) -and (-Not(Test-Path -Path "$env:SystemDrive\_SMSTaskSequence"))) {
        $true
    } Else {
        $false
    }
    
    # Diff version and folder: $false
    New-Item -Path "$env:SystemDrive\_SMSTaskSequence" -ItemType Directory | Out-Null
    $OsNumber = '1809'
    $OSversion = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name "ReleaseID").releaseID
    If (($OSversion -ne $osnumber) -and (-Not(Test-Path -Path "$env:SystemDrive\_SMSTaskSequence"))) {
        $true
    } Else {
        $false
    }
    Remove-Item -Path "$env:SystemDrive\_SMSTaskSequence"
    
    # Same version and no folder: $false
    $OsNumber = '1803'
    $OSversion = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name "ReleaseID").releaseID
    If (($OSversion -ne $osnumber) -and (-Not(Test-Path -Path "$env:SystemDrive\_SMSTaskSequence"))) {
        $true
    } Else {
        $false
    }
    
    # Same version and folder: $false
    New-Item -Path "$env:SystemDrive\_SMSTaskSequence" -ItemType Directory | Out-Null
    $OsNumber = '1803'
    $OSversion = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name "ReleaseID").releaseID
    If (($OSversion -ne $osnumber) -and (-Not(Test-Path -Path "$env:SystemDrive\_SMSTaskSequence"))) {
        $true
    } Else {
        $false
    }
    Remove-Item -Path "$env:SystemDrive\_SMSTaskSequence"
    
    True
    False
    False
    False
    
    • #119241

      Participant
      Points: 63
      Rank: Member

      Got a chance to review this while waiting for my bus home. Definitely going to head your warning. With the (! And -eq, my brain definately was hurting. I will test it, but from the looks of it looks like it will work with your code formatting.

       

      Appreciate everyone's input on this.

    • #119284

      Participant
      Points: 63
      Rank: Member

      Thanks Tommy this worked exactly as needed. Following your suggestion removing the ! -eq made reading and understanding my own code significantly easier.

      Blown away by the fast responses. Thank you all for your input.

  • #119236

    Participant
    Points: 63
    Rank: Member

    Correct, in practice osnumber will be set as 1607 and the folder should not exist for the rest of the code to initiate in the else statement block.

    I was using -or over -and.

    In my last snippet it was a copy of me testing -and vs -or.

    This is because if the folder does exist then it means an upgrade process im calling earlier has already began thus the break in the if statement block.

  • #119238

    Participant
    Points: 63
    Rank: Member

    Wow thanks, guys a lot to review, but I'm leaving work now. I wanted to leave you with something. Below is the code and output without folder followed by with folder and notice the os version variable as well. Its almost like it is only reading the first ( )

    $osnumber = 1709
    $path = Test-Path "$env:SystemDrive\_SMSTaskSequence"
    $OSversion = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name "ReleaseID").releaseID
    If (!($OSversion -eq $osnumber) -or (!(Test-Path "$env:SystemDrive\_SMSTaskSequence")))
    {Write-host "The Statement is True"
    }else{
    Write-host "The Statement is False"}
    
    
    # showing th file does not exist
    # Showing osversion 
    $OSversion
    $path
    
    The Statement is True
    1709
    False
    
    
    
    $osnumber = 1607
    $path = Test-Path "$env:SystemDrive\_SMSTaskSequence"
    $OSversion = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name "ReleaseID").releaseID
    If (!($OSversion -eq $osnumber) -or (!(Test-Path "$env:SystemDrive\_SMSTaskSequence")))
    {Write-host "The Statement is True"
    }else{
    Write-host "The Statement is False"}
    
    
    # showing the file does not exist
    # Showing osversion 
    $OSversion
    $path
    
    The Statement is True
    1709
    True

    Notice how in the last the osnumber is set to 1607 it does not match 1709 it should have returned The statement is False.

  • #119302
    js

    Participant
    Points: 316
    Helping Hand
    Rank: Contributor

    I feel like I'm back in computer science class. Just dealing with the original post, the code is working correctly using '-and'.

    PS /Users/js> $true -and $false                                                   
    False
    PS /Users/js> $false -and $true                                                   
    False
    

    Ok, in the last example, you're doing:

    PS /Users/js> $false -or $true                                                    
    True
    
    PS /Users/js> $true -or $false                                                     
    True
    

    Do you understand what the "!" does?

    PS /Users/js> !$false                                                             
    True
    PS /Users/js> !$true                                                              
    False
    
    
  • #119305

    Participant
    Points: 63
    Rank: Member

    JS yes i do, ! Is used for not.  I used it because in my brain at the time of writing it made sense. If not -eq to than true the first condition was never the issue it was the second that was causing me my headaches litterally especially with different variations.  Re writing using Tommys suggestion made it easier to read and proof the output. I know in ps there is a few ways to get to the same solution.

    • #119311
      js

      Participant
      Points: 316
      Helping Hand
      Rank: Contributor

      You might find this less confusing:

      If ( $OSversion -neq $osnumber -or (Test-Path $env:SystemDrive\_SMSTaskSequence) -eq $false )
      

You must be logged in to reply to this topic.