foreach语句显示问题
foreach statement display issue
我正在尝试通过提供文本文件中的服务器列表来从我的主机服务器中获取虚拟机列表。它有 FOREACH 循环来获取每一行并从中获取 VM 的列表。而是将所有 VM List togeter 和主机名一起显示,而不是根据每一行输入进行隔离。我尝试使用 $list 之类的变量存储它,但它没有显示任何输出。
$name= get-content C:\monitor\Serverlist\Serverlist2.txt
foreach($name1 in $name)
{
Write-Host $name1
Get-VM -computername $name1
}
我得到的输出是:
HOSTNAME1
HOSTNAME2
HOSTNAME3
HOSTNAME4
Name State CPUUsage(%) MemoryAssigned(M) Uptime Status
---- ----- ----------- ----------------- ------ ------
VM_006 Running 0 4096 19.19:46:12 Operating normally
VM_007 Running 0 4096 9.01:10:34 Operating normally
VM_008 Running 0 4096 8.23:12:15 Operating normally
VM_009 Running 0 4096 9.00:34:46 Operating normally
VM_010 Running 0 4096 9.01:53:33 Operating normally
VM_011 Running 0 4096 8.23:33:38 Operating normally
VM_012 Running 1 4096 2.22:17:51 Operating normally
VM_013 Running 0 4096 8.07:37:06 Operating normally
VM_017 Running 0 4096 08:20:28 Operating normally
VM_018 Running 0 4096 9.01:47:24 Operating normally
VM_019 Running 0 4096 9.01:33:16 Operating normally
VM_020 Running 0 4096 6.23:33:04 Operating normally
VM_021 Running 0 4096 9.01:28:32 Operating normally
VM_022 Running 0 4096 7.09:05:32 Operating normally
VM_023 Running 0 4096 8.19:35:35 Operating normally
VM_024 Running 0 4096 8.19:31:40 Operating normally
VM_025 Running 0 4096 9.00:01:48 Operating normally
所有的 VMS 和 HOSTS 都一起显示,而不是一次搜索和显示一台服务器。但是无法实现这一点,我是 powershell 的新手,知道我哪里出错了吗?
我期待如下结果:
HOSTNAME1
Name State CPUUsage(%) MemoryAssigned(M) Uptime Status
---- ----- ----------- ----------------- ------ ------
VM_006 Running 0 4096 19.19:46:12 Operating normally
VM_007 Running 0 4096 9.01:10:34 Operating normally
VM_008 Running 0 4096 8.23:12:15 Operating normally
HOSTNAME2
Name State CPUUsage(%) MemoryAssigned(M) Uptime Status
---- ----- ----------- ----------------- ------ ------
VM_009 Running 0 4096 9.00:34:46 Operating normally
VM_010 Running 0 4096 9.01:53:33 Operating normally
VM_011 Running 0 4096 8.23:33:38 Operating normally
VM_012 Running 1 4096 2.22:17:51 Operating normally
VM_013 Running 0 4096 8.07:37:06 Operating normally
显然您正在从交互式 Powershell 会话中运行脚本。这样 Powershell 的行为就像这样: Write-Host
的任何内容都直接输出到控制台,而任何未指示的内容都将转发到控制台(和 Get-VM
returns VM 对象数组,并且执行not instruct Powershell to write em to console) 累积为 script result 转发到管道,如果有的话。但后来诡计出现了any output within Powershell is piped to Out-Default
。只有 VM 对象,并且 Out-Default
在整个脚本输出中形成一个单一的 table,您可以观察到。要达到所需的行为,请将您的 Get-VM
输出直接转发到 Out-Default
,或转发到 Format-Table
。
foreach($name1 in $name)
{
Write-Host $name1
Get-VM -computername $name1 | out-default
}
我正在尝试通过提供文本文件中的服务器列表来从我的主机服务器中获取虚拟机列表。它有 FOREACH 循环来获取每一行并从中获取 VM 的列表。而是将所有 VM List togeter 和主机名一起显示,而不是根据每一行输入进行隔离。我尝试使用 $list 之类的变量存储它,但它没有显示任何输出。
$name= get-content C:\monitor\Serverlist\Serverlist2.txt
foreach($name1 in $name)
{
Write-Host $name1
Get-VM -computername $name1
}
我得到的输出是:
HOSTNAME1
HOSTNAME2
HOSTNAME3
HOSTNAME4
Name State CPUUsage(%) MemoryAssigned(M) Uptime Status
---- ----- ----------- ----------------- ------ ------
VM_006 Running 0 4096 19.19:46:12 Operating normally
VM_007 Running 0 4096 9.01:10:34 Operating normally
VM_008 Running 0 4096 8.23:12:15 Operating normally
VM_009 Running 0 4096 9.00:34:46 Operating normally
VM_010 Running 0 4096 9.01:53:33 Operating normally
VM_011 Running 0 4096 8.23:33:38 Operating normally
VM_012 Running 1 4096 2.22:17:51 Operating normally
VM_013 Running 0 4096 8.07:37:06 Operating normally
VM_017 Running 0 4096 08:20:28 Operating normally
VM_018 Running 0 4096 9.01:47:24 Operating normally
VM_019 Running 0 4096 9.01:33:16 Operating normally
VM_020 Running 0 4096 6.23:33:04 Operating normally
VM_021 Running 0 4096 9.01:28:32 Operating normally
VM_022 Running 0 4096 7.09:05:32 Operating normally
VM_023 Running 0 4096 8.19:35:35 Operating normally
VM_024 Running 0 4096 8.19:31:40 Operating normally
VM_025 Running 0 4096 9.00:01:48 Operating normally
所有的 VMS 和 HOSTS 都一起显示,而不是一次搜索和显示一台服务器。但是无法实现这一点,我是 powershell 的新手,知道我哪里出错了吗?
我期待如下结果:
HOSTNAME1
Name State CPUUsage(%) MemoryAssigned(M) Uptime Status
---- ----- ----------- ----------------- ------ ------
VM_006 Running 0 4096 19.19:46:12 Operating normally
VM_007 Running 0 4096 9.01:10:34 Operating normally
VM_008 Running 0 4096 8.23:12:15 Operating normally
HOSTNAME2
Name State CPUUsage(%) MemoryAssigned(M) Uptime Status
---- ----- ----------- ----------------- ------ ------
VM_009 Running 0 4096 9.00:34:46 Operating normally
VM_010 Running 0 4096 9.01:53:33 Operating normally
VM_011 Running 0 4096 8.23:33:38 Operating normally
VM_012 Running 1 4096 2.22:17:51 Operating normally
VM_013 Running 0 4096 8.07:37:06 Operating normally
显然您正在从交互式 Powershell 会话中运行脚本。这样 Powershell 的行为就像这样: Write-Host
的任何内容都直接输出到控制台,而任何未指示的内容都将转发到控制台(和 Get-VM
returns VM 对象数组,并且执行not instruct Powershell to write em to console) 累积为 script result 转发到管道,如果有的话。但后来诡计出现了any output within Powershell is piped to Out-Default
。只有 VM 对象,并且 Out-Default
在整个脚本输出中形成一个单一的 table,您可以观察到。要达到所需的行为,请将您的 Get-VM
输出直接转发到 Out-Default
,或转发到 Format-Table
。
foreach($name1 in $name)
{
Write-Host $name1
Get-VM -computername $name1 | out-default
}