Hey You
It has been a while since I posted something on my blog, I do apologize. I just started with a new client and my schedule has been like a roller coaster. CRAZY!!!
Anyway; I’ve got another Powershell script that you might find interesting.
My last Powershell script was a big success on Reddit. So maybe you’ll like it too. Here’s a way to create a ServiceDesk GUI Tool.
Scenario
To this day I am still baffled by some companies that lack documentation of their system or configuration. It shouldn’t be that difficult. Documentation helps you, saves you, gets you coffee, informs you and so much more.
At a recent client of mine, they used an SMTP Relay that had a list of IP addresses that was a mile long. Without documentation, you cannot know what those IP addresses are, or are they malevolent?
What I wanted is a script that would export the complete list of IP addresses, ping them if they are alive or not, and if they are alive try to look up their FQDN name.
Did you get that? Let’s start.
Disclaimer
I am no Powershell guru nor do I claim to be. I have never had a Powershell course or whatever. This is just me making stuff up on the fly, so I you are a Powershell ultra master; here’s a laugh:
#Here I define the location that I want to export the list
$OutputCSV = "C:\Temp\IPRelay.csv"
#This is the magical export line. If you type $RelayIPList after this you'll get a
#list of IP addresses.
$SMTP = Get-WmiObject IISSmtpServerSetting -namespace "ROOT\MicrosoftIISv2" | Where-Object { $_.name -like "SmtpSVC/1" }
$RelayIPList = @()
For ($CurrentIt = 0; $CurrentIt -lt $SMTP.RelayIpList.Count; $CurrentIt += 4) {
$EndIndex = $CurrentIt + 3
$RelayIPList += $SMTP.RelayIpList[$CurrentIt..$EndIndex] -join "."
}
$RelayIPList = $RelayIPList[20..($RelayIPList.Count - 1)]
#$RelayIPList
#Here I start to ping every IP address that is exported.
#If an IP address answers it will look up the dns name.
ForEach ($RelayIP in $RelayIPList){
$pingtest = Test-Connection -ComputerName $RelayIP -Quiet -Count 1 -ErrorAction SilentlyContinue
If ($pingtest) {
#I have a split here. That is because at first I didn't want the domain name
#to show up... but then I changed my mind.
$dnsName = [System.Net.Dns]::GetHostEntry("$RelayIP").Hostname#.Split('.')[0]
$Output += "$RelayIP Online $dnsName`n"
}
Else {
$Output += "$RelayIP Not Online`n"
}
}
$Output | Out-File $OutputCSV -Encoding utf8 -Append
Conclusion
I had very little time to do this. The ‘n at the and is to give the line a BREAK. I didn’t care much about the code itself more about the result of it.
And if I have to be honest; for someone who isn’t a Powershell master… it still isn’t that good but it’s free for you to have.
I would love to hear feedback.
Thanks for reading.
Cheers,
Engin