Solved

Powershell test-connection output

Posted on 2014-01-22
2
1,088 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
2 Comments
 
LVL 83

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 68

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

We are happy to announce a brand new addition to our line of acclaimed email signature management products – CodeTwo Email Signatures for Office 365.
This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…
The viewer will learn how to dynamically set the form action using jQuery.

861 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now