VMware Cloud Community
vmmeup
Expert
Expert
Jump to solution

VI Tool Kit Script Question

I'm trying to write a script that will give me a csv file that has infoamtion that is the output of two different commands. I'm trying to take the following two commmands and export the comnbined output to one file.

get-vm | select name, host, powerstate, MemoryMB, numCPU | export-csv c:\vm_info.csv

get-vm | get-vmguest | select VMname, IP Address, hostname | export-csv c:\vm_info2.csv

Ideally I want to create a script that will match name in the top command to VMname in the bottom command and generate one csv file with all the infoamtion for each vm instead of two files. My thought was to load each one into a array and somehow merge the two arrays.......anyone have any idea on how to acompish this?

Sid Smith ----- VCP, VTSP, CCNA, CCA(Xen Server), MCTS Hyper-V & SCVMM08 [http://www.dailyhypervisor.com] - Don't forget to award points for correct and helpful answers. πŸ˜‰
Reply
0 Kudos
1 Solution

Accepted Solutions
halr9000
Commander
Commander
Jump to solution

get-vm | select name, host, powerstate, MemoryMB, numCPU | export-csv c:\vm_info.csv

get-vm | get-vmguest | select VMname, IP Address, hostname | export-csv c:\vm_info2.csv

Ok, there's a few ways to do this. Here is what I would do. You can take advantage of the fact that each VM object has a Guest property which is the very same thing as what is returned by the Get-VMGuest cmdlet. Or you can use the cmdlet actually, either way, doesn't matter. The point is that you will add members (properties) from both places using what's called the calculated properties feature of select-object (and a few other cmdlets can do it as well).

While you could actually do the whole thing in one line, the end result is some funky looking column names in your CSV file. In order to have nice column names, you have to do a bit more work. When you use calculated properties with Select-Object, you can provide both a Name for the column and an Expression which is executed. Looks like this:

$IPprop = @{ Name = "IP Address"; Expression = { $_.Guest.IpAddress } }
$HostNameProp = @{ Name = "Hostname"; Expression = { $_.Guest.Hostname } }
Get-VM | select name, host, powerstate, MemoryMB, numCPU, $IPprop, $HostNameProp | export-csv c:\vm_info.csv






[PowerShell MVP|https://mvp.support.microsoft.com/profile=5547F213-A069-45F8-B5D1-17E5BD3F362F], VI Toolkit forum moderator

Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

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

Reply
0 Kudos
4 Replies
240Zeus
Enthusiast
Enthusiast
Jump to solution

Well, this won't give you exactly what you want, but it will show you how to use the Excel COM object to manipulate Excel and have a nicely formatted Excel spreadsheet.

Major props out to Alan Renouf as this started as a modification of his Word Report. My boss wanted the data in Excel format so I took Alan's lead and went down the same route he did for the Word report. I've not cleaned this up by any means so my apologies if things are a bit messy or convoluted. By default it saves the file to C:\VMReport.xls but you can change this at the very end of the file if you want to save it to another location.

Z

Reply
0 Kudos
halr9000
Commander
Commander
Jump to solution

get-vm | select name, host, powerstate, MemoryMB, numCPU | export-csv c:\vm_info.csv

get-vm | get-vmguest | select VMname, IP Address, hostname | export-csv c:\vm_info2.csv

Ok, there's a few ways to do this. Here is what I would do. You can take advantage of the fact that each VM object has a Guest property which is the very same thing as what is returned by the Get-VMGuest cmdlet. Or you can use the cmdlet actually, either way, doesn't matter. The point is that you will add members (properties) from both places using what's called the calculated properties feature of select-object (and a few other cmdlets can do it as well).

While you could actually do the whole thing in one line, the end result is some funky looking column names in your CSV file. In order to have nice column names, you have to do a bit more work. When you use calculated properties with Select-Object, you can provide both a Name for the column and an Expression which is executed. Looks like this:

$IPprop = @{ Name = "IP Address"; Expression = { $_.Guest.IpAddress } }
$HostNameProp = @{ Name = "Hostname"; Expression = { $_.Guest.Hostname } }
Get-VM | select name, host, powerstate, MemoryMB, numCPU, $IPprop, $HostNameProp | export-csv c:\vm_info.csv






[PowerShell MVP|https://mvp.support.microsoft.com/profile=5547F213-A069-45F8-B5D1-17E5BD3F362F], VI Toolkit forum moderator

Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

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

do you know if the host has an IP property? Using get-vmhost it doesn't have it by default but do you know if their is somrhing similiar to how you ontained the guest info in the vm?

Sid Smith ----- VCP, VTSP, CCNA, CCA(Xen Server), MCTS Hyper-V & SCVMM08 [http://www.dailyhypervisor.com] - Don't forget to award points for correct and helpful answers. πŸ˜‰
Reply
0 Kudos
240Zeus
Enthusiast
Enthusiast
Jump to solution

vmmeup,

See the attached script. It will enumerate the IP of the vswif0 interface and provide you the IP of the SC on the ESX host.

Z