简体   繁体   中英

PowerShell: How to concatenate results in ForEach loop

I want to group the section in the 2nd ForEach. How do I do the equivalent of $result = $result + ...?

$clusters = "cluster1", "cluster2"
ForEach ($item in $clusters)
{
  $clusterNodes = Get-ClusterNode -Cluster $item  ;
  $clusterNodes|select Cluster,NodeName, State|Sort-Object NodeName|Format-Table -Wrap -AutoSize;

  ForEach ($vm in $clusterNodes)
  {
    $result = Get-VM -ComputerName $vm.Name |select VMName, ComputerName, PrimaryOperationalStatus, State, Path, CreationTime, Uptime, IntegrationServicesVersion,ProcessorCount, DynamicMemoryEnabled, MemoryMinimum,MemoryMaximum     |Sort-Object VMName|Format-Table -Wrap -AutoSize;
    $result
  }
}

For example, in my output, each section has its own results. I want to combine the results, sort them, count them, and display them as one result, not x times the number of $clusters.

Cluster          NodeName State
-------          -------- -----
CLUSTER1         SERVER1      Up
CLUSTER1         SERVER2      Up
CLUSTER1         SERVER3      Up
CLUSTER1         SERVER4      Up


VMName  ComputerName PrimaryOperationalStatus   State Path                              CreationTime          Uptime      IntegrationServicesVersion ProcessorCount Dynam
------  ------------ ------------------------   ----- ----                              ------------          ------      -------------------------- -------------- -----
XYZ080  SERVER1                             Ok Running C:\ClusterStorage\Volume1\XYZ080  11/15/2013 2:16:39 PM 3.13:46:52  6.2.9200.16384                          4  True
XYZ019  SERVER1                             Ok Running C:\ClusterStorage\Volume1\XYZ019  11/6/2013 10:24:58 AM 68.07:02:32 6.2.9200.20655                          2 False
XYZ021A SERVER1                             Ok Running C:\ClusterStorage\Volume1\ XYZ021A 11/1/2013 10:33:20 AM 68.07:02:01 6.2.9200.20655                          6 False


VMName  ComputerName PrimaryOperationalStatus   State Path                              CreationTime           Uptime      IntegrationServicesVersion ProcessorCount Dyna
------  ------------ ------------------------   ----- ----                              ------------           ------      -------------------------- -------------- ----
XYZ078  SERVER2                             Ok Running C:\ClusterStorage\Volume1\XYZ078  10/30/2013 11:20:05 AM 61.04:32:55 6.2.9200.20655                          4 Fals
NXYZ001 SERVER2                             Ok Running C:\ClusterStorage\volume1\NXYZ001 11/7/2013 8:54:29 AM   1.01:55:10  6.2.9200.16384                          2 Fals
ABC051  SERVER2                             Ok Running C:\ClusterStorage\volume1\ABC051  11/1/2013 2:52:24 PM   1.06:57:57  6.2.9200.20655                          4 Fals

I want it to appear like the following:

Cluster          NodeName State
-------          -------- -----
CLUSTER1         SERVER1      Up
CLUSTER1         SERVER2      Up
CLUSTER1         SERVER3      Up
CLUSTER1         SERVER4      Up


VMName  ComputerName PrimaryOperationalStatus   State Path                              CreationTime          Uptime      IntegrationServicesVersion ProcessorCount Dynam
------  ------------ ------------------------   ----- ----                              ------------          ------      -------------------------- -------------- -----
XYZ080  SERVER1                             Ok Running C:\ClusterStorage\Volume1\XYZ080  11/15/2013 2:16:39 PM 3.13:46:52  6.2.9200.16384                          4  True
XYZ019  SERVER1                             Ok Running C:\ClusterStorage\Volume1\XYZ019  11/6/2013 10:24:58 AM 68.07:02:32 6.2.9200.20655                          2 False
XYZ021A SERVER1                             Ok Running C:\ClusterStorage\Volume1\ XYZ021A 11/1/2013 10:33:20 AM 68.07:02:01 6.2.9200.20655                          6 False
XYZ078  SERVER2                             Ok Running C:\ClusterStorage\Volume1\XYZ078  10/30/2013 11:20:05 AM 61.04:32:55 6.2.9200.20655                          4 False
NXYZ001 SERVER2                             Ok Running C:\ClusterStorage\volume1\NXYZ001 11/7/2013 8:54:29 AM   1.01:55:10  6.2.9200.16384                          2 False
ABC051  SERVER2                             Ok Running C:\ClusterStorage\volume1\ABC051  11/1/2013 2:52:24 PM   1.06:57:57  6.2.9200.20655                          4 Fals

You're executing Format-Table multiple times, so you're getting a new set of formatted output each time. Instead of a foreach that contains the Format-Table filter, use a single pipeline with Format-Table at the end.

Also, there are a couple of redundancies in your code:

  • There's no need to first and then pipe to . ,然后通过管道传递给You can just list the properties you want to output in the statement. 语句中列出要输出的属性。
  • There's no need to assign the results to a variable unless you plan on reusing those results elsewhere in your code. If you just want to output the results once, omit the assignment to a variable and the results will automatically be sent to the standard output stream.

Replace the inner foreach loop with this:

$clusterNodes | %{Get-VM -ComputerName $_.Name} | Sort-Object VMName `
| Format-Table -Wrap -AutoSize VMName, ComputerName, PrimaryOperationalStatus, State, Path, CreationTime, Uptime, IntegrationServicesVersion,ProcessorCount, DynamicMemoryEnabled, MemoryMinimum,MemoryMaximum

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM