PowerCLI

 View Only
  • 1.  VI Tool Kit Script Question

    Posted Nov 04, 2008 01:27 AM

    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?



  • 2.  RE: VI Tool Kit Script Question

    Posted Nov 04, 2008 02:39 AM

    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



  • 3.  RE: VI Tool Kit Script Question
    Best Answer

    Posted Nov 04, 2008 03:09 AM

    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)



  • 4.  RE: VI Tool Kit Script Question

    Posted Nov 04, 2008 05:05 PM

    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?



  • 5.  RE: VI Tool Kit Script Question

    Posted Nov 04, 2008 05:20 PM

    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