VMware Cloud Community
JindiJee
Contributor
Contributor
Jump to solution

Understanding Export-CSV

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

Reply
0 Kudos
1 Solution

Accepted Solutions
olan025
Enthusiast
Enthusiast
Jump to solution

JindiJee,

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.

View solution in original post

Reply
0 Kudos
5 Replies
olan025
Enthusiast
Enthusiast
Jump to solution

JindiJee,

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.

Reply
0 Kudos
JindiJee
Contributor
Contributor
Jump to solution

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!

Thanks

Reply
0 Kudos
olan025
Enthusiast
Enthusiast
Jump to solution

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.

JindiJee
Contributor
Contributor
Jump to solution

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!!

Reply
0 Kudos
olan025
Enthusiast
Enthusiast
Jump to solution

Happy to help!

Reply
0 Kudos