Skip to content

Instantly share code, notes, and snippets.

@dotps1
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save dotps1/709bf487ba5f9967b45f to your computer and use it in GitHub Desktop.

Select an option

Save dotps1/709bf487ba5f9967b45f to your computer and use it in GitHub Desktop.
Convert or remove the delimiter in a MAC Address String.
function Convert-MACAddressDelimiter
{
[CmdletBinding()]
[OutputType([String])]
Param
(
[Parameter(Position = 0,
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[ValidateScript({$patterns = @(
'^([0-9a-f]{2}:){5}([0-9a-f]{2})$'
'^([0-9a-f]{2}-){5}([0-9a-f]{2})$'
'^([0-9a-f]{4}.){2}([0-9a-f]{4})$'
'^([0-9a-f]{12})$'
)
if ($_ -match ($patterns -join '|')){ $true } else { throw "The argument '$_' does not match a valid MAC address format." } })]
[string]
$MacAddress,
[Parameter(ValueFromPipelineByPropertyName=$true)]
[ValidateSet(':', '-', '.', $null)]
[string]
$Delimiter = ':'
)
$rawAddress = $MacAddress -replace '\W'
switch ($Delimiter)
{
{ $_ -match ':|-' } { for ($i = 2; $i -le 14; $i += 3) { $result = $rawAddress = $rawAddress.Insert($i, $_) } break }
'.' { for ($i = 4; $i -le 9; $i += 5) {$result = $rawAddress = $rawAddress.Insert($i, $_) } break }
default { $result = $rawAddress }
}
return $result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment