September 26, 2013

Creating a Range of IP Addresses

So, I wanted a way to iterate through a series of IP addresses given any two addresses. For example, if I wanted all the addresses between 10.0.0.1 and 10.0.0.20. I wanted to do this in the most PowerShell way possible... so I started with an IP address.

PS scripts:\> [ipaddress]"10.0.0.1"


Address            : 16777226
AddressFamily      : InterNetwork
ScopeId            : 
IsIPv6Multicast    : False
IsIPv6LinkLocal    : False
IsIPv6SiteLocal    : False
IsIPv6Teredo       : False
IsIPv4MappedToIPv6 : False
IPAddressToString  : 10.0.0.1


Well, that looks promising. It looks like they give you an Address property to work with. Let's check another IP...

PS scripts:\> [ipaddress]"10.0.0.0"


Address            : 10
AddressFamily      : InterNetwork
ScopeId            : 
IsIPv6Multicast    : False
IsIPv6LinkLocal    : False
IsIPv6SiteLocal    : False
IsIPv6Teredo       : False
IsIPv4MappedToIPv6 : False

IPAddressToString  : 10.0.0.0

Wait, that doesn't make sense, how is this address '10?'

Good question, the answer is, it is a result of a LittleEndian calculation. Doing some research, Microsoft says that the property is obsolete and should not be used. (See: http://msdn.microsoft.com/en-us/library/system.net.ipaddress.address.aspx)

So, I did some research about ranges of IP addresses in PowerShell and found some scripts to build the address list manually, but that's not what we want, we want the system to do it, I know there's a way.

I then did some research into sorting IP addresses using PowerShell, since it makes sense that someone has wanted to do this and the best way to sort IP addresses is to use their decimal equivalent, someone must have tried to convert an address into a decimal.

Then I found it, a great blog article written several years ago about creating a type property and adding it to the ipaddress class. (See: http://rkeithhill.wordpress.com/2007/06/23/sorting-ipaddresses-the-powershell-way/)

In his article, he documents that you need to create a BigEndian address property and sort on that. So, we create a .ps1xml file, containing the following: (For more about format.ps1xml files, see: http://technet.microsoft.com/en-us/library/hh847831.aspx)

Load this file by using Update-TypeData and you'll get...

PS scripts:\> [ipaddress]"10.0.0.0"

Address            : 10
AddressFamily      : InterNetwork
ScopeId            : 
IsIPv6Multicast    : False
IsIPv6LinkLocal    : False
IsIPv6SiteLocal    : False
IsIPv6Teredo       : False
IsIPv4MappedToIPv6 : False
IPAddressToString  : 10.0.0.0
BigEndianAddress   : 167772160

So, now we can do the following:

PS scripts:\> ([ipaddress]"10.0.0.1").BigEndianAddress .. ([ipaddress]"10.0.0.20").BigEndianAddress|%{([ipaddress]::Parse($_)).IPAddressToString}
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
10.0.0.5
10.0.0.6
10.0.0.7
10.0.0.8
10.0.0.9
10.0.0.10
10.0.0.11
10.0.0.12
10.0.0.13
10.0.0.14
10.0.0.15
10.0.0.16
10.0.0.17
10.0.0.18
10.0.0.19
10.0.0.20

It works on any range:

PS scripts:\> $range = ([ipaddress]"10.0.0.1").BigEndianAddress .. ([ipaddress]"10.0.10.1").BigEndianAddress|%{([ipaddress]::Parse($_)).IPAddressToString}

PS scripts:\> $range | select -First 10
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
10.0.0.5
10.0.0.6
10.0.0.7
10.0.0.8
10.0.0.9
10.0.0.10

PS scripts:\> $range | select -last 10
10.0.9.248
10.0.9.249
10.0.9.250
10.0.9.251
10.0.9.252
10.0.9.253
10.0.9.254
10.0.9.255
10.0.10.0
10.0.10.1

PS scripts:\> $range.count
2561

And that's about it.

September 18, 2013

PowerShell -- Where to start.

PowerShell

This can be an overpowering experience for most administrators used to the standard point-and-click interface. But, fear not, there are a wealth of resources out there ready to help you in your endeavours.

Below, I've listed a few very helpful websites and articles to help you get started.

September 17, 2013

Powershell Profiles

Powershell has 6 profiles that it uses for certain situations as listed below:

Description
Path
Current User, Current Host - console
$Home\[My ]Documents\WindowsPowerShell\Profile.ps1
Current User, All Hosts
$Home\[My ]Documents\Profile.ps1
All Users, Current Host - console
$PsHome\Microsoft.PowerShell_profile.ps1
All Users, All Hosts
$PsHome\Profile.ps1
Current user, Current Host - ISE
$Home\[My ]Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
 All users, Current Host - ISE
$PsHome\Microsoft.PowerShellISE_profile.ps1

From: 
Understanding the Six PowerShell Profiles via Hey, Scripting Guy! Blog

Reference:
about_Profiles