Newbie issue with array output...

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Drew Drew 3 years, 9 months ago.

  • Author
    Posts
  • #10318
    Profile photo of Drew
    Drew
    Participant

    Does it seem I am using the "+=" operator wrong? I cannot get the elements of an array to display separately on a new line with Write-Host. This is really just for concept of the constructs and not much of anything else. If someone could assist me with this, I would greatly appreciate it. I'm not sure if "-join "n'" would be the correct usage as each element is displayed in a single string of characters when the array is called.

    Here's the code snippet:

    ForEach ($server in $servers){

    if (Test-Connection $server -Quiet -Count 1){
    Write-Host $server "- Ping Success!"
    }else{

    Write-Host $server "- Ping Failed!"
    $pingFailed += $server
    }
    }

    if ($PingFailed -ne $null){
    Write-Host "The following servers failed ping: " $PingFailed
    }

  • #10319
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    Not sure why you are showing the failure data twice but I tried your code and got this


    server04 - Ping Failed!
    server05 - Ping Failed!
    The following servers failed ping: server04server05

    What seems to have happened is that you have created a single string with all of the failed server names rather than an array. if its an array you want you'll have to do something like this:

    $servers = "server04", "server05"
    $pingFailed = @()

    ForEach ($server in $servers){

    if (Test-Connection $server -Quiet -Count 1){
    Write-Host $server “- Ping Success!”
    }else{

    Write-Host $server “- Ping Failed!”
    $pingFailed += $server
    }
    }

    if ($PingFailed -ne $null){
    Write-Host “The following servers failed ping: ”
    $PingFailed
    }

    which gave an output like this

    server04 - Ping Failed!
    server05 - Ping Failed!
    The following servers failed ping:
    server04
    server05

    I've defined the array before the foreach statement and I've moved the display of the array contents to its own line rather than being part of the write-host line

    Hope this helps. if you have further questions only to happy to help

  • #10322
    Profile photo of Drew
    Drew
    Participant

    Thank you, Richard. I see where the issue was... I had actually commented out the array outside of the loop. I suppose that's what happens after looking at something so long – you miss the obvious. I was really just playing with some scripting. Essentially all I was doing was querying Active Directory for servers and seeing which ones were not responding to ping. Although the information was displayed once during the ping sweep the information was a bit tough on the eyes to read so I figured I would place them into an array and display the ones that were not responding to ping. Again, this is just something I am playing with. I'm kind of fresh to PowerShell so I'm just coming up with new ways of using PowerShell and getting some experience with it.

    Thanks again. I knew I called the array, just did not see that I commented it out.

    Happy scripting!

    Drew

  • #10323
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    If you want ideas on what to do with PowerShell there is a series starting very soon (this week I hope) on the Scripting Guy blog that is designed to help people who've learned PowerShell and now want to know how to use it.
    http://blogs.technet.com/b/heyscriptingguy/

    You might find it useful

  • #10324
    Profile photo of Drew
    Drew
    Participant

    Awesome. Thank you very much, Richard.

You must be logged in to reply to this topic.