Ask your 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.
This topic is 5 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
lontru
Posts: 103
Last visit: Sun Apr 14, 2024 11:13 pm
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'
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
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})$'
lontru
Posts: 103
Last visit: Sun Apr 14, 2024 11:13 pm
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 146 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
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})$'
}
lontru
Posts: 103
Last visit: Sun Apr 14, 2024 11:13 pm
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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
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.
This topic is 5 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked