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.

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

February 3 2011

SCCM report to show all Apple devices (iPhone, iPod, iPad)

As part of the network discovery process, I noticed that it was picking up the iPhone and other Apple machines (Macs, iPads) that were on the corporate network either via being plugged in or on the wireless LAN.

Shortly afterwards, there was a requirement to report on all the Apple devices found in the environment. SCCM to the rescue.

After visiting the IEEE website (http://standards.ieee.org/develop/regauth/oui/public.html) and doing a search for ‘Apple’, I found the OUI (first 6 hex characters of the MAC address) that all Apple product would have (keep in mind some manufacturers subcontract component manufacture and others include registered firm OUIs in their products).

I then wrote a collection membership rule SQL query to find all the devices that have these OUIs as the start of their MAC address:

select SMS_R_SYSTEM.ResourceID, SMS_R_SYSTEM.ResourceType, SMS_R_SYSTEM.Name, SMS_R_SYSTEM.SMSUniqueIdentifier, SMS_R_SYSTEM.ResourceDomainORWorkgroup, SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.MACAddresses like "58:55:CA%" or SMS_R_System.MACAddresses like "00:03:93%" or SMS_R_System.MACAddresses like "00:03:93%" or SMS_R_System.MACAddresses like "00:05:02%" or SMS_R_System.MACAddresses like "00:0A:27%" or SMS_R_System.MACAddresses like "00:0A:95%" or SMS_R_System.MACAddresses like "00:10:FA%" or SMS_R_System.MACAddresses like "00:11:24%" or SMS_R_System.MACAddresses like "00:14:51%" or SMS_R_System.MACAddresses like "00:16:CB%" or SMS_R_System.MACAddresses like "00:17:F2%" or SMS_R_System.MACAddresses like "00:19:E3%" or SMS_R_System.MACAddresses like "00:1B:63%" or SMS_R_System.MACAddresses like "00:1C:B3%" or SMS_R_System.MACAddresses like "00:1D:4F%" or SMS_R_System.MACAddresses like "00:1E:52%" or SMS_R_System.MACAddresses like "00:1E:C2%" or SMS_R_System.MACAddresses like "00:1F:5B%" or SMS_R_System.MACAddresses like "00:1F:71%" or SMS_R_System.MACAddresses like "00:1F:F3%" or SMS_R_System.MACAddresses like "00:21:E9%" or SMS_R_System.MACAddresses like "00:22:41%" or SMS_R_System.MACAddresses like "00:23:12%" or SMS_R_System.MACAddresses like "00:23:32%" or SMS_R_System.MACAddresses like "00:23:6C%" or SMS_R_System.MACAddresses like "00:23:DF%" or SMS_R_System.MACAddresses like "00:24:36%" or SMS_R_System.MACAddresses like "00:25:00%" or SMS_R_System.MACAddresses like "00:25:4B%" or SMS_R_System.MACAddresses like "00:25:BC%" or SMS_R_System.MACAddresses like "00:26:08%" or SMS_R_System.MACAddresses like "00:26:4A%" or SMS_R_System.MACAddresses like "00:26:B0%" or SMS_R_System.MACAddresses like "00:26:BB%" or SMS_R_System.MACAddresses like "00:30:65%" or SMS_R_System.MACAddresses like "00:50:E4%" or SMS_R_System.MACAddresses like "00:A0:3F%" or SMS_R_System.MACAddresses like "00:A0:40%" or SMS_R_System.MACAddresses like "04:1E:64%" or SMS_R_System.MACAddresses like "08:00:07%" or SMS_R_System.MACAddresses like "10:93:E9%" or SMS_R_System.MACAddresses like "10:9A:DD%" or SMS_R_System.MACAddresses like "18:E7:F4%" or SMS_R_System.MACAddresses like "24:AB:81%" or SMS_R_System.MACAddresses like "28:E7:CF%" or SMS_R_System.MACAddresses like "34:15:9E%" or SMS_R_System.MACAddresses like "3C:07:54%" or SMS_R_System.MACAddresses like "40:A6:D9%" or SMS_R_System.MACAddresses like "40:D3:2D%" or SMS_R_System.MACAddresses like "44:2A:60%" or SMS_R_System.MACAddresses like "58:1F:AA%" or SMS_R_System.MACAddresses like "58:55:CA%" or SMS_R_System.MACAddresses like "58:B0:35%" or SMS_R_System.MACAddresses like "5C:59:48%" or SMS_R_System.MACAddresses like "60:33:4B%" or SMS_R_System.MACAddresses like "60:FB:42%" or SMS_R_System.MACAddresses like "64:B9:E8%" or SMS_R_System.MACAddresses like "70:CD:60%" or SMS_R_System.MACAddresses like "78:CA:39%" or SMS_R_System.MACAddresses like "7C:6D:62%" or SMS_R_System.MACAddresses like "7C:C5:37%" or SMS_R_System.MACAddresses like "88:C6:63%" or SMS_R_System.MACAddresses like "8C:58:77%" or SMS_R_System.MACAddresses like "8C:7B:9D%" or SMS_R_System.MACAddresses like "90:27:E4%" or SMS_R_System.MACAddresses like "90:84:0D%" or SMS_R_System.MACAddresses like "98:03:D8%" or SMS_R_System.MACAddresses like "A4:67:06%" or SMS_R_System.MACAddresses like "B8:FF:61%" or SMS_R_System.MACAddresses like "C4:2C:03%" or SMS_R_System.MACAddresses like "C8:2A:14%" or SMS_R_System.MACAddresses like "C8:AA:CC%" or SMS_R_System.MACAddresses like "C8:BC:C8%" or SMS_R_System.MACAddresses like "CC:08:E0%" or SMS_R_System.MACAddresses like "D4:9A:20%" or SMS_R_System.MACAddresses like "D8:30:62%" or SMS_R_System.MACAddresses like "D8:A2:5E%" or SMS_R_System.MACAddresses like "DC:2B:61%" or SMS_R_System.MACAddresses like "E0:F8:47%" or SMS_R_System.MACAddresses like "E8:06:88%" or SMS_R_System.MACAddresses like "F0:B4:79%" or SMS_R_System.MACAddresses like "F8:1E:DF%"

I then had a collection that has all the devices that have Apple MACs. Then I simply created a report that shows all the members in this collection.

This report will display all of the Apple devices discovered on the network. This may include computers that have Apple devices connected (eg iPod touch) as their MAC addresses are recorded by SCCM.

Useful.