我正在尝试通过从文本文件提供服务器列表来从我的主机服务器获取VM列表。它有FOREACH循环来获取每一行并从中获取VM列表。相反,它将所有VM List togeter和Hostnames一起显示,而不是根据每行输入进行隔离。我尝试使用像$ 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正在一起显示,而不是一次搜索和显示一台服务器。但是,无法解决这个问题,我是新手,知道我哪里出错了吗?
我期待如下结果:
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
答案 0 :(得分:4)
显然,您正在从交互式Powershell会话中运行脚本。这样Powershell就像这样:任何' Write-Host
直接输出到控制台,而没有指示转发到控制台的任何东西(和Get-VM
返回一个VM数组对象,并没有指示Powershell将em写入控制台)作为脚本结果被累积到转发到管道(如果有的话)。但随后诀窍似乎是any output within Powershell is piped to Out-Default
。只有VM对象,Out-Default
从整个脚本输出中形成一个表,您可以看到它。要达到所需的行为,请将Get-VM
输出直接转发至Out-Default
或Format-Table
。
foreach($name1 in $name)
{
Write-Host $name1
Get-VM -computername $name1 | out-default
}