繁体   English   中英

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

[英]PowerShell: How to concatenate results in ForEach loop

我想在第二个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
  }
}

例如,在我的输出中,每个部分都有其自己的结果。 我想组合结果,对其进行排序,计数,并将其显示为一个结果,而不是$ clusters数量的x倍。

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

您将多次执行Format-Table ,因此每次都会获得一组新的格式化输出。 代替使用包含Format-Table过滤器的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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