5

So basically what I'm trying to achieve is to get a MAC address from a text file and increment the value by one.

Been bashing my head against the Google/StackOverflow wall for a couple of hours, think there's a concept I'm just not getting.

PowerShell:

$Last_MAC_Address = (Get-Content -LiteralPath "\\UNC\Path\Last MAC Address.txt")

Write-Host ($Last_MAC_Address)
# Output: 00155DE10B73

$Next_MAC_Address = (($Last_MAC_Address | Format-Hex) + 1)
3
  • 6
    ([Int64]::Parse('00155DE10B73', 'HexNumber') + 1).ToString('X12') Commented Jul 9, 2016 at 8:07
  • Decimal: ("Good to know." | Format-Hex).Bytes.ForEach{[char]$_} -join '' Hexadecimal: ("Good to know." | Format-Hex).HexBytes.Split(' ').ForEach{[char][int]::Parse($_,'HexNumber')} -join '' Try: ("Good to know." | Format-Hex). <ctrl>+<space> to know more... Commented Jul 19, 2024 at 7:33
  • For thos that want to investigate a hexstring: 'E476F6F6420746F206B6E6F772'.ToCharArray().ForEach{if($null -eq $HxSplt){$HxSplt = $_}else{"$HxSplt$_"; $HxSplt=$null}} Commented Jul 19, 2024 at 8:17

2 Answers 2

8

This is a 3 step process, and although PetSerAl answered it in the comments as a one liner, I'll break it down slightly for posterity (and use a different class).

The first step is to get the Hex number as a decimal (mathematical base 10, not type).

The Second step is the incrementation of the decimal.

And the final step is converting it back to hexadecimal.

broken down and not a one liner this will accomplish the task at hand:

$asDecimal = [System.Convert]::ToInt64("00155DE10B73", 16)
$asDecimal++
$asHex = [System.Convert]::ToString($asDecimal, 16)
Sign up to request clarification or add additional context in comments.

3 Comments

Good answer. However Convert.ToInt64() calls Int64.Parse() internally, similarly Convert.ToString(val) calls val.ToString() internally, so technically you are using the same classes as in @PetSerAl's comment :-)
Thanks, @MathiasR.Jessen ! I never looked at the internals and that's good to know.
As @MathiasR.Jessen shows, the .parse() method is very powerfull. If you want learn more searching for .parse() or ::parse() will widen your perspective.
4

Another option is to prefix the value with 0x and cast it to an int64:

$Next_MAC_Address = ([int64]"0x$Last_MAC_Address"+1).ToString('X12')

You could also use the format operator (-f) instead of the ToString() method:

$Next_MAC_Address = '{0:X12}' -f ([int64]"0x$Last_MAC_Address"+1)

There is, however, one thing that may be worth noting. MAC addresses aren't just random 6-byte numbers without any inner structure. They actually consist of two parts. The first 3 bytes form the Organizationally Unique Identifier (OUI), a vendor-specific prefix (00-15-5D is one of the OUIs belonging to Microsoft). Only the last 3 bytes are a random number, a unique identifier for each card from the vendor identified by the OUI.

Taking that into consideration you may want to split the MAC address accordingly, e.g. like this:

$oui, $nid = $Last_MAC_Address -split '(?<=^[0-9a-f]{6})(?=[0-9a-f]{6}$)'

or like this:

$oui = $Last_MAC_Address.Substring(0, 6)
$nid = $Last_MAC_Address.Substring(6, 6)

and increment only the NIC identifier, and only if it wouldn't overflow:

if ($nid -ne 'ffffff') {
  $Next_MAC_Address = "{0}{1:X6}" -f $oui, ([int64]"0x$nid"+1)
} else {
  Write-Error 'MAC address overflow.'
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.