Home > Back-end >  Elseif statements only reading the "if" portion in boolean based statements
Elseif statements only reading the "if" portion in boolean based statements

Time:12-02

Good day everyone, I looked through some previous posts and unfortunately, was not able to find an answer that was applicable to my particular situation. The background is a I have script that removes Visual Studio and all its directory paths/registry entries which works. What I am having a problem with, is the validation. I am trying to validate that the certain paths/directories were removed.

What happens is, the function will only look at the first "if" statement and will seemingly ignore the other elseif and else statements I have included. I know I probably have them formatted wrong, as I am new to powershell There may be a better way to do this, and if so, feel free to share. Code seen below:

    function scriptvalidation{
$l1 = Test-Path -path "Path placeholder Number One"
$l2 = Test-Path -path "Path placeholder Number Two"
$l3 = Test-Path -path "Path placeholder Number Three"
$r1 = Test-Path -path "Registry path Number One"
$r2 = Test-Path -path "Registry path Number Two"
$r3 = Test-Path -path "Registry path Number Three"
$DirectoryArray = @($l1, $l2, $l3)
$RegistryArray = @($r1, $r2, $r3)

Above is the variables and arrays I used, below are the statements:

    if ($l1 -eq $true) {
Write-Host ("Path Placeholder number one was found and needs to be removed")
}
elseif ($l2 -eq $true) {
Write-Host ("Path Placeholder Number Two was found and needs to be removed")
}
elseif ($l3 -eq $true) {
Write-Host ("Path Placeholder Number three was found and needs to be removed")
}
else{[String] "$DirectoryArray -eq $false" Write-Host "Directory paths were removed successfully"
}


    if ($r1 -eq $true) {
Write-Host ("Registry Placeholder Number One was found and needs to be removed")
}
elseif ($r2 -eq $true) {
Write-Host ("Registry Placeholder Number Two was found and needs to be removed")
}
elseif ($r3 -eq $true) {
Write-Host ("Registry Placeholder Number Three was found and needs to be removed")
}
else{[String] "$RegistryArray -eq $false Write-Host "Registry paths were removed, script completed successfully"
}
scriptvalidation

That concludes this portion of my script, which I cannot get to run properly. When I have the program in question installed, the output I get is as follows: "Path Placeholder number one was found and needs to be removed" "Registry Placeholder Number one was found and needs to be removed" and that is it, nothing else.

I know I have this set up wrong, but being new, it has been tough to learn where it is wrong.

Any help would be appreciated.

CodePudding user response:

elseif means "if and only if the previous condition wasn't met AND this condition is met". Since file 2 might be present regardless of whether file 1 is, this is not the behavior your want.

Instead, use 3 separate if statements:

if ($l1) {
   Write-Host ("Path Placeholder number one was found and needs to be removed")
}

if ($l2) {
   Write-Host ("Path Placeholder Number Two was found and needs to be removed")
}

if ($l3) {
   Write-Host ("Path Placeholder Number three was found and needs to be removed")
}

Now the only thing needed is the last else block - here you'll want to test that neither of the $l* variables hold $true, which we can do like this:

if(-not($l1 -or $l2 -or $l3)){
  [string]"$DirectoryArray -eq $false" 
  Write-Host "Directory paths were removed successfully"
}

$l1 -or $l2 -or $l3 will evaluate to true if any of the three variables hold $true, so if they're all $false, this expression will be $false to - -not then inverts it so it's only ever $true if none of the variables are $true

  • Related