We help IT Professionals succeed at work.

Need help Creating PowerShell Script

225 Views
1 Endorsement
Last Modified: 2017-03-08
Greeting Experts,

    I need help creating a script that can create a list of Ip addresses from range of CIDR Notation (i.e. 0.0.0.0/22) in collum A with different subnet mask ( /22, /21, /20, /19, etc) and add the Site location related to that subnet in collum B (I.e. Site1, Site2, Site3, etc). I have attached example below.

List of Content to input into the script.
IPRange.jpg

List of IP Address with the Site Location Associated to the subnet
ScriptOutput.jpg
Comment
Watch Question

Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Long, long ago I wrote a set of functions that could take a CIDR range and turn that into a list of IP addresses. I since published it as a module on PSGallery:

https://www.powershellgallery.com/packages/Indented.Net.IP/5.0.12

Get-NetworkRange is able to turn CIDR notation into a list of IP addresses.

Chris
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Without module, and assuming a csv with headers as in your screenshot, and export to a csv:
Function Get-IP4Range([string]$CIDR) {
	$AddressDotted, [int]$NetmaskCidr = $CIDR.Split('/') | ForEach-Object {$_.Trim()}
	$arrAddress = $AddressDotted.Split('.')
	[array]::Reverse($arrAddress)
	$AddressInt = [System.BitConverter]::ToUInt32($arrAddress, 0)
	$NetmaskInt = [System.Convert]::ToUInt32(('{0}{1}' -f $('1' * $NetmaskCidr), $('0' * (32 - $NetmaskCidr))), 2)
	[uint32]$Network = $AddressInt -band $NetmaskInt
	$BroadcastInt = $Network -bor (-bnot $NetmaskInt)
	For ($IP = $Network + 1; $IP -lt $BroadcastInt; $IP++) {
		$arrIP = [System.BitConverter]::GetBytes($IP)
		[array]::Reverse($arrIP)
		$arrIP -join '.'
	}
}

Import-Csv C:\Temp\IP.csv | ForEach-Object {
	$SiteLocation = $_.'Site Location'
	Get-IP4Range -CIDR $_.'CIDR/IP Range' | Select-Object @{n='List of IP Addresses'; e={$_}}, @{n='Site Locations'; e={$SiteLocation}}
}

Open in new window

MikeSecurity

Author

Commented:
thanks for the Script, After uploading the Indented.Net.IP.psm1 module I was able to use the script you created... thanks
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
Since my code is based on the suggestion of (and the module published by) Chris Dent, he should receive some points too.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.