Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [CmdletBinding()]
- Param (
- # InputFile
- [Parameter(Mandatory = $true)]
- [string]
- $InputFile,
- # OutputFile
- [Parameter(Mandatory = $true)]
- [string]
- $OutputFile
- )
- $ErrorActionPreference = 'Stop'
- try {
- $File = Get-ChildItem $InputFile
- }
- catch {
- throw $_
- }
- if ($File.Extension -eq '.csv') {
- $InputFile = Import-Csv $InputFile
- }
- else{
- throw "Only CSV files are currently supported."
- }
- if (Test-Path -Path $OutputFile){
- Clear-Content -Path $OutputFile
- }
- function ConvertTo-DecimalOctet{
- Param (
- # Octet Bits
- [Parameter(Mandatory=$true)]
- [int]
- $Bits
- )
- $Octet = 0
- for ($x=7;$Bits -gt 0;$Bits--){
- $Octet = $Octet + [math]::Pow(2,$x)
- $x--
- }
- return $Octet
- }
- function ConvertTo-DecimalMask{
- Param (
- [Parameter(Mandatory=$True)]
- [int]
- $CIDRNumber
- )
- # Validate input based on numerical range
- if ($CIDRNumber -lt 0 -or $CIDRNumber -gt 32){
- Throw "Invalid input detected. CIDRNumber must be between 0 and 32."
- Exit
- }
- # Define the network and host bits
- [int]$NetworkOctets = [Math]::DivRem($CIDRNumber,8,[ref]0)
- [int]$HostBits = ($CIDRNumber%8)
- $Octets = New-Object System.Collections.ArrayList
- # Fill each decimal octet for the network bit allocation
- if ($NetworkOctets -gt 0){
- for ($NetworkOctets; $NetworkOctets -gt 0; $NetworkOctets--){
- $Octets.Add(255) | Out-Null
- }
- }
- # Add the decimal octet for the host bit allocation
- if ($HostBits -ne 0){
- $Octets.Add((ConvertTo-DecimalOctet -Bits $HostBits)) | Out-Null
- }
- # Add remaining zeros for each empty octet
- if ($Octets.Count -ne 5){
- for ($OctCount = $Octets.Count;$OctCount -lt 4;$OctCount++){
- $Octets.Add(0) | Out-Null
- }
- }
- # Return the octets in full decimal format
- return ($Octets -join '.')
- }
- function New-Route{
- Param(
- # Destination IP
- [Parameter(Mandatory = $true)]
- [String]
- $DestinationIP,
- # Parameter help description
- [Parameter(Mandatory = $false)]
- [Int]
- $CIDRNumber,
- # Parameter help description
- [Parameter(Mandatory = $false)]
- [string]
- $SubnetMask,
- # Parameter help description
- [Parameter(Mandatory = $false)]
- [string]
- $NextHopIP,
- # Sets destination to an interface
- [Parameter(Mandatory = $false)]
- [string]
- $Interface
- )
- if ($CIDRNumber -ne "") {
- $Mask = ConvertTo-DecimalMask -CIDRNumber ([int]$CIDRNumber)
- }
- elseif ($SubnetMask -ne "") {
- $Mask = [string]$SubnetMask
- }
- else {
- Throw "No Input for 'SubnetMask' or 'CIDRNumber' detected."
- }
- if ($NextHopIP -ne "") {
- $Destination = [string]$NextHopIP
- }
- elseif ($Interface -ne "") {
- $Destination = [string]$Interface
- }
- else {
- Throw "No Input for 'NextHopIP' or 'Interface' detected."
- }
- $Command = "ip route $DestinationIP $Mask $Destination"
- return $Command
- }
- foreach ($route in $InputFile) {
- $routeObj = @{
- 'DestinationIP' = $route.DestinationIP
- 'CIDRNumber' = $route.CIDRNumber
- 'SubnetMask' = $route.SubnetMask
- 'NextHopIP' = $route.NextHopIP
- 'Interface' = $route.Interface
- }
- New-Route @routeObj | Out-File -FilePath $OutputFile -Append
- }
Add Comment
Please, Sign In to add comment