我想使用PowerShell生成Hyper-V主机及其托管的VM的列表。之后,我要搜索在哪个Hyper-V上托管的VM。
我已经有了第一部分,它是生成Hyper-V主机列表以及它们的托管VM列表,并导出到文本文件。输出看起来像这样:
----HyperV Hostname ---- state VM Name Running VmHostname A Running VMHostname B Off VMHostname C
下面是我的原始代码。
Write-Host "Get list of VM on listed Hyper-Vs"
$enddate = (Get-Date).ToString("ddMMyyyy")
$filename = $enddate + "-vmlist.txt"
$hostlist = 1..137 | ForEach-Object {"MyHostname$_"}
$ErrorActionPreference = 'SilentlyContinue'
foreach ($host_vm in $host_list) {
echo "---$host_vm---"
Write-Host "Populating $host_vm..."
Get-VM -ComputerName $host_vm | ft State, Name -AutoSize
}
Write-Host "Completed"