Solved

Powershell get computers list and ping

Posted on 2012-03-19
7
2,218 Views
Last Modified: 2012-03-20
Morning everyone,

Please can someone give a bit of instruction....

I want to get a list of servers from Active Directory (using Quest addins) and then for each server returned I want to ping it

So I have this:
================================================
$ProdServer=Get-QADComputer -Searchroot 'mydomain.com/servers' |Select-Object name

forEach ($Server in $ProdServer)

{
$Alive=Get-WMIObject win32_pingstatus -Filter "Address-'$Server'" |Select-Object statuscode if ($Alive.statuscode -eq 0)

{
Write-Host $Server is Reachable
}
Else
{
Write-Host $Server is NOT reachable
}
}
============================================

The problem is that the result I get is this:
@{Name=servername} is NOT reachable

How do I change what I'm inputting to the ping piece so that it doesn't contain the column heading (property name) of 'Name'?

Many thanks :)
0
Comment
Question by:Tommy_Cooper
  • 5
  • 2
7 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 37738065
why using wmi to ping when u can use testconnection?

Get-QADComputer -Searchroot 'mydomain.com/servers'  | foreach{
If (Test-Connection $_){ 

Write-Host $_.name is Reachable

}Else{

Write-Host $_.name is NOT reachable
}
}

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37738187
also u have a mistake in your script:

instead of:
$Alive=Get-WMIObject win32_pingstatus -Filter "Address-'$Server'" |Select-Object

should be:
Alive=Get-WMIObject win32_pingstatus -Filter "Address='$Server'" |Select-Object

here's your script updated:
$ProdServer=Get-QADComputer -Searchroot 'mydomain.com/servers' |Select-Object name
forEach ($Server in $ProdServer)
{

	$Alive = Get-WMIObject win32_pingstatus -Filter "Address='$Server'" |Select-Object statuscode 

	if ($Alive.statuscode -eq 0)
	{
		Write-Host $Server.Name is Reachable
	}
	Else
	{
		Write-Host $Server.Name is NOT reachable
	}
}

Open in new window

0
 
LVL 3

Author Comment

by:Tommy_Cooper
ID: 37741598
Hi Sedgwick,

I'm learning Powershell and wasn't aware of test-connection.  This looks good.  Kind of the same really. But I'll investigate that more: )

Sadly, running your script with test-connection gives me this result:
=========================================
Test-Connection : Testing connection to computer 'DOMAINNAME\'ServerName$' failed: The requested name is valid, but no data of the requested type was found
At line:2 char:20
+ If (Test-Connection <<<<  $_){
    + CategoryInfo          : ResourceUnavailable: (DOMAINNAME\'ServerName$:String) [Test-Connection], PingException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
 
Test-Connection : Testing connection to computer 'DOMAINNAME\'ServerName$' failed: The requested name is valid, but no data of the requested type was found
At line:2 char:20
+ If (Test-Connection <<<<  $_){
    + CategoryInfo          : ResourceUnavailable: (DOMAINNAME\'ServerName$:String) [Test-Connection], PingException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
 
Test-Connection : Testing connection to computer 'DOMAINNAME\'ServerName$' failed: The requested name is valid, but no data of the requested type was found
At line:2 char:20
+ If (Test-Connection <<<<  $_){
    + CategoryInfo          : ResourceUnavailable: (DOMAINNAME\'ServerName$:String) [Test-Connection], PingException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
 
Test-Connection : Testing connection to computer 'DOMAINNAME\'ServerName$' failed: The requested name is valid, but no data of the requested type was found
At line:2 char:20
+ If (Test-Connection <<<<  $_){
    + CategoryInfo          : ResourceUnavailable: (DOMAINNAME\'ServerName$:String) [Test-Connection], PingException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
 
ServerName is NOT reachable
===========================================

ALL instances of 'ServerName' are referring to the same server. Notice the very last entry also. This is bizarre because 'servername' is reachable.

Can you explain what's going on and what causes these multiple entries?

Many tanks
0
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 37741692
my mistake, here's an updated script:

Get-QADComputer -Searchroot 'mydomain.com/servers'  | foreach{
If (Test-Connection $_.name ){ 

Write-Host $_.name is Reachable

}Else{

Write-Host $_.name is NOT reachable
}
}
                                            

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37741699
if u still get errors when using test-connection, use your script instead with the changes i've made.
0
 
LVL 3

Author Comment

by:Tommy_Cooper
ID: 37741902
Sedgwick,

This is brilliant many thanks.  Your script is working fine.  I see that it works because we're doing 'Test-Connection' against only the property named 'name'.

Thanks awfully for your help.

Tommy
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37741995
u welcome.

cheers
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

A procedure for exporting installed hotfix details of remote computers using powershell
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

20 Experts available now in Live!

Get 1:1 Help Now