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.

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

January 10 2011

Converting Windows Media playlist (WPL) to iTunes / iPhone

I’ve recently got my first iPhone and assumed that all of my Windows Media playlists would never work on the iPhone. Well there seems to be a way that worked for me.

  1. Save your WMP playlist as an M3U file.  To do this you can use the “Save Playlist As” option from the list pane when you’re editing the playlist. Then change the “Save as type:” field from WPL (the default) to M3U and also specify the name and location of the file.
  2. Open Windows Explorer to the directory where you saved the M3U file.
  3. Open iTunes alongside Windows Explorer.
  4. Drag the M3U file from Windows Explorer to the PLAYLISTS section of iTunes. (If you want to create it as a new playlist be sure to drop it in the left-hand pane under the PLAYLISTS section.)  This process will then read the M3U file and create the playlist in iTunes.
  5. Connect your iPhone and drag your new playlist over to your iPhone.

 
 

Category: Geek | LEAVE A COMMENT