Basic Idea and Challenge
PowerShell is all about automation, and creating a loop in PowerShell that creates an array of numbers is easy, add a prefix to that and hey you have an array
1..10 | ForEach-Object {"LC-SRV-" + $_}
The Magical -F
But, there is a small issue, the names of the server names that i need to be generated is kind of correct, but wrong, or “almost” great, I need the length to be the same. That can be fixed by using the magical –f.
1..10 | ForEach-Object {"LC-SRV-{0:D3}" -f $_}
The Function
So, now it looks the way I need it to be, and if you need to use it often, let’s create a function for it.
Function Create-VIAComputerName{ Param( $ComputerPrefix, $LowNumber, $HigNumber ) $Servers = $($LowNumber..$HigNumber| ForEach-Object {"$ComputerPrefix{0:D3}" -f $_}) Return $Servers } Create-VIAComputerName -ComputerPrefix LC-SRV- -LowNumber 1 -HigNumber 64
/mike
Categories: PowerShell