Solved

Powershell test-connection output

Posted on 2014-01-22
2
1,133 Views
Last Modified: 2014-01-24
Hi, I've just started scripting again, since my stroke and just needed some help with a simple powershell script. All I wanted is a list of servers I can connect to ($true) to be made available further down the script for me to use. What's the best way to get a list of names of the servers I can connect to?

$A = get-exchangeserver |? {$_.IsE14OrLater -eq $True -and $_.IsClientAccessServer}
$B = $a | ForEach-Object {$_.name}
 
foreach ($CasSvr in $b) {
                              # New PS snap-in
                              $TestCon = Test-Connection -ComputerName "$CasSvr" -Quiet
                              IF ($TestCon -eq $True) {
                                          Write-Host "Test Server connection to" $CasSvr is ok -ForegroundColor White
                                          } ELSE {
                                                Write-Host "Test Server connection to" $CasSvr has failed -ForegroundColor DarkRed
0
Comment
Question by:patelbg2001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 500 total points
ID: 39800641
Just collect them in an array; the following should collect the complete Exchangeserver objects (not only the names) in the array $ExchangeServersAlive:
$ExchangeServers = get-exchangeserver |? {$_.IsE14OrLater -eq $True -and $_.IsClientAccessServer}
$ExchangeServersAlive = @()
foreach ($CasSvr in $ExchangeServers) {
	IF (Test-Connection -ComputerName $CasSvr.Name -Quiet) {
		Write-Host "Test Server connection to $($CasSvr.Name) is ok" -ForegroundColor White
		$ExchangeServersAlive += $CasSvr
	} ELSE {
		Write-Host "Test Server connection to $($CasSvr) has failed" -ForegroundColor DarkRed
	}
}
$ExchangeServersAlive | % {Write-Host $_.Name -ForegroundColor Green}

Open in new window

0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39800713
Please either use
  ? {$_.IsE14OrLater -and $_.IsClientAccessServer}
or
  ? {$_.IsE14OrLater -eq $True -and $_.IsClientAccessServer -eq $True}
to be consistent in your conditions.

In oBdA's code I would only use either line 5 or line 11. Both seems a bit exaggerated, though one shows the progress, the other the result ...

Further improvements are to restrict the time to wait and the number of pings sent with Test-Connection by replacing line 4 with:
 IF (Test-Connection -ComputerName $CasSvr.Name -Count 1 -TTL 5 -Quiet) {
to wait no more than 5 seconds for a reply.

Even faster is to let do Test-Connection the lion's share of work, by stuffing in an array of computer names to contact, and evaluate the result.
$ExchangeServers = get-exchangeserver |? {$_.IsE14OrLater -and $_.IsClientAccessServer}
$ExchangeServersAlive = Test-Connection ($ExchangeServers | Select -Expand Name) -Count 1 -TTL 5 -EA SilentlyContinue | Select -Expand Address
Write-Host $($ExchangeServersAlive -join "`n") -ForegroundColor Green

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Unified and professional email signatures help maintain a consistent company brand image to the outside world. This article shows how to create an email signature in Exchange Server 2010 using a transport rule and how to overcome native limitations …
This video discusses moving either the default database or any database to a new volume.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question