Solved

Powershell test-connection output

Posted on 2014-01-22
2
1,072 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
A procedure for exporting installed hotfix details of remote computers using powershell
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

757 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

21 Experts available now in Live!

Get 1:1 Help Now