locked
Getting list of VMs from a list of Hyper-V hosts. RRS feed

  • Question

  • Any idea would be appreciated on how to get the list of VM from a list of Hyper-V host stored in a CSV or a Text file using powershell.

    Thanks

    Roushan

    Tuesday, October 17, 2017 12:31 PM

Answers

  • hi Roushan,

    do I understand you correctly that you want to query a list of Hyper-V hosts to get a list of the VMs on those hosts and store the result in a CSV file?

    If so, this is it:

    $hosts = "192.168.178.10","192.168.178.12"
    Get-VM -ComputerName $hosts | Export-Csv -Path C:\tmp\vmlist.csv -NoTypeInformation

    $hosts is an array of your Hyper-V hosts (IP addresses or hostnames).

    The second line queries these hosts and saves the output to a file.

    You can select the columns you want to export like so:

    Get-VM -ComputerName $hosts | select name, state | Export-Csv -Path C:\tmp\vmlist.csv -NoTypeInformation

    You could also make this a true oneliner:

    Get-VM -ComputerName "192.168.178.10","192.168.178.12" | select name, state | Export-Csv -Path C:\tmp\vmlist.csv -NoTypeInformation
    

    KR

    Guenther



    Wednesday, October 18, 2017 6:16 AM

All replies

  • For a text file with only a list of names and no header:

    import-csv <path to the file> -Header Name


    \_(ツ)_/

    Tuesday, October 17, 2017 1:03 PM
  • If you have the Hyper-V Module installed, you should be able to do something like this:

    $vmList = @() foreach ($i in (Get-Content "C:\pathtofile\file.csv")) { $vmList += Get-VMHost $i.name | Get-VM }

    Write-Host $vmList


    I don't know your environment, what format the hyper-v hosts are stored in the csv, etc. So that means that the above command will require a little bit of a re-write. But it should get you going in the right direction.


    • Edited by Jacorbello Tuesday, October 17, 2017 1:08 PM
    Tuesday, October 17, 2017 1:07 PM
  • hi Roushan,

    do I understand you correctly that you want to query a list of Hyper-V hosts to get a list of the VMs on those hosts and store the result in a CSV file?

    If so, this is it:

    $hosts = "192.168.178.10","192.168.178.12"
    Get-VM -ComputerName $hosts | Export-Csv -Path C:\tmp\vmlist.csv -NoTypeInformation

    $hosts is an array of your Hyper-V hosts (IP addresses or hostnames).

    The second line queries these hosts and saves the output to a file.

    You can select the columns you want to export like so:

    Get-VM -ComputerName $hosts | select name, state | Export-Csv -Path C:\tmp\vmlist.csv -NoTypeInformation

    You could also make this a true oneliner:

    Get-VM -ComputerName "192.168.178.10","192.168.178.12" | select name, state | Export-Csv -Path C:\tmp\vmlist.csv -NoTypeInformation
    

    KR

    Guenther



    Wednesday, October 18, 2017 6:16 AM
  • Hi Roushan,

    Just checking in to see if the information provided was helpful.

    Please let us know if you would like further assistance.

    Best Regards,

    Candy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, October 25, 2017 10:03 AM
  • couple of follow up questions:

    1) if I'm running this across 10 hosts, how do I get the host name to return in the result, so that I can open the CSV and see what's living where? 

    2) Can you export other info from Hyper-V (processor assigned/ram assigned & type dynamic/static), drive info.



    Michael

    Sunday, March 31, 2019 3:51 PM
  • Please do not add additional questions to other peoples threads. Create a new one for yourself and place a link to this one if needed.

    Thanks.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Sunday, March 31, 2019 4:22 PM
  • couple of follow up questions:

    1) if I'm running this across 10 hosts, how do I get the host name to return in the result, so that I can open the CSV and see what's living where? 

    2) Can you export other info from Hyper-V (processor assigned/ram assigned & type dynamic/static), drive info.



    Michael

    1) just like so:

    Get-VM -ComputerName $hosts | select name, state, computername

    2) sure you can, like in this script: https://github.com/k4cg/beehive-scripts/blob/master/monitorVmUsageData.ps1

    Monday, April 1, 2019 5:20 AM