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.
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:
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.
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"))
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"
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.
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.
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.
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.
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.