foreach statement display issue

别说谁变了你拦得住时间么 提交于 2019-12-06 14:51:54

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!