foreach语句显示问题 [英] foreach statement display issue

查看:110
本文介绍了foreach语句显示问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过提供文本文件中的服务器列表来从我的主机服务器中获取VM列表.它具有FOREACH循环来获取每一行并从中获取VM的列表.而是同时显示所有VM List togeter和主机名,而不是根据输入的每一行进行分隔.我尝试使用$ list之类的变量存储它,但是它不显示任何输出.

I am Trying to Fetch List of VM's from My host Servers by Providing List of Servers from a text file. It is having FOREACH loop to fetch each line and GET VM's list from them. instead its displaying all the VM List togeter and Hostnames together and not segregating based on each line of input. I tried storing it using variable like $list but it doesnot show any output.

$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的人,知道我要去哪里错了吗?

All the VMS and HOSTS are being being displayed together rather than searching and Displaying one server at a time. But Couldnot achive this, I am new to powershell any idea where I am going wrong?

我期望得到如下结果:

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则返回VM对象数组,并且不指示Powershell将em写入控制台) )作为 script结果累积,然后转发到管道(如果有).但随后的窍门似乎是 any Powershell中的输出通过管道传递到Out-Default .只有VM对象,您观察到的Out-Default在整个脚本输出中形成了一个表.为了达到所需的行为,请将您的Get-VM输出直接转发到Out-DefaultFormat-Table.

Apparently you are runing the script from an interactive Powershell session. This way Powershell behaves like so: Anything that's Write-Host is directly outputted to console, while anything that's not instructed to be forwarded to console (and Get-VM returns an array of VM objects, and does not instruct Powershell to write em to console) is accumulated as script result to be forwarded to the pipeline, if any. But then the trick appears that any output within Powershell is piped to Out-Default. There are only VM objects, and Out-Default forms one single table out of the entire script output, which you observe. To reach desired behavior, forward your Get-VM output either directly to Out-Default, or to Format-Table.

foreach($name1 in $name)
{ 
    Write-Host $name1
    Get-VM -computername $name1 | out-default
}

这篇关于foreach语句显示问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