SHOW:
|
|
- or go back to the newest paste.
| 1 | [CmdletBinding()] | |
| 2 | Param ( | |
| 3 | # InputFile | |
| 4 | [Parameter(Mandatory = $true)] | |
| 5 | [string] | |
| 6 | $InputFile, | |
| 7 | ||
| 8 | # OutputFile | |
| 9 | [Parameter(Mandatory = $true)] | |
| 10 | [string] | |
| 11 | $OutputFile | |
| 12 | ) | |
| 13 | $ErrorActionPreference = 'Stop' | |
| 14 | try {
| |
| 15 | $File = Get-ChildItem $InputFile | |
| 16 | } | |
| 17 | catch {
| |
| 18 | throw $_ | |
| 19 | } | |
| 20 | ||
| 21 | if ($File.Extension -eq '.csv') {
| |
| 22 | $InputFile = Import-Csv $InputFile | |
| 23 | } | |
| 24 | else{
| |
| 25 | throw "Only CSV files are currently supported." | |
| 26 | } | |
| 27 | ||
| 28 | if (Test-Path -Path $OutputFile){
| |
| 29 | Clear-Content -Path $OutputFile | |
| 30 | } | |
| 31 | ||
| 32 | function ConvertTo-DecimalOctet{
| |
| 33 | Param ( | |
| 34 | # Octet Bits | |
| 35 | [Parameter(Mandatory=$true)] | |
| 36 | [int] | |
| 37 | $Bits | |
| 38 | ) | |
| 39 | ||
| 40 | $Octet = 0 | |
| 41 | ||
| 42 | for ($x=7;$Bits -gt 0;$Bits--){
| |
| 43 | $Octet = $Octet + [math]::Pow(2,$x) | |
| 44 | $x-- | |
| 45 | } | |
| 46 | ||
| 47 | return $Octet | |
| 48 | } | |
| 49 | function ConvertTo-DecimalMask{
| |
| 50 | Param ( | |
| 51 | [Parameter(Mandatory=$True)] | |
| 52 | [int] | |
| 53 | $CIDRNumber | |
| 54 | ) | |
| 55 | ||
| 56 | # Validate input based on numerical range | |
| 57 | if ($CIDRNumber -lt 0 -or $CIDRNumber -gt 32){
| |
| 58 | Throw "Invalid input detected. CIDRNumber must be between 0 and 32." | |
| 59 | Exit | |
| 60 | } | |
| 61 | ||
| 62 | # Define the network and host bits | |
| 63 | [int]$NetworkOctets = [Math]::DivRem($CIDRNumber,8,[ref]0) | |
| 64 | [int]$HostBits = ($CIDRNumber%8) | |
| 65 | $Octets = New-Object System.Collections.ArrayList | |
| 66 | ||
| 67 | # Fill each decimal octet for the network bit allocation | |
| 68 | if ($NetworkOctets -gt 0){
| |
| 69 | for ($NetworkOctets; $NetworkOctets -gt 0; $NetworkOctets--){
| |
| 70 | $Octets.Add(255) | Out-Null | |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | # Add the decimal octet for the host bit allocation | |
| 75 | if ($HostBits -ne 0){
| |
| 76 | $Octets.Add((ConvertTo-DecimalOctet -Bits $HostBits)) | Out-Null | |
| 77 | } | |
| 78 | ||
| 79 | ||
| 80 | # Add remaining zeros for each empty octet | |
| 81 | if ($Octets.Count -ne 5){
| |
| 82 | for ($OctCount = $Octets.Count;$OctCount -lt 4;$OctCount++){
| |
| 83 | $Octets.Add(0) | Out-Null | |
| 84 | } | |
| 85 | } | |
| 86 | ||
| 87 | # Return the octets in full decimal format | |
| 88 | return ($Octets -join '.') | |
| 89 | ||
| 90 | } | |
| 91 | function New-Route{
| |
| 92 | Param( | |
| 93 | # Destination IP | |
| 94 | [Parameter(Mandatory = $true)] | |
| 95 | [String] | |
| 96 | $DestinationIP, | |
| 97 | ||
| 98 | # Parameter help description | |
| 99 | [Parameter(Mandatory = $false)] | |
| 100 | [Int] | |
| 101 | $CIDRNumber, | |
| 102 | ||
| 103 | # Parameter help description | |
| 104 | [Parameter(Mandatory = $false)] | |
| 105 | [string] | |
| 106 | $SubnetMask, | |
| 107 | ||
| 108 | # Parameter help description | |
| 109 | [Parameter(Mandatory = $false)] | |
| 110 | [string] | |
| 111 | $NextHopIP, | |
| 112 | ||
| 113 | # Sets destination to an interface | |
| 114 | [Parameter(Mandatory = $false)] | |
| 115 | [string] | |
| 116 | $Interface | |
| 117 | ) | |
| 118 | ||
| 119 | if ($CIDRNumber -ne "") {
| |
| 120 | $Mask = ConvertTo-DecimalMask -CIDRNumber ([int]$CIDRNumber) | |
| 121 | } | |
| 122 | elseif ($SubnetMask -ne "") {
| |
| 123 | $Mask = [string]$SubnetMask | |
| 124 | } | |
| 125 | else {
| |
| 126 | Throw "No Input for 'SubnetMask' or 'CIDRNumber' detected." | |
| 127 | } | |
| 128 | ||
| 129 | if ($NextHopIP -ne "") {
| |
| 130 | $Destination = [string]$NextHopIP | |
| 131 | } | |
| 132 | elseif ($Interface -ne "") {
| |
| 133 | $Destination = [string]$Interface | |
| 134 | } | |
| 135 | else {
| |
| 136 | Throw "No Input for 'NextHopIP' or 'Interface' detected." | |
| 137 | } | |
| 138 | ||
| 139 | $Command = "ip route $DestinationIP $Mask $Destination" | |
| 140 | ||
| 141 | return $Command | |
| 142 | } | |
| 143 | ||
| 144 | foreach ($route in $InputFile) {
| |
| 145 | ||
| 146 | $routeObj = @{
| |
| 147 | 'DestinationIP' = $route.DestinationIP | |
| 148 | 'CIDRNumber' = $route.CIDRNumber | |
| 149 | 'SubnetMask' = $route.SubnetMask | |
| 150 | 'NextHopIP' = $route.NextHopIP | |
| 151 | 'Interface' = $route.Interface | |
| 152 | } | |
| 153 | ||
| 154 | New-Route @routeObj | Out-File -FilePath $OutputFile -Append | |
| 155 | } |