Solved

Ping every machine in a OU called 'Computers' and if ping reply or time out with 192 ip series then put the computer name and ip into a txt file.

Posted on 2010-08-17
43
919 Views
Last Modified: 2012-06-22
Hi,

Ping every machine in a OU called 'Computers' and if ping reply or time out with 192 ip series then put the computer name and ip into a txt file.

Can anyone help me with this.

its basically if new computers are added for a region need to know to move them to an OU.

Powershell or Vbs script.

regards
Sharath
0
Comment
Question by:bsharath
  • 21
  • 20
  • +1
43 Comments
 
LVL 1

Expert Comment

by:RobGar05
Comment Utility
computers is where newly added computers wind up by default.  If you move them out of this OU they will no longer be there. So can't you just monitor the computer ou?
0
 
LVL 3

Expert Comment

by:arthurmnev
Comment Utility
If this is a one time operation , try AngryIP scanner (http://www.angryip.org/w/Download) -- the small tool that works wonders, it is multithreaded so you can scan a class C in about 3 seconds... achieving the same through VBS is hard if not impossible.

to give you a quick idea of how to do ping through vbs:

Option Explicit

Dim Shell, strCommand, strHost, ReturnCode

'Host or IP to ping
strHost = "192.168.1.1"

'Create shell object
Set Shell = wscript.createObject("wscript.shell")

'What to run in command line
strCommand = "ping -n 1 -w 300 " & strHost

'Run command and get return code
ReturnCode = Shell.Run(strCommand, 0, True)

'0 = pingable, 1 = no response
If ReturnCode = 0 Then
wscript.echo strHost & " is pingable"
Else
wscript.echo strHost & " is not pingable"
End If

do that in network loop and all is good, If you need to, I can try to write one later on
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I will not move them until i find they are from 192 range ip .
If they are found to be my country computer objects only then will i move...
I will need a script that will ping and find the ip's and if 192 log them into a txt file in the C:\ping_Results.txt
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

Using Quest's CmdLets. I've substituted Ping for a DNS lookup since you don't care about the result of ping, only the address it comes back with.

Chris
Get-QADComputer -SearchRoot "domain.com/Computers" -SizeLimit 0 | 
  Select-Object Name, @{n='Address';e={ [Net.Dns]::GetHostEntry($_.Name).AddressList }} |
  Where-Object { $_.Address -Match '^192\.' } |
  Export-Csv Results.csv

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Thanks Chris works fine
I missed some ranges
Can i get results saved only if in this ranges

192.168.1.0 to 192.168.10.254
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility
Yes, certainly.

This regular expression controls what we see:

'^192\.168\.10?\.'

We're asking matches starting (^) with 192.168.1.<anything> or 192.168.10.<anything>. The question mark makes the 0 optional in the string. The slashes before . escapes it since . represents any character in a regular expression.

Chris
Get-QADComputer -SearchRoot "domain.com/Computers" -SizeLimit 0 | 
  Select-Object Name, @{n='Address';e={ [Net.Dns]::GetHostEntry($_.Name).AddressList }} |
  Where-Object { $_.Address -Match '^192\.168\.10?\.' } |
  Export-Csv Results.csv

Open in new window

0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

Sorry... ranges. Nothing thinking properly.

One moment.

Chris
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

This is a little messy. We could do proper IP math, but the smallest subnet including those you've specified is 192.168.0.0 to 192.168.15.255.

Chris
Get-QADComputer -SearchRoot "domain.com/Computers" -SizeLimit 0 | 
  Select-Object Name, @{n='Address';e={ [Net.Dns]::GetHostEntry($_.Name).AddressList }} |
  Where-Object { $_.Address -Match '^192\.168\.([1..9]|10|11)\.' } |
  Export-Csv Results.csv

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
i did not get any machines listed...

192.168 is fine what should i do for a ip range as this

192.168.176.1 to 192.168.187.254
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

It might be better to approach it using IP Ranges instead of a regular expression.

I mean that we could quite easily return all machines in this subnet:

192.168.176.0 to 192.168.191.255 (192.168.176.0 255.255.240.0)

Would that work? It'll be more reliable than playing with regular expressions in this case.

Chris
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I will need within this range

192.168.176.1 to 192.168.187.254
As before or after this range even 1 ip will consider to a different Country
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

Please try this?

Chris
Get-QADComputer -SearchRoot "domain.com/Computers" -SizeLimit 0 | 
  Select-Object Name, @{n='Address';e={ [Net.Dns]::GetHostEntry($_.Name).AddressList }} |
  Where-Object { $_.Address -Match '^192\.168\.1((7[6..9])|(8[0..7]))\.' } |
  Export-Csv Results.csv

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Found a few
but a ip
192.168.177.15 the object is there and destination not reachable and that was not captured in the log.
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

I don't do anything to determine whether the host is reachable or not. Do you need to know the ping status?

Chris
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I dont want the ping status but that ip did not be retrieved. That machine which had that Ip which had to be logged . The script did not log it.
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

Run:

nslookup TheComputerName

That's ultimately what we're doing here.

Chris
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Ok for nslookup i get
192.168.177.15
Now the script checks the whole range in this right

192.168.176.1 to 192.168.187.254
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
Comment Utility

Ack sorry, syntax errors, should be fixed here.

Chris
Get-QADComputer -SearchRoot "domain.com/Computers" -SizeLimit 0 | 
  Select-Object Name, @{n='Address';e={ [Net.Dns]::GetHostEntry($_.Name).AddressList }} |
  Where-Object { $_.Address -Match '^192\.168\.1((7[6-9])|(8[0-7]))\.' } |
  Export-Csv Results.csv

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Thanks Chris works fine.
Can we make this code to run as scheduled task and email via Outlook without any smtp only when there is a name as output. I mean a computer found.
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

Sure.  Attachment or inline?

This is the attachment version. You need:

1. Quest CmdLets installed
2. PowerShell 2 installed
3. A mail server
4. SMTP open (TCP Port 25), check AV :)

