Last active
June 22, 2018 15:45
-
-
Save realslacker/2d15d0aefc845bc70e1c63bdbd775012 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Reformats a MAC Address depending on the use case. | |
.DESCRIPTION | |
Takes a string or strings that should be reformated as a MAC address. | |
.PARAMETER Address | |
The MAC Address to reformat as a string | |
.PARAMETER Separator | |
What separator should be inserted, defaults to ':' | |
.EXAMPLE | |
Format-MACAddress '01-23-45-67-89-AF | |
.EXAMPLE | |
Add-CredentialToCache -Username [email protected] | |
#> | |
[CmdletBinding(DefaultParameterSetName='Uppercase')] | |
param( | |
[Parameter(Position=0, Mandatory, ValueFromPipeline)] | |
[string[]] | |
$Address, | |
[Parameter(Position=1)] | |
[string] | |
$Separator = ':', | |
[Parameter(ParameterSetName='Uppercase')] | |
[switch] | |
$Uppercase, | |
[Parameter(ParameterSetName='Lowercase')] | |
[switch] | |
$Lowercase | |
) | |
process { | |
$Address | ForEach-Object { | |
$mac = $_ -replace '[^a-f0-9]', '' | |
if ( ( $_ -match '[^a-f0-9\s:-]' ) -or ( ( $mac.Length -ne 12 ) -and ( $mac.Length -ne 28 ) ) ) { | |
Write-Warning "May be an invalid MAC Address: $_" | |
} | |
$mac = $mac -replace '[^a-f0-9]', '' | |
$mac = $mac -replace '(..(?!$))',"`$1$Separator" | |
if ( $psCmdlet.ParameterSetName -eq 'Uppercase' ) { $mac.ToUpper() } | |
else { $mac.ToLower() } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment