Solved

Powershell test-connection output

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

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 69

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
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…

839 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