afaque.khan1
afaque.khan1

Reputation: 79

foreach statement display issue

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

     }

Output I am getting is:

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

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?

I am expecting result like below:

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

Upvotes: 1

Views: 52

Answers (1)

Vesper
Vesper

Reputation: 18757

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
}

Upvotes: 4

Related Questions