mercredi 1 décembre 2021

Elseif statements only reading the "if" portion in boolean based statements

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.

Aucun commentaire:

Enregistrer un commentaire