How would I find the host that a vm is on via powercli given a text file with the list of vm names then export to csv? I have a text file that lists the vm names of servers and i need to find what host they are on.
This one liner works great, although very slow, but it would be cumbersome doing this a few hundred times!
Get-VMHost -VM VMName | select Name
Try below commands. txt file path - C:\temp\inputFile.txt
$vmList = Get-Content C:\temp\inputFile.txt
foreach ($vmName in $vmList) {Get-VM $vmName | select Name, @{N="VMHost"; E={$_.VMHost.Name}}}
-
Haridas Vhadade
Try below commands. txt file path - C:\temp\inputFile.txt
$vmList = Get-Content C:\temp\inputFile.txt
foreach ($vmName in $vmList) {Get-VM $vmName | select Name, @{N="VMHost"; E={$_.VMHost.Name}}}
-
Haridas Vhadade
This works great and fast. This is the ticket for me. Thanks!
Right as I saw your response I was toying with this. It takes a lot longer and it does not have the much better formatting of yours.
$vms = gc .\_VMs.txt
foreach($vm in $vms) {
get-vm $VM | get-vmhost | select name
}