Skip to content

PowerShell Function: Fix-MAC

function Fix-MAC
{
    [CmdletBinding(DefaultParameterSetName=’Upper’)]
    [OutputType([string])]
    Param
    (
        # MAC Address
        [Parameter(Mandatory=$true, 
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true, 
                   ValueFromRemainingArguments=$false, 
                   Position=0)]
        [Parameter(ParameterSetName='Upper')]
        [Parameter(ParameterSetName='Lower')]
        [Parameter(ParameterSetName='IOS')]
        [string[]]$MACS,
        
        [Parameter(Mandatory=$False,
                   ParameterSetName=’Lower’)]
        [switch]$Lower,
        
        [Parameter(Mandatory=$False,
        ParameterSetName=’IOS’)]
        [switch]$IOS
    )
    Process
    {
        foreach ($MAC in $MACS)
        {
            if ($MAC -match "^([a-f0-9]{2})[.-: ]?([a-f0-9]{2})[.-: ]?([a-f0-9]{2})[.-: ]?([a-f0-9]{2})[.-: ]?([a-f0-9]{2})[.-: ]?([a-f0-9]{2})$")
            {
                if ($IOS)
                {
                    ("{0}{1}.{2}{3}.{4}{5}" -f $matches[1..6]).ToLower()
                }
                elseif ($Lower)
                {
                    ("{0}:{1}:{2}:{3}:{4}:{5}" -f $matches[1..6]).ToLower()
                }
                else
                {
                    ("{0}:{1}:{2}:{3}:{4}:{5}" -f $matches[1..6]).ToUpper()
                }
            }
            else
            {
                "Could not parse $MAC"
            }
        }
    }
}
Published inTech

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *