trojanjo
Enthusiast
Enthusiast

Problem with Script to dump each VM by Host into a CSV

Jump to solution

I would like to get a dynamic way to find exactly what VM is on each host. Maybe there is a better way to do this. Below is the way I think it should work:

Get-VIServer

$esx = get-vmhost

Write-Output "Looking for VMs...."

$esx | ForEach-Object {

$VmHost = $_
Write-Output $VmHost
*Get-VMHost $VmHost | *Get-VM | Export-Csv d:\$HostText.csv

}

The output creates a csv called VMware.VimAutomation.Client20.VMHostImpl.csv with nothing inside of it. If I change the script to this:

Get-VIServer <br /$esx = "esxserver1","esxserver2"

Write-Output "Looking for VMs...."

$esx | ForEach-Object {

$VmHost = $_
Write-Output $VmHost
*Get-VMHost $VmHost | *Get-VM | Export-Csv d:\$HostText.csv

}

It works fine. I think there is some kind of type mismatch. Is there a way to convert this? or pull the host server name into a string so it will work. I want this script to work across different enviroments so I don't have to manually put in each host name.

Any thoughts?

---- Visit my blog. http://www.2vcps.com
Follow me: http://twitter.com/jon_2vcps
0 Kudos
1 Solution

Accepted Solutions
halr9000
Commander
Commander

The problem lies in how you are accessing the VMs in this line:

Get-VMHost $VmHost | Get-VM

What that does is return a VM object of type VMware.VimAutomation.Client20.VMHostImpl. What you really wanted to do was to access the Name property of that object. So just slap a ".name" on the end and it should work like you expect. That said, I would do this a different way. I'll hit submit now to get the answer in and if I have time in a bit I'll come back and give a further suggestion.

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000

View solution in original post

0 Kudos
6 Replies
halr9000
Commander
Commander

The problem lies in how you are accessing the VMs in this line:

Get-VMHost $VmHost | Get-VM

What that does is return a VM object of type VMware.VimAutomation.Client20.VMHostImpl. What you really wanted to do was to access the Name property of that object. So just slap a ".name" on the end and it should work like you expect. That said, I would do this a different way. I'll hit submit now to get the answer in and if I have time in a bit I'll come back and give a further suggestion.

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000

View solution in original post

0 Kudos
admin
Immortal
Immortal

Hal outlined the problem above - basically, in that first example your $esx holds objects that represent the hosts and in the second version it holds strings that are the names of the hosts.

I would try something like this if you want to restrict to a specific set of hosts:

$esx = "esx0","esx1","esx2"

$esx | get-vmhost | %{

get-vm -location $_ | Select-Object name,@{Name="Host"; Expression = {$_.host.name}} | Export-Csv ($_.name + ".csv")

}

or maybe something like this if you don't really need one file per host:

get-vm | Select-Object @{Name="Host"; Expression = {$_.host.name}},name | Export-Csv VMByHost.csv

trojanjo
Enthusiast
Enthusiast

Thank you both.

I took from both of you to get what it was I wanted.

Any other suggested alternatives?

---- Visit my blog. http://www.2vcps.com
Follow me: http://twitter.com/jon_2vcps
0 Kudos
admin
Immortal
Immortal

Well, top of the head, you could get the information, pipe it to a group-object and do various other things to look at it in different ways. e.g.

get-vm | Select-Object @{Name="VMName"; Expression = {$_.name}},@{Name="Host"; Expression = {$_.host.name}} | Group-Object Host

Though that's going to be slightly harder to serialize into a csv if that's the end goal. Probably a few other ways depending on what else you want. So I'll bounce the question back to you: what else are you trying to achieve?

0 Kudos
trojanjo
Enthusiast
Enthusiast

For me I may travel to several client sites, so I may not always have access to the Virtual Center. That is why I would like to dump all the info into excel.

I would like to have each VM by its name, the IP Address, Virtual Network name, Disk size, Datastore name, Memory Size,Number of vCPU and the ESX Host's name where it is currently living.

There are probably several tools already written that might do this. I would like the freedom and customization of having it all in one script where I can keep my records for each client as current as possible without editing an excel file by hand.

So yes this is just the start but I believe it can be very useful in my role and for people like me.

---- Visit my blog. http://www.2vcps.com
Follow me: http://twitter.com/jon_2vcps
0 Kudos
halr9000
Commander
Commander

Ahh, Excel. Sounds like next steps for you is to skip the CSV altogether and start researching Excel automation. Smiley Happy There's a few ways of doing that, I've not messed much with it myself. You'll probably get better help with that by searching existing resources (newsgroup) and using the powershellcommunity.org forums.

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos