• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 933
  • Last Modified:

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.

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
bsharath
Asked:
bsharath
  • 21
  • 20
  • +1
1 Solution
 
RobGar05Commented:
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
 
arthurmnevCommented:
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
 
bsharathAuthor Commented:
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
Chris DentPowerShell DeveloperCommented:

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
 
bsharathAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:
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
 
Chris DentPowerShell DeveloperCommented:

Sorry... ranges. Nothing thinking properly.

One moment.

Chris
0
 
Chris DentPowerShell DeveloperCommented:

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
 
bsharathAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
 
bsharathAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
 
bsharathAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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

Chris
0
 
bsharathAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

Run:

nslookup TheComputerName

That's ultimately what we're doing here.

Chris
0
 
bsharathAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
 
bsharathAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
 
bsharathAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
 
bsharathAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
 
bsharathAuthor Commented:
Thanks Chris will check this out
0
 
bsharathAuthor Commented:
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
 
bsharathAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
 
Chris DentPowerShell DeveloperCommented:

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
 
bsharathAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
 
bsharathAuthor Commented:
Thanks a lot Chris all fine now.. :-)
0
 
bsharathAuthor Commented:
Chris when i scedule it
 PowerShell.exe c:\somewhere\TheScript.ps1
i get some errors
is it via powershell or Quest
0
 
Chris DentPowerShell DeveloperCommented:

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
 
bsharathAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
 
bsharathAuthor Commented:
I think this is the issue

is not digitally signed
0
 
Chris DentPowerShell DeveloperCommented:
Yeah, you can change it's behaviour there.

For the account it runs as, run:

Set-ExecutionPolicy RemoteSigned

Chris
0
 
bsharathAuthor Commented:
Still get some error.
With -NoExit also i dont see the screen
0
 
Chris DentPowerShell DeveloperCommented:

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

is missing the terminator: '.
0
 
Chris DentPowerShell DeveloperCommented:

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

Chris
0
 
bsharathAuthor Commented:
Thanks Chris now schedule also works fine.. :-)
0

Featured Post

Independent Software Vendors: 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!

  • 21
  • 20
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now