Working with MAC address

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
lontru
Posts: 58
Joined: Fri Aug 18, 2017 4:36 am

Working with MAC address

Post by lontru » Tue Mar 26, 2019 2:42 am

Hi

Im working on a tool where i need the mac address in this syntax xx:xx:xx:xx:xx:xx

I have below code to validate diff syntax but want it xx:xx:xx:xx:xx:xx

so how to do this ?

Code: Select all

function Validate-MAC ($MACAddress) {
	$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 ($MACAddress -match ($patterns -join '|')) {$true} else {$false}
}

Validate-MAC -MACAddress '3D:F2:C9:A6:B3:00'

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Working with MAC address

Post by jvierra » Tue Mar 26, 2019 3:30 am


$MACAddress = '12:12:12:12:12:12'
$MACAddress -match '^([0-9a-f]{2}:){5}([0-9a-f]{2})$'

User avatar
lontru
Posts: 58
Joined: Fri Aug 18, 2017 4:36 am

Re: Working with MAC address

Post by lontru » Tue Mar 26, 2019 3:41 am

created below function to return XX:XX:XX:XX:XX:XX if syntax is a valid mac

Code: Select all

Validate-MAC -MACAddress '01:23:45:67:89:ab'
Validate-MAC -MACAddress '3D:F2:C9:A6:B3:00'

Validate-MAC -MACAddress '001e.1324.683f'
Validate-MAC -MACAddress '001E.1324.683F'

Validate-MAC -MACAddress '00-25-86-DC-FD-3B'
Validate-MAC -MACAddress '00-25-86-dc-fd-3b'

Validate-MAC -MACAddress '002586DCFD3B'
Validate-MAC -MACAddress '002586dcfd3b'
The function:

Code: Select all

function Validate-MAC ($MACAddress)
{
	$patterns = @(
		'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$'
		'^([0-9A-Fa-f]{4}.){2}([0-9A-Fa-f]{4})$'
		'^([0-9A-Fa-f]{12})$')
	if ($MACAddress -match ($patterns -join '|'))
	{
		Write-Host "Valid MAC: $true" -ForegroundColor Green
        $MACAddress = $MACAddress.ToUpper()
		if (!($MACAddress -match "^([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})$"))
		{
			Write-Host "wrong formatting: $MACAddress" -ForegroundColor Red
			# XX-XX-XX-XX-XX-XX
			if ($MACAddress -match "^([0-9A-Fa-f]{2}[-]){5}([0-9A-Fa-f]{2})$")
			{
				$MACAddress = $MACAddress -replace ('-', ':')
				return $MACAddress
			}
            # XXXX.XXXX.XXXX
			if ($MACAddress -match "^([0-9A-Fa-f]{4}.){2}([0-9A-Fa-f]{4})$")
			{
                $MACAddress = $MACAddress -replace ('\.', '')
                $MACAddress = $MACAddress -replace '..(?!$)', '$&:'
                return $MACAddress
			}
            # XXXXXXXXXXXX
            if ($MACAddress -match "([0-9A-Fa-f]{2}){5}([0-9A-Fa-f]{2})$")
            {
                $MACAddress = $MACAddress.Insert(2,':').Insert(5, ':').Insert(8, ':').Insert(11, ':').Insert(14, ':')
                return $MACAddress
            }
		}
		else
		{
			return $MACAddress
		}
	}
	else
	{
		$false
		Write-Host "MACAddress not in correct format: XX:XX:XX:XX:XX:XX'" -ForegroundColor Red
	}
}
Attachments
Validate-MAC.ps1
(1.72 KiB) Downloaded 20 times

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Working with MAC address

Post by jvierra » Tue Mar 26, 2019 7:07 am

You don't need a function. Just use the two lime I posted. It returns true/false.

If you want it in a function then do this:

Code: Select all

function Validate-MAC ($MACAddress){
     $MACAddress -match '^([0-9a-f]{2}:){5}([0-9a-f]{2})$'
}

User avatar
lontru
Posts: 58
Joined: Fri Aug 18, 2017 4:36 am

Re: Working with MAC address

Post by lontru » Mon Apr 01, 2019 1:49 am

I needed to validate diff way to type a mac address socreated the function for that purpose.

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Working with MAC address

Post by jvierra » Mon Apr 01, 2019 9:15 am

There are only two valid frms of a MAC address:

00-25-86-DC-FD-3B
00:25:86:DC:FD:3B


The other forms are numeric or byte array.

Locked