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.

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

October 19 2011

How to set date format and timezone in WinPE

I had a need for an accurate timezone and specific date format during Windows PE session when using SCCM OSD.

To change timezone:

At the start of the task sequence, run a command like “reg.exe import AUS_EST.reg”
This is simply an export of HKLMSystemCurrentControlSetControlTimeZoneInformation.
In my case, the contents of AUS_EST.reg looked like:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlTimeZoneInformation]
“Bias”=dword:fffffda8
“StandardName”=”AUS Eastern Standard Time”
“StandardBias”=dword:00000000
“StandardStart”=hex:00,00,04,00,01,00,03,00,00,00,00,00,00,00,00,00
“DaylightName”=”AUS Eastern Daylight Time”
“DaylightBias”=dword:ffffffc4
“DaylightStart”=hex:00,00,0a,00,01,00,02,00,00,00,00,00,00,00,00,00
“ActiveTimeBias”=dword:fffffd6c

To change date format in WinPE:

Mount WIM using DISM
Load hive “mount”WindowsSystem32configDEFAULT into registry
Navigate to the loaded hive then Control PanelInternational
Update the values you need to in here such as sLongDate and sShortDate
Unload hive
Unmount WIM using DISM, ensuring that you use the /commit switch

For more info on editing the registry for a WIM file, see my other post http://blog.danovich.com.au/2010/03/16/edit-the-registry-inside-a-wim-file/