~cloudbaseit/charms/win2012r2/drupal-iis/trunk

« back to all changes in this revision

Viewing changes to lib/Modules/Networking/IPOperations.psm1

  • Committer: Ionut Balutoiu
  • Date: 2016-12-23 12:55:39 UTC
  • Revision ID: ibalutoiu@cloudbasesolutions.com-20161223125539-j795kbynab3uflha
Added charm code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# (c) 2008-2014 Chris Dent.
 
2
#
 
3
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without
 
4
# fee is hereby granted, provided that the above copyright notice and this permission notice appear
 
5
# in all copies.
 
6
#
 
7
# THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 
8
# SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 
9
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
10
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 
11
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 
12
# OF THIS SOFTWARE.
 
13
 
 
14
 
 
15
# Original work found at: http://www.indented.co.uk/2010/01/23/powershell-subnet-math/
 
16
function ConvertTo-DecimalIP {
 
17
  <#
 
18
    .Synopsis
 
19
      Converts a Decimal IP address into a 32-bit unsigned integer.
 
20
    .Description
 
21
      ConvertTo-DecimalIP takes a decimal IP, uses a shift-like operation on each octet and returns a single UInt32 value.
 
22
    .Parameter IPAddress
 
23
      An IP Address to convert.
 
24
  #>
 
25
 
 
26
  [CmdLetBinding()]
 
27
  param(
 
28
    [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
 
29
    [Net.IPAddress]$IPAddress
 
30
  )
 
31
 
 
32
  process {
 
33
    $i = 3; $DecimalIP = 0;
 
34
    $IPAddress.GetAddressBytes() | ForEach-Object { $DecimalIP += $_ * [Math]::Pow(256, $i); $i-- }
 
35
 
 
36
    return [UInt32]$DecimalIP
 
37
  }
 
38
}
 
39
 
 
40
# Original work found at: http://www.indented.co.uk/2010/01/23/powershell-subnet-math/
 
41
function ConvertTo-MaskLength {
 
42
  <#
 
43
    .Synopsis
 
44
      Returns the length of a subnet mask.
 
45
    .Description
 
46
      ConvertTo-MaskLength accepts any IPv4 address as input, however the output value
 
47
      only makes sense when using a subnet mask.
 
48
    .Parameter SubnetMask
 
49
      A subnet mask to convert into length
 
50
  #>
 
51
 
 
52
  [CmdLetBinding()]
 
53
  param(
 
54
    [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)]
 
55
    [Alias("Mask")]
 
56
    [Net.IPAddress]$SubnetMask
 
57
  )
 
58
 
 
59
  process {
 
60
    $Bits = "$( $SubnetMask.GetAddressBytes() | ForEach-Object { [Convert]::ToString($_, 2) } )" -replace '[\s0]'
 
61
 
 
62
    return $Bits.Length
 
63
  }
 
64
}
 
65
 
 
66
# Original work found at: http://www.indented.co.uk/2010/01/23/powershell-subnet-math/
 
67
function ConvertTo-DottedDecimalIP {
 
68
  <#
 
69
    .Synopsis
 
70
      Returns a dotted decimal IP address from either an unsigned 32-bit integer or a dotted binary string.
 
71
    .Description
 
72
      ConvertTo-DottedDecimalIP uses a regular expression match on the input string to convert to an IP address.
 
73
    .Parameter IPAddress
 
74
      A string representation of an IP address from either UInt32 or dotted binary.
 
75
  #>
 
76
 
 
77
  [CmdLetBinding()]
 
78
  param(
 
79
    [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
 
80
    [String]$IPAddress
 
81
  )
 
82
 
 
83
  process {
 
84
    Switch -RegEx ($IPAddress) {
 
85
      "([01]{8}.){3}[01]{8}" {
 
86
        return [String]::Join('.', $( $IPAddress.Split('.') | ForEach-Object { [Convert]::ToUInt32($_, 2) } ))
 
87
      }
 
88
      "\d" {
 
89
        $IPAddress = [UInt32]$IPAddress
 
90
        $DottedIP = $( For ($i = 3; $i -gt -1; $i--) {
 
91
          $Remainder = $IPAddress % [Math]::Pow(256, $i)
 
92
          ($IPAddress - $Remainder) / [Math]::Pow(256, $i)
 
93
          $IPAddress = $Remainder
 
94
         } )
 
95
 
 
96
        return [String]::Join('.', $DottedIP)
 
97
      }
 
98
      default {
 
99
        Write-Error "Cannot convert this format"
 
100
      }
 
101
    }
 
102
  }
 
103
}
 
104
 
 
105
# Original work found at: http://www.indented.co.uk/2010/01/23/powershell-subnet-math/
 
106
function ConvertTo-Mask {
 
107
  <#
 
108
    .Synopsis
 
109
      Returns a dotted decimal subnet mask from a mask length.
 
110
    .Description
 
111
      ConvertTo-Mask returns a subnet mask in dotted decimal format from an integer value ranging
 
112
      between 0 and 32. ConvertTo-Mask first creates a binary string from the length, converts
 
113
      that to an unsigned 32-bit integer then calls ConvertTo-DottedDecimalIP to complete the operation.
 
114
    .Parameter MaskLength
 
115
      The number of bits which must be masked.
 
116
  #>
 
117
 
 
118
  [CmdLetBinding()]
 
119
  param(
 
120
    [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
 
121
    [Alias("Length")]
 
122
    [ValidateRange(0, 32)]
 
123
    $MaskLength
 
124
  )
 
125
 
 
126
  Process {
 
127
    return ConvertTo-DottedDecimalIP ([Convert]::ToUInt32($(("1" * $MaskLength).PadRight(32, "0")), 2))
 
128
  }
 
129
}
 
130
 
 
131
# Original work found at: http://www.indented.co.uk/2010/01/23/powershell-subnet-math/
 
132
function Get-NetworkAddress {
 
133
  <#
 
134
    .Synopsis
 
135
      Takes an IP address and subnet mask then calculates the network address for the range.
 
136
    .Description
 
137
      Get-NetworkAddress returns the network address for a subnet by performing a bitwise AND
 
138
      operation against the decimal forms of the IP address and subnet mask. Get-NetworkAddress
 
139
      expects both the IP address and subnet mask in dotted decimal format.
 
140
    .Parameter IPAddress
 
141
      Any IP address within the network range.
 
142
    .Parameter SubnetMask
 
143
      The subnet mask for the network.
 
144
  #>
 
145
 
 
146
  [CmdLetBinding()]
 
147
  param(
 
148
    [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
 
149
    [Net.IPAddress]$IPAddress,
 
150
 
 
151
    [Parameter(Mandatory = $true, Position = 1)]
 
152
    [Alias("Mask")]
 
153
    [Net.IPAddress]$SubnetMask
 
154
  )
 
155
 
 
156
  process {
 
157
    return ConvertTo-DottedDecimalIP ((ConvertTo-DecimalIP $IPAddress) -band (ConvertTo-DecimalIP $SubnetMask))
 
158
  }
 
159
}
 
160
 
 
161
Export-ModuleMember -Function * -Alias *
 
 
b'\\ No newline at end of file'