Powershell – Parse SpamHaus.org Drop List Text File from CR to CR/LF

This little script gets a text file from SpamHaus.orgDrop List text file, convert it to a test file windows can read (not a Linux file that is the source), and extract the Network Subnets out.

#Declare header names for CSV file to be imported
$Header = @("NetworkSubnet","Label")
#Get Web Page
$WebpageResult = Invoke-WebRequest  -uri "http://www.spamhaus.org/drop/drop.txt"
#get the content out of the webpage
$WebPageContent = $WebpageResult.content
#Add Carriage return to each line so Powershell can read it properly
$WebPageContent |ForEach-Object { $_.Replace("`n","`r`n") } |Set-Content c:\temp\BritV8.txt
# Import the file back in as a CSV with a delimiter of ";" and skip the first 4 lines
$CSVFile = import-csv -path c:\temp\BritV8.txt -Delimiter ";" -header $Header |select -skip 4

#create/reset result file
$Null |Set-Content c:\temp\Result-BritV8.txt
#Export out only the NetworkSubnet to a file and trim trailing blanks off
foreach ($item in $CSVFile)
{
$item.NetworkSubnet.trim() |add-Content c:\temp\Result-BritV8.txt
}
#job done

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.