View Only
  • 1.  Understanding Export-CSV

    Posted Mar 24, 2009 11:47 PM

    Hi Everyone,

    I am writing (trying, I should say) to write a script which fetches from vCenter all ESX Hosts, and for each Host fetches the Guests (selecting name, powerstate, Host it lives on and memory) AND gets the datastore for each Host )selecting name of DS, freespace and tot capacity).

    I am not getting the intended output in my CSV file that I had hoped for. Can someone tell me what I am doing wrong? I assumed that my data lived in the variable - $esxHost and so O should pass that to the export.

    #Initialize variables

    $Hosts = 0

    $esxHost = 0

    $Hosts = Get-vmHost

    foreach ($esxHost in $Hosts) {

    Get-VM | select name, powerstate, host, memoryMB

    Get-Datastore | select name, freespacemb, capacitymb }

    $esxHost | Export-Csv "C:\LUN_Report.csv" –noTypeInformation

  • 2.  RE: Understanding Export-CSV
    Best Answer

    Posted Mar 25, 2009 03:15 AM


    This one should be able to do what you're looking for. Albeit this generates two CSVs since it really is two seperate reports you're looking for.

    Unless you're trying to print the Datastore for the VM on the same line as the VM, that would be a bit different.

    This script builds a hash table which is a nice way of then shoving it into a CSV.

    You could trim this one up to make it more "one-liner" , but should assist in getting the picture across.

    Let me know if you get this working or if it doesn't do what you're thinking.

    The first one builds two Hash tables and slaps them into two CSVs. If you want to see your Datastores seperate from your VMs.

    The second script will add the datastore to the first hash table and list it with the VM. This one is not setup to handle VMs with disks on more than 1 datastore. That can be done with some tweaking.

    Guess I was bored... I added the 3rd which will add a line in your CSV for each Datastore associated with a VM... This means your VM can be listed more than once, but it will list each DS.

    This should give you a good idea on how you can play with exporting to CSVs. I like the hash table setup.

  • 3.  RE: Understanding Export-CSV

    Posted Mar 26, 2009 01:16 AM

    Great scripts Olan. Thanks for sharing. I just ran them and the return exaclty what I need. Your script is vastly different from mine so I need to understand what you did here. BTW, I added -NoTypeInformation as an argument to your esxport-csv cmd-let to remove the type info which appears by default in the first line of the CSV.

    I have questions about your script which I want to better understand. I will post them as soon as I can so I hope you patience to answer them!


  • 4.  RE: Understanding Export-CSV

    Posted Mar 26, 2009 03:52 AM

    No problem Jindi,

    I attached a PNG graphic with some notes on it if you want to look at that. If you would, I'd appreciate it if you gave my response the Correct Answer or Helpful answer flag.

  • 5.  RE: Understanding Export-CSV

    Posted Mar 26, 2009 04:19 AM

    The graphic you attached here is excellent and is well beyond what I had hoped for. Outstanding way to comment your code and extrememly helpful. Thanks!!

  • 6.  RE: Understanding Export-CSV

    Posted Mar 26, 2009 01:56 PM

    Happy to help!