Windows Vista Forums

Foreach and if else loop

  1. #1


    Jsimpson Guest

    Foreach and if else loop

    Ok, I am missing something here... I want to do a loop to check for
    three things from a ping-host: a reply ("1"), no reply ("0"), and
    failed to resolve ("null").

    My first try that worked looked like this:

    foreach ($comp in get-qadcomputer)
    {
    if ((ping-host -hostname $comp.name -count 1).Received -eq "1")
    { Write-Host "1"}
    elseif ((ping-host -hostname $comp.name -count 1).Received -eq "0")
    {Write-Host "0"}
    else {Write-Host "null"}
    }

    The problem with this is it pings each twice. So then I tried this:


    foreach ($comp in get-qadcomputer)
    {{$result = ping-host -hostname $comp.name -count 1}
    if ($result.Received -eq "1") { Write-Host "1"}
    elseif ($result.Received -eq "0") {Write-Host "0"}
    else {Write-Host "null"}
    }

    But I am sure my syntax it all wrong. Can someone give me a hand?

    Thanks,
    Jim

      My System SpecsSystem Spec

  2.   


  3. #2


    Jon Guest

    Re: Foreach and if else loop

    "Jsimpson" <jsimpson2000@xxxxxx> wrote in message
    news:295b6957-1974-4f04-83e0-d016e9495441@xxxxxx

    >
    > foreach ($comp in get-qadcomputer)
    > {{$result = ping-host -hostname $comp.name -count 1}
    > if ($result.Received -eq "1") { Write-Host "1"}
    > elseif ($result.Received -eq "0") {Write-Host "0"}
    > else {Write-Host "null"}
    > }
    >

    You're on the right lines by the looks of it. Try it without the additional
    curly brackets eg

    foreach ($comp in get-qadcomputer) {
    $result = ping-host -hostname $comp.name -count 1
    if ($result.Received -eq "1") { Write-Host "1"}
    elseif ($result.Received -eq "0") {Write-Host "0"}
    else {Write-Host "null"}
    }




    --
    Jon




      My System SpecsSystem Spec

  4. #3


    Jsimpson Guest

    Re: Foreach and if else loop

    On Jun 17, 12:51*pm, "Jon" <Email_Addr...@xxxxxx> wrote:

    > "Jsimpson" <jsimpson2...@xxxxxx> wrote in message
    >
    > news:295b6957-1974-4f04-83e0-d016e9495441@xxxxxx
    >
    >
    >

    > > foreach ($comp in get-qadcomputer)
    > > {{$result = ping-host -hostname $comp.name -count 1}
    > > if ($result.Received -eq "1") { Write-Host "1"}
    > > elseif ($result.Received -eq "0") {Write-Host "0"}
    > > else {Write-Host "null"}
    > > }
    >
    > You're on the right lines by the looks of it. Try it without the additional
    > curly brackets eg
    >
    > foreach ($comp in get-qadcomputer) {
    > $result = ping-host -hostname $comp.name -count 1
    > if ($result.Received -eq "1") { Write-Host "1"}
    > elseif ($result.Received -eq "0") {Write-Host "0"}
    > else {Write-Host "null"}
    >
    > }
    >
    > --
    > Jon
    Doh!! Thank you so much.

      My System SpecsSystem Spec


Foreach and if else loop
Similar Threads
Thread Forum
Why is my foreach loop only looping through the first object in th PowerShell
Remove from CSV file using a foreach loop PowerShell
method Error in foreach loop PowerShell
Foreach loop over $NULL in Powershell PowerShell
Output of Powershell ForEach Loop PowerShell