February 28 2012

Powershell MAC address format validation for SCCM computer association

Recently I was writing a Powershell form frontend for SCCM Opearting System Deployment so technicians didn’t need to go into the MMC console to perform refresh, upgrade or new computer SCCM OSD scenarios.  In the new computer scenario, we need to do a computer association between the computer name and MAC address – which needs to be in the 00:00:00:AA:AA:AA format.

Therefore my Powershell form needs to do MAC address format validation. I took a look at this script and made a fair few modifications for my scenario. I’ve shown the relevant sections of my script below – it checks for valid MAC address hex characters and then converts 00-00-00-AA-AA-AA or 000000AAAAAA or 0000.00AA.AAAA formats to 00:00:00:AA:AA:AA format. As I said, this is part of a bigger Powershell form and I use a textbox to display messages, so you will see references to it like $textboxResults.Text or $text which you can ignore or re-use for yourself, the validation part is most important:


$MACAddress = $txtMAC.Text.toUpper()
$inParamsAddEntry.MACAddress =$MACAddress
<# This section checks for valid MAC address hex characters and then converts 00-00-00-AA-AA-AA or 000000AAAAAA or 0000.00AA.AAAA to 00:00:00:AA:AA:AA format #>
$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
{
$text = "$(get-date -format "dd/MM/yy HH:mm:ss") ERROR: Invalid MAC address specified: '$MACAddress'"
$stringBuilder.AppendLine($text)
$textboxResults.Text = $stringBuilder.ToString()
return
}
$Delimiter = ':'
$rawAddress = $MacAddress -replace 'W'
switch ($Delimiter)
{
{$_ -match ':|-'}
{
if ($MacAddress -match ":")
{
$result=$MacAddress
$text = "$(get-date -format "dd/MM/yy HH:mm:ss") MAC address $MacAddress already in correct format"
$stringBuilder.AppendLine($text)
$textboxResults.Text = $stringBuilder.ToString()
}
else
{
for ($i = 2 ; $i -le 14 ; $i += 3) {
$result = $rawAddress = $rawAddress.Insert($i, $_)}
$text = "$(get-date -format "dd/MM/yy HH:mm:ss") MAC address converted from $MacAddress to $result"
$stringBuilder.AppendLine($text)
$textboxResults.Text = $stringBuilder.ToString()
}
break
}
'.'
{
for ($i = 2 ; $i -le 14 ; $i += 3)
{
$result = $rawAddress = $rawAddress.Insert($i, $_)
}
$text = "$(get-date -format "dd/MM/yy HH:mm:ss") MAC address converted from $MacAddress to $result"
$stringBuilder.AppendLine($text)
$textboxResults.Text = $stringBuilder.ToString()
break
}
} # End switch
# Use $result from here onward for correctly formatted MAC address
$rawAddress = $txtMAC.Text.toUpper()
$MACAddress = $txtMAC.Text.toUpper()

So when I run my Powershell form, I have a few scenarios:

Invalid MAC address entered

 

Correct format MAC address entered

 

Wrong format MAC address entered

 
 
 



----------------------------------------------------------------------------
I use a maximum of one Google Ad per post to help offset some of my blog hosting costs.

----------------------------------------------------------------------------


Tags: , , , , , , , ,

Posted February 28, 2012 by danovich in category "Powershell", "SMS / SCCM

2 COMMENTS :

  1. Pingback: Configuration avancée de MDT 2013 | Déploiement MDT - WDS | IT-Connect

  2. Pingback: Configuration avancée de MDT 2013 – pozod

Leave a Reply