PowerShell:如何在ForEach循环中连接结果

时间:2014-01-28 18:22:10

标签: powershell foreach windows-server-2012 hyper-v

我想在第二个ForEach中对该部分进行分组。我如何做相当于$ 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
  }
}

例如,在我的输出中,每个部分都有自己的结果。我希望将结果组合起来,对它们进行排序,计算它们,并将它们显示为一个结果,而不是$ cluster的数量。

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

我希望它看起来如下:

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

1 个答案:

答案 0 :(得分:1)

您多次执行格式表,因此每次都会获得一组新的格式化输出。而不是包含格式表过滤器的 foreach ,而是在末尾使用带有 Format-Table 的单个管道。

此外,您的代码中还有一些冗余:

      
  • 首先选择,然后输入 Format-Table 。您只需在 Format-Table 语句中列出要输出的属性即可。   
  • 除非您计划在代码中的其他位置重复使用这些结果,否则无需将结果分配给变量。如果您只想输出一次结果,则省略对变量的赋值,结果将自动发送到标准输出流。

用以下内容替换内部的 foreach 循环:

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