SR175
Contributor
Contributor

PowerCLI script to find the host a vm is on from a text file with server names

Jump to solution

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

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
vHaridas
Expert
Expert

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

Please consider awarding points for "Correct" or "Helpful" replies. Thanks....!!! https://vprhlabs.blogspot.in/

View solution in original post

0 Kudos
2 Replies
vHaridas
Expert
Expert

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

Please consider awarding points for "Correct" or "Helpful" replies. Thanks....!!! https://vprhlabs.blogspot.in/
0 Kudos
SR175
Contributor
Contributor

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

}

0 Kudos