Search code examples
powershellcurly-bracesbracesbrace-expansion

PowerShell multiple sets of brace expansion?


Using the stock PS in W10 on a corporate desktop.

$PSVersionTable

Name                           Value                                                                                                                                  
----                           -----                                                                                                                                  
PSVersion                      5.1.19041.4291                                                                                                                         
PSEdition                      Desktop                                                                                                                                
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                
BuildVersion                   10.0.19041.4291                                                                                                                        
CLRVersion                     4.0.30319.42000                                                                                                                        
WSManStackVersion              3.0                                                                                                                                    
PSRemotingProtocolVersion      2.3                                                                                                                                    
SerializationVersion           1.1.0.1 

In bash, one can easily expand multiple sets of variables at one time to make a long list. This one, for example, generate 18 elements:

$ for s in FISP{CC,CDS,TAP}PGS{302,405}{a,b,c}; do ping -c3 $s; done
[bunch of ping output]

How does one do this in PowerShell? The best I've come up with is this, which isn't so short.

foreach ($app in "CC","CDS","TAP") 
{ 
    foreach ($client in 302,405) 
    { 
        foreach ($node in "a","b","c") 
        { 
            $s="FISP${app}PGS${client}${Node}"
            ping $s
        } 
    } 
}

Solution

  • First build the array of names, then do the Test-Connection as suggested ( in the comments)

    $ip=@()
    foreach ($app in "CC","CDS","TAP") 
    { 
        foreach ($client in 302,405) 
        { 
            foreach ($node in "a","b","c") 
            { 
                $ip += "FISP${app}PGS${client}${Node}"
            } 
        } 
    }
    $ip | %{ $i=Test-Connection $_ -Count 1 -Quiet; $_, $i -join ' '}
    

    output (on my system):

    FISPCCPGS302a False
    FISPCCPGS302b False
    FISPCCPGS302c False
    FISPCCPGS405a False
    FISPCCPGS405b False
    FISPCCPGS405c False
    ....