LouisSanchez
asked on
Powershell Script needed
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oh, it *won't* add a host which just times out to the output.txt by the way...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER
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($_ .InputObje ct)
} Catch { }
If (!$?) {
"Ping request could not find host $($_.InputObject)"
$_.InputObject>> test.txt
}
Write-Verbose "$($_.InputObject) : $($Entry.AddressList)"
}
#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($_
} Catch { }
If (!$?) {
"Ping request could not find host $($_.InputObject)"
$_.InputObject>> test.txt
}
Write-Verbose "$($_.InputObject) : $($Entry.AddressList)"
}
You mean you need a unique list of machines from both files?
Chris
ASKER
Yes in other words merge, remove duplicates, process remaining machines.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.