Author Posts

September 23, 2013 at 10:35 am

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
}

September 23, 2013 at 10:51 am

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

September 23, 2013 at 11:17 am

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

September 23, 2013 at 11:21 am

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

September 23, 2013 at 11:50 am

Awesome. Thank you very much, Richard.