-
Notifications
You must be signed in to change notification settings - Fork 29
/
Format-Permutations.ps1
73 lines (63 loc) · 1.6 KB
/
Format-Permutations.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<#
.SYNOPSIS
Builds format strings using every combination of elements from multiple arrays.
.OUTPUTS
System.String list of all combinations
.FUNCTIONALITY
PowerShell
.LINK
https://social.technet.microsoft.com/wiki/contents/articles/7855.powershell-using-the-f-format-operator.aspx
.EXAMPLE
Format-Permutations.ps1 'srv-{0}-{1:00}' 'dev','test','stage','live' (1..4)
srv-dev-01
srv-dev-02
srv-dev-03
srv-dev-04
srv-test-01
srv-test-02
srv-test-03
srv-test-04
srv-stage-01
srv-stage-02
srv-stage-03
srv-stage-04
srv-live-01
srv-live-02
srv-live-03
srv-live-04
.EXAMPLE
Format-Permutations.ps1 '{0}{1}{2}{3}' (0,1) (0,1) (0,1) (0,1)
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
#>
#Requires -Version 3
[CmdletBinding()][OutputType([string[]])] Param(
# A standard .NET format string as used with the PowerShell -f operator.
[Parameter(Position=0,Mandatory=$true)][string] $Format,
<#
A list of lists to put together in all combinations (a Cartesian cross-product) and
format with the supplied format string.
#>
[Parameter(Position=1,Mandatory=$true,ValueFromRemainingArguments=$true)][ValidateNotNull()][object[][]] $InputObject
)
function Format-Permute([string]$Format,[object[][]]$NextValues,[object[]]$Values = @())
{
Write-Verbose "'$Format' -f $NextValues ($Values)"
if($NextValues.Length -eq 1) {$NextValues[0] |ForEach-Object {$Format -f ($Values+$_)}}
else {$NextValues[0] |ForEach-Object {Format-Permute $Format $NextValues[1..($NextValues.Length-1)] ($Values+$_)}}
}
Format-Permute $Format $InputObject