Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

Script loops through list of IP addresses – need to make it run more efficiently

$
0
0

The script below goes through a list of IP addresses and resolves the hostname. It then sends a quick ping to the host to see if it is online and records the status.

<# Adapted from script found online Would like to add functionality to choose output type as well. .SYNOPSIS Gets hostname data for a list of IP addresses and outputs to console or file. .PARAMETER The IPListFile and Outfile are mandatory parameters. If you do not use them, you will be prompted for values. .EXAMPLE IPLookup.ps1 -IPListFile <path to file> -Outfile <path to output file> This code does not scale well for large amounts of IP addresses. #> param ( [Parameter(Mandatory=$true)] [string]$IPListFile, [string]$Outfile ) $ListofIPs = Get-Content $IPListFile # original code which worked fine for small lists # creates a blank array for resolved names and creates a header row #$ResultList = @() #$ResultList += 'IP_Address'+','+'Server_Name'+','+'Online' # using the .NET Generic List object, still seems to take a long time. $ResultList = New-Object System.Collections.Generic.List[System.String] $ResultList.Add('IP_Address'+','+'Server_Name'+','+'Online') # Resolve each of the addresses foreach ($ip in $ListofIPs) { $result = $null $Online = $null $currentEAP = $ErrorActionPreference $ErrorActionPreference = "silentlycontinue" # Use the DNS Static .NET class for reverse lookup # details on this found http://msdn.microsoft.com/en-us/library/ms143997.aspx $result = [system.net.dns]::GetHostEntry($ip) $Online = Test-Connection -ComputerName $ip -Count 1 -Quiet $ErrorActionPreference = $currentEAP If ($result) { $ResultList.Add($ip+','+[string]$result.HostName+','+$Online) } Else { $ResultList.Add($ip+','+'No Hostname Found'+','+$Online) } } # Output to txt file $ResultList | Out-File $Outfile # Would like to build the file on the fly and have it update using -Append # output to console #$ResultList 

So the script is fine for small lists, up to a couple hundred IP addresses. But going into a couple 1000 addresses, it gets a bit clunky. The original script wrote to an array (see the commented code at the beginning of the script) but after letting it run for almost a day, it had not finished. I figured that the array was getting to big to handle efficiently, running against 11K IP addresses. So I tried to switch it up to use the list but I think I am running into a similar problem. My thinking is to just right the results to the output file on the fly so the information is ready immediately and there is no list or array that the script is waiting on to complete. Any assistance would be great. I have similar script that works against a list of hosts as well.

submitted by /u/TheDewser
[link] [comments]

The post Script loops through list of IP addresses – need to make it run more efficiently appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Trending Articles