我想在第二个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
答案 0 :(得分:1)
您多次执行格式表,因此每次都会获得一组新的格式化输出。而不是包含格式表过滤器的 foreach ,而是在末尾使用带有 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