Foreach and if else loop


  1.    17 Jun 2008 #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.    17 Jun 2008 #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

  3.    17 Jun 2008 #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
$domain = new-object system.directoryservices.directoryentry("LDAP://ou=domain controllers,dc=mhco,dc=mizehouser,dc=com") $searcher = new-object...
PowerShell
Can't append date to file name through a ForEach Loop?
Can you guys help me out and tell whats wrong with this: $curDate = get-date -uformat "%Y-%m-%d.%H.%M.%S" $test = "C:\test" $GCI = Get-ChildItem...
PowerShell
Remove from CSV file using a foreach loop
I have a list of users in a seperate text file that I want to exclude from the csv file. E.g. List.txt: User1 User3 But if I...
PowerShell
method Error in foreach loop
Hi I wrote a PS script which gets registry informations about specific drivers. Here is the code:...
PowerShell
PS stalls in foreach loop (remote eventlog)
Hi, I made a script to get some remote events, and when i get the collection of events, and start working on each collected event, at the end, it...
PowerShell
Foreach loop over $NULL in Powershell
I've noticed that in powershell, if I run a foreach loop over a null valued expression, the loop runs at least once. The following code illustrates...
PowerShell
Output of Powershell ForEach Loop
I'm trying to query my root domain for all it's users and produce a csv file that includes the user name and desription. I can use the write-host...
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • Old server
    About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 05:37.
    .