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
925 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
[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
  • 21
  • 20
  • +1
43 Comments
 
LVL 1

Expert Comment

by:RobGar05
ID: 33457777
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
ID: 33457852
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
ID: 33460840
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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 71

Expert Comment

by:Chris Dent
ID: 33463616

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
ID: 33463829
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 71

Expert Comment

by:Chris Dent
ID: 33463866
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 71

Expert Comment

by:Chris Dent
ID: 33463869

Sorry... ranges. Nothing thinking properly.

One moment.

Chris
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 33463947

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
ID: 33464785
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 71

Expert Comment

by:Chris Dent
ID: 33465002

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
ID: 33465099
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 71

Expert Comment

by:Chris Dent
ID: 33465126

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
ID: 33465385
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 71

Expert Comment

by:Chris Dent
ID: 33465450

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
ID: 33466261
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 71

Expert Comment

by:Chris Dent
ID: 33466388

Run:

nslookup TheComputerName

That's ultimately what we're doing here.

Chris
0
 
LVL 11

Author Comment

by:bsharath
ID: 33466459
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 71

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 33466538

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
ID: 33467039
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 71

Expert Comment

by:Chris Dent
ID: 33467779

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
ID: 33467805
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
 
LVL 71

Expert Comment

by:Chris Dent
ID: 33467882

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
ID: 33467977
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 71

Expert Comment

by:Chris Dent
ID: 33468038

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
ID: 33468256
Thanks Chris will check this out
0
 
LVL 11

Author Comment

by:bsharath
ID: 33472213
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
ID: 33472254
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 71

Expert Comment

by:Chris Dent
ID: 33472736

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 71

Expert Comment

by:Chris Dent
ID: 33472739

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
ID: 33472766
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 71

Expert Comment

by:Chris Dent
ID: 33472771

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
ID: 33472852
Thanks a lot Chris all fine now.. :-)
0
 
LVL 11

Author Comment

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

Expert Comment

by:Chris Dent
ID: 33473013

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
ID: 33473146
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 71

Expert Comment

by:Chris Dent
ID: 33473161

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
ID: 33473200
I think this is the issue

is not digitally signed
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 33473206
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
ID: 33473264
Still get some error.
With -NoExit also i dont see the screen
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 33473316

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
ID: 33473378
The string starting
At line:1 char:43

is missing the terminator: '.
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 33473404

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
ID: 33473680
Thanks Chris now schedule also works fine.. :-)
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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…

737 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