Showing posts with label Composite formatting. Show all posts
Showing posts with label Composite formatting. Show all posts

2016/08/29

PowerShell Tip - Generate a list of Mac Addresses

While playing in my lab I needed to create a bunch of fake Devices in System Center Configuration Manager (SCCM). When creation devices you need two pieces of information, the computer name and the Mac Address of the device.

I used the following method to generate my list of mac addresses.

# First, get a list of mac address.
1..150 | ForEach-Object { '{0:X12}' -f $_ }
# Second step: Before we import we need to add a colon ":" every two characters
1..150|%{((('{0:X12}' -f $_) -split '(..)')|?{$_}) -join ":"}


What is happening here

  1. We list from the number 1 to 150
    1. 1..150

2016/08/28

PowerShell - Composite Formatting references (-f Operator)

If you play often with PowerShell you might have encountered something called "Composite Formatting". What is that ? Here is a quick example

"Welcome to {0}" -f "LazyWinAdmin.com"


Each format item takes the following form and consists of the following components:
{index[,alignment][:formatString]}
The matching braces "{" and "}" are required.


Composite Formation

The .NET Framework composite formatting feature takes a list of objects and a composite format string as input. A composite format string consists of fixed text intermixed with indexed placeholders, called format items, that correspond to the objects in the list. The formatting operation yields a result string that consists of the original fixed text intermixed with the string representation of the objects in the list.
https://msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx

##########################
# Numeric Format Strings #
##########################

# The Decimal ("D") Format Specifier
"{0:D}" -f 50999994 #50999994
"{0:D10}" -f 50999994 #0050999994
"{0:D6}" -f -1234 #-001234