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"
            }
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.