?
Solved

Powershell test-connection output

Posted on 2014-01-22
2
Medium Priority
?
1,161 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 2000 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

Need protection from advanced malware attacks?

Look no further than WatchGuard's Total Security Suite, providing defense in depth against today's most headlining attacks like Petya 2.0 and WannaCry. Keep your organization out of the news with protection from known and unknown threats.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

764 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