Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell Script needed

Posted on 2010-09-02
8
Medium Priority
?
475 Views
Last Modified: 2012-05-10
I would like a script that does the following. Compare two text files and find duplicates. Then take the duplicates and try to ping them. If the address can't be resolved(only this case "Ping request could not find host $hostname") then output to file. I'm looking for the exact code, I'm pretty sure it's out there, just don't have the time right now and it's an easy 500 points.
0
Comment
Question by:LouisSanchez
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 3

Assisted Solution

by:bobalob
bobalob earned 1000 total points
ID: 33585974
Ok here's one - cheap and dirty - doesn't do any exception catching, makes nasty red exception text appear on the screen, but only had 5 mins to do it. I have't done any testing with this apart from making sure it kinda works. If you get *any* exception from the ping the host will be added to the output file.

I'll do it again with DNS lookup when I get some time but this will be a start...
$file1 = gc c:\temp\file1.txt
$file2 = gc c:\temp\file2.txt

$FinalResultsArry

foreach ($server in $file1)
{
	if ($file2 -contains $server)
	{
		write-host "Duplicate-Found - Pinging $($server)..." -nonewline
		$pingObj,$pingresult = $null
		$pingObj = new-object System.Net.NetworkInformation.Ping
		$PingResult = $pingObj.send($server)
		Write-host "$($pingresult.status)"
		if ($Pingresult.status -eq $null) {$FinalResultsArry += $server}
	}
}

$FinalResultsArry | Out-File -file "c:\temp\output.txt" -enc ASCII

Open in new window

0
 
LVL 3

Expert Comment

by:bobalob
ID: 33585979
oh, it *won't* add a host which just times out to the output.txt by the way...
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 1000 total points
ID: 33586659

I've used Compare-Object to find the matching entries between the files, and System.Net.Dns to tell you if the name resolves to an IP rather than Ping.

If you set $VerbosePreference = "continue" you'll see the verbose messages as well, useful for checking it's doing something.

Chris
Compare-Object $(Get-Content File1.txt) $(Get-Content File2.txt) -ExcludeDifferent -IncludeEqual | ForEach-Object {
  $Entry = $Null
  Try {
    $Entry = [Net.Dns]::GetHostEntry($_.InputObject)
  } Catch { }
  If (!$?) {
    "Ping request could not find host $($_.InputObject)"
  }
  Write-Verbose "$($_.InputObject) : $($Entry.AddressList)"
}

Open in new window

0
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

 
LVL 71

Expert Comment

by:Chris Dent
ID: 33586778

I forgot to add file output, if you redirect at the end it'll deal with that.

That is, add this after the last curly bracket:  > OutputFile.txt

Chris
0
 

Author Comment

by:LouisSanchez
ID: 33586993
I guess I need more of a merge of duplicates not just only duplicates. This is what I have. If one of you guys could spruce it up I'll be all set.

#delete
$NULL>test.txt

#Merge Files
$file = gc file1.txt
$file += gc file2.txt
$file=$file | sort | get-unique
$file | Out-File -file "merge.txt" -enc ASCII

Compare-Object $(Get-Content merge.txt) $(Get-Content merge.txt) -IncludeEqual | ForEach-Object {
  $Entry = $Null
  Try {
    $Entry = [Net.Dns]::GetHostEntry($_.InputObject)
  } Catch { }
  If (!$?) {
    "Ping request could not find host $($_.InputObject)"
    $_.InputObject>> test.txt
  }
  Write-Verbose "$($_.InputObject) : $($Entry.AddressList)"
  }
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 33587097

You mean you need a unique list of machines from both files?

Chris
0
 

Author Comment

by:LouisSanchez
ID: 33587128
Yes in other words merge, remove duplicates, process remaining machines.
0
 
LVL 3

Assisted Solution

by:bobalob
bobalob earned 1000 total points
ID: 33587204
i guessed thats what he ment - i've plagurised your code to make something horrific but works as requested.


#Merge Files
$file = gc c:\temp\file1.txt
$file += gc c:\temp\file2.txt
$file=$file | sort | get-unique


$output = $file | ForEach-Object {
  $Entry = $Null
  Try {
    $Entry = [Net.Dns]::GetHostEntry($_)
  } Catch { }
  If (!$?) {
    "$($_) : Ping request could not find host"
    $_.InputObject>> test.txt
  } else {
  "$($_) : $($Entry.AddressList)"}
  } 

$output | out-file -file c:\temp\output.txt -enc ASCII

gc c:\temp\output.txt | ? {$_ -match "Ping request could not find host"}

Open in new window

0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Six Sigma Control Plans
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…

598 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