Solved

how to ping a computer using Powershell

Posted on 2012-03-19
16
1,027 Views
Last Modified: 2012-03-27
I need to ping a computer using Powershell to determine whether a computer exists with a particular computer name.

I tried:

 $ping = new-object system.net.networkinformation.ping
  $ret=$ping.send($entry)

Open in new window


but it gives me an error:
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: address"
0
Comment
Question by:YZlat
[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
  • 5
  • 3
  • 3
  • +4
16 Comments
 
LVL 16

Expert Comment

by:Dale Harris
ID: 37739270
Where are you declaring $entry?
0
 
LVL 2

Expert Comment

by:ven16
ID: 37739272
why don't you try straight 'ping'  command with computer name.

ping <computer name>
0
 
LVL 7

Expert Comment

by:abdulalikhan
ID: 37739292
If you are using PowerShell 2.0 you should use Test-Connection instead of System.Net.NetworkInformation.Ping

Follow this link.

http://www.ilovepowershell.com/powershell-how-to-ping-computers/
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:ven16
ID: 37739298
$ping = new-object system.net.networkinformation.ping
  $ret=$ping.send("<computername or ip address>")

may be it will help you
http://www.ilovepowershell.com/powershell-how-to-ping-computers/
0
 
LVL 35

Author Comment

by:YZlat
ID: 37739405
ven16, if I use ping command, how do I retrieve the results?
0
 
LVL 35

Author Comment

by:YZlat
ID: 37739409
ven16, do you realize that your last comment is identical to the code I posted?
0
 
LVL 2

Expert Comment

by:ven16
ID: 37739482
yes,i do just put the computername in brackets, i just add the computer name to your code.
sorry it was again wrong code ....
0
 
LVL 35

Author Comment

by:YZlat
ID: 37739551
I tried Test-Connection and it seems to work, but what I really need is to return an error if the computer name cannot be found.
0
 
LVL 4

Expert Comment

by:R3C0N
ID: 37739871
PING-EE-WORKS.Export.txt

This is a form based on a list of computers stored in the default path in the textbox. "C:\computers.txt" Or just create a text file on the C:\ drive named computers.txt from active directory. the results will be in green for computers connected and in red for those disconnected.

-R3c0n
0
 
LVL 4

Expert Comment

by:R3C0N
ID: 37739878
Yea forgot to tell you to open it with powershell or rename it with a .ps1 extension using quotes      "MyPingScriptForm.ps1"
0
 
LVL 11

Expert Comment

by:X_layer
ID: 37739886
Try this:
Test-Connection -ComputerName ComputerNameYouWantToPing -Quiet -Count 1

Open in new window

You'll get true or false response.
0
 
LVL 4

Expert Comment

by:R3C0N
ID: 37739935
Just a quick update or step by step if your new to forms and powershell.

1.) If you exported your computers from active directory and saved only the computernames in a text file. You would then click the UPDATE button,

2.) Then highlight one of the computernames in the listbox. Click the GET INFO button and in the background test connection will run on the machine highlighted returning the results in the rich text box.

3.) You can save the results or look in the script I think I hard coded the results to a servers.rtf file on the C:\ drive. But it returns green for good and red for bad in the RTF box. I've been wanting to get this out for people to work with. Let me know how it works for everyone. Just trying to give back to the community.

-R3c0n
0
 
LVL 35

Author Comment

by:YZlat
ID: 37742416
That's not really what I need. I am already getting true or false response whether the computer is online. I want to get more detailed response as to why computer is unavailable. What I need is to find out which ones are ghost computers that do not exist. The name might be in AD but the machine does not exist. That's what I am looking for. Can anyone help?
0
 
LVL 5

Expert Comment

by:kollenh
ID: 37743429
Off the top of my head, sounds like you want to query AD for some properties of the object.  If you have the Quest Active Directory tools installed, use:
get-qadcomputer {name} | Select whenChanged

Open in new window

 That will (essentially) tell you the last time to computer talked with AD.

If you don't have the QAD tools, I suggest you go download them.  :)
0
 
LVL 35

Author Comment

by:YZlat
ID: 37758582
But that does not tell me whether the machine exists
0
 
LVL 5

Accepted Solution

by:
kollenh earned 250 total points
ID: 37766703
You won't be able to make a single check that a machine "exists" - that information just isn't able to be stored anywhere.  The best that is available to you is checking some attributes and making your own determination.

Since there is a domain policy specifying how often computers must change their password, use that as a basis for a 'maximum number of days since last communication'.  Many people use 90 days as their "stale" value; if it's been longer than 90 days since a computer updated/talked with AD then it is considered a stale object and is either deleted outright or disabled, depending on their internal policies.

You can easily use Powershell + Quest (QAD) tools to automate such a process or just build a report.  This will show you a list of computers that last changed their password more than 90 days ago, along with the exact date of the last password change and when they were joined to AD:
 get-qadcomputer | ? {$_.whenchanged -lt ((get-date).addDays(-90))} | sort whenChanged | select Name,whenChanged,whenCreated

Open in new window


Assuming you're running Powershell 2.0 (if not, why? go get it), you could include your ping test as a layer of validation, although it will drastically slow down the script:
get-qadcomputer | `
  Where-Object {$_.whenChanged -lt ((Get-Date).AddDays(-90))} | `
    ForEach-Object {
    	$computername = $_.name
    	$changed = $_.whenchanged
    	$created = $_.whenCreated
    	if (Test-Connection $computername -Count 2 -ErrorAction SilentlyContinue) {
    		$cs = gwmi Win32_OperatingSystem -computername $computername
    		if ($cs.csname -eq $computername) {
    		  Write-Host $computername "last updated > 90 days but is pingable"
    		} else {
    		  Write-Host $computername "is pingable but actual computername is" $($cs.csname)
    		}
    	} else {
    		Write-Host $computername "is a stale object"
    	}
    }

Open in new window


If you have a number of computers that do not directly connect to your network - such as remote/VPN users - then you may have trouble weeding through which systems are truly gone and those that are just not connected.

I hope this helps you out.  Let us know if you need/want more information.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

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.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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