SDFormats.psm1

<#
    This Module contains all the formating related functions used in StackDeploy.
 
    Author: John Edward Willman V
 
    Version: 1.0
 
    Date: July 26, 2022
#>


Function Set-SDNormalizedMac {
    [CmdletBinding()]
    param (
        #MAC Address input from command
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [ValidateLength(12, 17)]
        [String]
        $mac
    )

    if ($mac -match '^(?i)[0-9a-f]{12}$') {
        $mac = (($mac -split '(..)').Where({ $_ }) -join ':')
        $mac.ToLower()
    }
    elseif ($mac -match '^(?i)([0-9a-f]{2}-?){6}$') {
        #Normalize from Dashed Format
        $mac = $mac.Replace("-", ":")
        $mac.ToLower()
    }
    elseif ($mac -match '^(?i)([0-9a-f]{4}\.?){3}$') {
        #Normalize from Cisco Format
        $mac = $mac.Replace(".", "")
        ($mac -split '(..)').Where({ $_ }) -join ':'
    }
    elseif ($mac -match '^(?i)([0-9a-f]{2}\.?){6}$') {
        #Normalize from Dashed Format
        $mac = $mac.Replace(".", ":")
        $mac.ToLower()
    }
    elseif ($mac -match '^(?i)([0-9a-f]{2}:?){6}$') {
        #Normalize from : Seperated Format
        $mac.ToLower()
    }
    else {
        return { Not a Valid Format }
    }
}
Function Set-SDMacFormat {

    [CmdletBinding()]
    param (
        #MAC Address input from command
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [ValidateLength(12, 17)]
        [String]
        $mac,
        # Format
        [Parameter()]
        [ValidateSet("Colon-Notation", "Hypen-Notation", "Period-Seperated-Notation", "Cisco")]
        [String]
        $Format
    )

    if ($Format -eq "Colon-Notation") {
        $mac = Set-SDNormalizedMac -mac $mac
        $mac
    }
    elseif ($Format -eq "Hypen-Notation") {
        $mac = Set-SDNormalizedMac -mac $mac
        $mac.Replace(":", "-")
    }
    elseif ($format -eq "Period-Seperated-Notation") {
        $mac = Set-SDNormalizedMac -mac $mac
        $mac = $mac.Replace(":", "")
        $mac = ($mac -split '(...)').Where({ $_ }) -join "."
        $mac
    }
    elseif ($Format -eq "Cisco") {
        $mac = Set-SDNormalizedMac -mac $mac
        $mac = $mac.Replace(":", "")
        $mac = ($mac -split '(....)').Where({ $_ }) -join "."
        $mac
    }
}
Function Set-SiteIPFormat {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [String]
        $site
    )

    if ($site -match "^(?i)E\d$") {
        $ip = $Matches[0]
        $ip = $ip.Replace("E", "")
    }
    if ($site -match "^(?i)E0\d$") {
        $ip = $Matches[0]
        $ip = $ip.Replace("E0", "")
    }
    if ($site -match "^(?i)E1\d$") {
        $ip = $Matches[0]
        $ip = $ip.Replace("E", "")
    }
    $ip
    
}