简体   繁体   中英

If and ElseIf statement not working in PowerShell?

I have written the following PowerShell script below:

if ($Animal -eq "Chicken") {
    if ($Food -eq "Egg") {
        Write-Host "This chicken eats egg"
    } ElseIf ($Food -eq "Soup") {
        Write-Host "This chicken eats soup"
} ElseIf ($Animal -eq "Cow") {
    if ($Food -eq "Egg") {
        Write-Host "This cow eats egg"
    } ElseIf ($Food -eq "Soup") {
        Write-Host "This cow eats soup"
} ElseIf ($Animal -eq "Zebra") {
    if ($Food -eq "Egg") {
        Write-Host "This zebra eats egg"
    } ElseIf ($Food -eq "Soup") {
        Write-Host "This zebra eats soup"

The script works for the Chicken and the Cow but does not register the zebra portion of the code. I am not sure of what I am doing wrong as there are no errors being returned. Can someone please provide some guidance?

Whilst I fully agree with Doug's answer, I am completely lost as to why you are using if statements in the first place. If this is a learning exercise, I would recommend using a switch statement such as this:

$Animal = "Zebra"
$Food = "Soup"
$output = ""

switch ($Animal){
    "Chicken" { $output = "This $($Animal) eat $($Food)"; break}
    "Cow" { $output = "This $($Animal) eat $($Food)"; break }
    "Zebra" { $output = "This $($Animal) eat $($Food)"; break }
}

Write-Output $output

or just outputting the values without the unnecessary checking:

$Animal = "Zebra"
$Food = "Soup"

Write-Output "This $($Animal) eat $($Food)"

You are missing the } after your ElseIf s.

Corrected code:

if ($Animal -eq "Chicken") {
    if ($Food -eq "Egg") {
        Write-Host "This chicken eats egg"
    } ElseIf ($Food -eq "Soup") {
        Write-Host "This chicken eats soup"
    }
} ElseIf ($Animal -eq "Cow") {
    if ($Food -eq "Egg") {
        Write-Host "This cow eats egg"
    } ElseIf ($Food -eq "Soup") {
        Write-Host "This cow eats soup"
    }
} ElseIf ($Animal -eq "Zebra") {
    if ($Food -eq "Egg") {
        Write-Host "This zebra eats egg"
    } ElseIf ($Food -eq "Soup") {
        Write-Host "This zebra eats soup"
    }
}

Better Way

That said, as @MathiasR.Jessen pointed out, this can be simplified using variable expansion:

Write-Host "This $($Animal.ToLower()) eats $($Food.ToLower())"

See About Quoting Rules for more info.

Doug is correct, you missed some }

I have re-formatted it for you. I find this formatting helps to catch mistakes like this...

if ($Animal -eq "Chicken") 
{
    if ($Food -eq "Egg") 
    {
        Write-Host "This chicken eats egg"
    } 
    ElseIf ($Food -eq "Soup") 
    {
        Write-Host "This chicken eats soup"
    }
}
ElseIf ($Animal -eq "Cow") 
{
    if ($Food -eq "Egg")
    {
        Write-Host "This cow eats egg"
    }
    ElseIf ($Food -eq "Soup")
    {    
        Write-Host "This cow eats soup"
    }
} 
ElseIf ($Animal -eq "Zebra") 
{
    if ($Food -eq "Egg")
    {
        Write-Host "This zebra eats egg"
    } 
    ElseIf ($Food -eq "Soup") 
    {
        Write-Host "This zebra eats soup"
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM