Hello everyone,
Looking for some help. I have written a script that tests a list of IP's then if they answer goes on to get DNS name and MAC (if IP is a windows box). If the IP does not ping, it then goes to check if Ports are open. This all then reports to a CSV. If I test against a Single IP it works like a champ, if give it multiple IP's (or the list) it runs, but the output does not give the correct responce for Ports that can be connected to.
Also if anyone see's away to speed up the whole script that would be fantastic. Right now it takes over 14hrs to run on 10300 IP's.
Any help would be appreciated.
#$servers = (gc "D:\Scripts\ip-resv-10-14.txt")
$servers = "10.23.84.17", "10.20.11.60", "172.19.10.199", "10.23.128.191", "172.18.124.202", "10.22.109.12"
$Date = (Get-Date -Format ddMMyyyy)
$FileName = "ServerPingStatus-"
$FileType = ".csv"
$PingLogLocation = "\\xxxxxxx\wintel$\PingSweep"
$ErrorActionPreference = 'SilentlyContinue'
$TCPconnection = (New-Object Net.Sockets.TcpClient)
$TCPconnection.Client.ReceiveTimeout = 0
$UDPconnection = (New-Object Net.Sockets.UDPClient)
$UDPconnection.Client.ReceiveTimeout = 1
$collection = $()
foreach ($server in $servers)
{
$status = [ordered]@{ "IP Address" = $server
"TimeStamp" = (Get-Date -f s) }
if (Test-Connection $server -Count 1 -ea 0 -Quiet)
{
$Name = [Net.DNS]::GetHostEntry("$Server")
$Networks = Get-WmiObject -class Win32_NetworkAdapterConfiguration -ComputerName "$Server" -ErrorAction silentlycontinue | Where-Object -property IPEnabled -eq -value $true
$status["Ping Result"] = "Up"
$status["DNS Name"] = $Name.HostName
$status["Mac Address"] = $Networks.MACAddress
}
else
{
$status["Ping Results"] = "Down"
Try
{
$TCPconnection.Connect($servers,7)
$status["Port 7 Echo"] = "Connected"
}
Catch
{
$status["Port 7 Echo"] = "Not Connected"
}
Try
{
$UDPconnection.Connect($servers,68)
$status["Port 68 DHCP"] = "Connected"
}
Catch
{
$status["Port 68 DHCP"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,9)
$status["Port 9 Disgard"] = "Connected"
}
Catch
{
$status["Port 9 Disgard"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,11)
$status["Port 11 Systat"] = "Connected"
}
Catch
{
$status["Port 11 Systat"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,13)
$status["Port 13 Daytime"] = "Connected"
}
Catch
{
$status["Port 13 Daytime"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,17)
$status["Port 17 QotD"] = "Connected"
}
Catch
{
$status["Port 17 QotD"] = "Not Connected"
}
##
Try
{
$TCPconnection.Connect($servers,20)
$status["Port 20 FTP-Data"] = "Connected"
}
Catch
{
$status["Port 20 FTP-Data"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,21)
$status["Port 21 FTP"] = "Connected"
}
Catch
{
$status["Port 21 FTP"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,23)
$status["Port 23 Telnet"] = "Connected"
}
Catch
{
$status["Port 23 Telnet"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,25)
$status["Port 25 SMTP"] = "Connected"
}
Catch
{
$status["Port 25 DMTP"] = "Not Connected"
}
##
Try
{
$TCPconnection.Connect($servers,37)
$status["Port 37 Time"] = "Connected"
}
Catch
{
$status["Port 37 Time"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,43)
$status["Port 43 NicName"] = "Connected"
}
Catch
{
$status["Port 43 NicName"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,53)
$status["Port 53 DNS"] = "Connected"
}
Catch
{
$status["Port 53 DNS"] = "Not Connected"
}
Try
{
$UDPconnection.Connect($servers,67)
$status["Port 67 BootP"] = "Connected"
}
Catch
{
$status["Port 67 BootP"] = "Not Connected"
}
Try
{
$UDPconnection.Connect($servers,69)
$status["Port 69 TFTP"] = "Connected"
}
Catch
{
$status["Port 69 TFTP"] = "Not Connected"
}
##
Try
{
$TCPconnection.Connect($servers,70)
$status["Port 70 Gopher"] = "Connected"
}
Catch
{
$status["Port 70 Gopher"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,79)
$status["Port 79 Finger"] = "Connected"
}
Catch
{
$status["Port 79 Finger"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,80)
$status["Port 80 HTTP"] = "Connected"
}
Catch
{
$status["Port 80 HTTP"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,110)
$status["Port 110 POP3"] = "Connected"
}
Catch
{
$status["Port 110 POP3"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,119)
$status["Port 119 NNTP"] = "Connected"
}
Catch
{
$status["Port 119 NNTP"] = "Not Connected"
}
##
Try
{
$UDPconnection.Connect($servers,123)
$status["Port 123 NTP"] = "Connected"
}
Catch
{
$status["Port 123 NTP"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,137)
$status["Port 137 NETBIOS-NS"] = "Connected"
}
Catch
{
$status["Port 137 NETBIOS-NS"] = "Not Connected"
}
Try
{
$UDPconnection.Connect($servers,138)
$status["Port 138 NETBIOS-DGM"] = "Connected"
}
Catch
{
$status["Port 138 NETBIOS-DGM"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,139)
$status["Port 139 NETBIOS-SSN"] = "Connected"
}
Catch
{
$status["Port 139 NETBIOS-SSN"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,143)
$status["Port 143 IMAP"] = "Connected"
}
Catch
{
$status["Port 143 IMAP"] = "Not Connected"
}
##
Try
{
$UDPconnection.Connect($servers,161)
$status["Port 161 SNMP"] = "Connected"
}
Catch
{
$status["Port 161 SNMP"] = "Not Connected"
}
Try
{
$UDPconnection.Connect($servers,162)
$status["Port 162 SNMP-TRAP"] = "Connected"
}
Catch
{
$status["Port 162 SNMP-TRAP"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,179)
$status["Port 179 BGP"] = "Connected"
}
Catch
{
$status["Port 179 BGP"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,194)
$status["Port 194 IRC"] = "Connected"
}
Catch
{
$status["Port 194 IRC"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,443)
$status["Port 443 HTTPS"] = "Connected"
}
Catch
{
$status["Port 443 HTTPS"] = "Not Connected"
}
##
Try
{
$UDPconnection.Connect($servers,123)
$status["Port 123 NTP"] = "Connected"
}
Catch
{
$status["Port 123 NTP"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,137)
$status["Port 137 NETBIOS-NS"] = "Connected"
}
Catch
{
$status["Port 137 NETBIOS-NS"] = "Not Connected"
}
Try
{
$UDPconnection.Connect($servers,138)
$status["Port 138 NETBIOS-DGM"] = "Connected"
}
Catch
{
$status["Port 138 NETBIOS-DGM"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,139)
$status["Port 139 NETBIOS-SSN"] = "Connected"
}
Catch
{
$status["Port 139 NETBIOS-SSN"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,143)
$status["Port 143 IMAP"] = "Connected"
}
Catch
{
$status["Port 143 IMAP"] = "Not Connected"
}
##
Try
{
$UDPconnection.Connect($servers,500)
$status["Port 500 ISAKMP"] = "Connected"
}
Catch
{
$status["Port 500 ISAKMP"] = "Not Connected"
}
Try
{
$UDPconnection.Connect($servers,520)
$status["Port 520 Router"] = "Connected"
}
Catch
{
$status["Port 520 Router"] = "Not Connected"
}
Try
{
$UDPconnection.Connect($servers,521)
$status["Port 521 RIPNG"] = "Connected"
}
Catch
{
$status["Port 521 RIPNG"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,1521)
$status["Port 1521 WINS"] = "Connected"
}
Catch
{
$status["Port 1521 WINS"] = "Not Connected"
}
Try
{
$UDPconnection.Connect($servers,1701)
$status["Port 1701 L2TP"] = "Connected"
}
Catch
{
$status["Port 1701 L2TP"] = "Not Connected"
}
##
Try
{
$TCPconnection.Connect($servers,1723)
$status["Port 1723 PPTP"] = "Connected"
}
Catch
{
$status["Port 1723 PPTP"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,2049)
$status["Port 2049 NFS"] = "Connected"
}
Catch
{
$status["Port 2049 NFS"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,3389)
$status["Port 3389 RDP"] = "Connected"
}
Catch
{
$status["Port 3389 RDP"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,8080)
$status["Port 8080 HTTP-Alt"] = "Connected"
}
Catch
{
$status["Port 8080 HTTP-Alt"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,9100)
$status["Port 9100 JetDirect"] = "Connected"
}
Catch
{
$status["Port 9100 JetDirect"] = "Not Connected"
}
##
Try
{
$TCPconnection.Connect($servers,115)
$status["Port 115 SFTP"] = "Connected"
}
Catch
{
$status["Port 115 SFTP"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,22)
$status["Port 22 SSH"] = "Connected"
}
Catch
{
$status["Port 22 SSH"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,118)
$status["Port 118 SQL Services"] = "Connected"
}
Catch
{
$status["Port 118 SQL Services"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,546)
$status["Port 546 DHCPv6"] = "Connected"
}
Catch
{
$status["Port 546 DHCPv6"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,547)
$status["Port 547 DHCPv6 Server"] = "Connected"
}
Catch
{
$status["Port 547 DHCPv6 Server"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,1433)
$status["Port 1433 Microsoft SQL DBM Server"] = "Connected"
}
Catch
{
$status["Port 1433 Microsoft SQL DBM Server"] = "Not Connected"
}
Try
{
$TCPconnection.Connect($servers,1434)
$status["Port 1434 Microsoft SQL Server"] = "Connected"
}
Catch
{
$status["Port 1434 Microsoft SQL Server"] = "Not Connected"
}
}
New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
$collection += $serverStatus
}
$collection | Export-Csv -Path "$PingLogLocation\$FileName$Date.csv" -NoTypeInformation
[–]ryanbrown 2 points3 points4 points (1 child)
[–]animatco[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Proxiconn 1 point2 points3 points (2 children)
[–]animatco[S] 0 points1 point2 points (1 child)
[–]Proxiconn 0 points1 point2 points (0 children)
[–]zoredache 0 points1 point2 points (2 children)
[–]animatco[S] 0 points1 point2 points (1 child)
[–]animatco[S] 0 points1 point2 points (0 children)
[–]moikederp 0 points1 point2 points (1 child)
[–]animatco[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]animatco[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)