HTH

Chris
Add-PsSnapIn Quest.ActiveRoles.ADManagement

$Results = Get-QADComputer -SearchRoot "domain.com/Computers" -SizeLimit 0 | 
  Select-Object Name, @{n='Address';e={ [Net.Dns]::GetHostEntry($_.Name).AddressList }} |
  Where-Object { $_.Address -Match '^192\.168\.1((7[6-9])|(8[0-7]))\.' }

If ($Results) {
  $Results | Export-Csv Results.csv -NoTypeInformation
  Send-MailMessage -To "to@domain.com" -From "from@domain.com" -SmtpServer "mailserver.domain.com" `
    -Attachments Results.csv
}

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Thanks Chris
But i should be able to email without a SMTP addtess. Using the configured outlook i want the email to be delivered.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

I can't help you mailing through the Outlook application, SMTP is far simpler and all I will ever use for this kind of operation.

I'm sure we have had this conversion about e-mailing before now I think about it, you have SMTP locked down don't you?

Chris
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
ya Chris its blocked
I have one server that i have SMTP opened.
Can i route emails from 1 machine via that machines.
i cannot install powershell and quest there. is there a possibility
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

Not from me I'm afraid. I haven't studied the Outlook COM object at all, it's not really something that has ever interested me.

Could you put up a web server and serve the output from these scripts to a limited audience?

If you go down that road you might even consider PoshBoard:

http://poshboard.codeplex.com/

It's something I've been planning to use internally to present reports of much the same nature as this one.

Chris
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Thanks Chris will check this out
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Chris for the code here
ID: 33467779
It asks me for the subject to be entered. Can that be coded.
Can i use this as a scehuled task. by saving in a ps1 file?
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Chris i will schedule this 2 or 3 times a day. So can we email only if atleast one machine found.
If nothing found do not email
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

This adds the subject, and it only mails if it finds machines.

Chris
Add-PsSnapIn Quest.ActiveRoles.ADManagement

$Results = Get-QADComputer -SearchRoot "domain.com/Computers" -SizeLimit 0 | 
  Select-Object Name, @{n='Address';e={ [Net.Dns]::GetHostEntry($_.Name).AddressList }} |
  Where-Object { $_.Address -Match '^192\.168\.1((7[6-9])|(8[0-7]))\.' }

If ($Results) {
  $Results | Export-Csv Results.csv -NoTypeInformation
  Send-MailMessage -To "to@domain.com" -From "from@domain.com" -Subject "Computer Report" `
    -SmtpServer "mailserver.domain.com" -Attachments Results.csv
}

Open in new window

0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

Missed a bit. Yes you can save it as a .ps1 file, and you would call it like this in the scheduled task:

PowerShell.exe c:\somewhere\TheScript.ps1

Chris
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Thanks
I get this in the first lines

Add-PSSnapin : Cannot add Windows PowerShell snap-in Quest.ActiveRoles.ADManagement because it is already added. Verify
 the name of the snap-in and try again.
At line:1 char:13
+ Add-PsSnapIn <<<<  Quest.ActiveRoles.ADManagement
    + CategoryInfo          : InvalidArgument: (Quest.ActiveRoles.ADManagement:String) [Add-PSSnapin], PSArgumentExcep
   tion
    + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

This will stop that error :)

Chris
Add-PsSnapIn Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue

$Results = Get-QADComputer -SearchRoot "domain.com/Computers" -SizeLimit 0 | 
  Select-Object Name, @{n='Address';e={ [Net.Dns]::GetHostEntry($_.Name).AddressList }} |
  Where-Object { $_.Address -Match '^192\.168\.1((7[6-9])|(8[0-7]))\.' }

If ($Results) {
  $Results | Export-Csv Results.csv -NoTypeInformation
  Send-MailMessage -To "to@domain.com" -From "from@domain.com" -Subject "Computer Report" `
    -SmtpServer "mailserver.domain.com" -Attachments Results.csv
}

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Thanks a lot Chris all fine now.. :-)
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Chris when i scedule it
 PowerShell.exe c:\somewhere\TheScript.ps1
i get some errors
is it via powershell or Quest
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

What errors?

You might check that execution of scripts is allowed for the account executing the task?

I normally start up PowerShell using Run As with the schedule account to make sure it is capable of running it.

Chris
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I have the line as this

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe  D:\Run_Via_ScheduleTask\Email_Comp's.ps1

I get a flash red screen and closes immediately
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

With the quote? That needs to go away if so.

Otherwise run:

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe  D:\Run_Via_ScheduleTask\Email_Comp's.ps1 -NoExit

That should let us capture the error message.

Chris
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I think this is the issue

is not digitally signed
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility
Yeah, you can change it's behaviour there.

For the account it runs as, run:

Set-ExecutionPolicy RemoteSigned

Chris
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Still get some error.
With -NoExit also i dont see the screen
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

Hmm try:

powershell.exe -NoExit -Command "D:\Run_Via_ScheduleTask\Email_Comp's.ps1"

Need to prevent it closing to capture the error message. I assume you've changed the execution policy now?

Chris
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
The string starting
At line:1 char:43

is missing the terminator: '.
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

You must lose the apostrophe from the file name if you really have one. It's not going to help :)

Chris
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Thanks Chris now schedule also works fine.. :-)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

763 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

9 Experts available now in Live!

Get 1:1 Help Now