I'm looking for a script to get a bunch of data on VM's, then write it to a file, and format it in the file with specified delimiters in between the fields - so that in the file it would look like this (if commas are the delimiter):
<> NumberofvCPU's, RAMinGB, HDSizeinGB, OperatingSystem, HostName, IPAddress, MacAddress <>
Hi,
Apologies if you where just looking for the command to output in CSV format otherwise read on.
$vcserver = "vc-servername" $outputfile = "C:\VM-info.csv" Connect-VIServer -Server $vcserver $report = @() Get-VM | foreach { $vmv = Get-View $_.ID $row = " " | Select VMname, NumberofvCPU, RAMinGB, HDSizeGB, OperatingSystem, Hostname, IPAddress, MacAddress $row.VMname = $_.Name $row.NumberofvCPU = $_.NumCpu # Gets the guest memory size and then converts to GB $row.RAMinGB = ($_.MemoryMB)/1Kb # Gets the total size of all the harddisks connected to the guest and then converts this to GB $row.HDSizeGB = (($_.HardDisks | Measure-object -property CapacityKB -sum).Sum)/1Mb $row.OperatingSystem = $vmv.Guest.GuestFullName $row.Hostname = $_.Host # We are assuming here that there is a maximum of 1 network adapter in the guest. $row.IPAddress = $vmv.Guest.Net[0].IpAddress $row.MacAddress = $vmv.Guest.Net[0].MacAddress $report += $row } # Outputing the report in a CSV format, removing TypeInformation from the top $report | sort -property VMname | Export-Csv $outputfile -NoTypeInformation Disconnect-VIServer -confirm:$false
Modify the variables vcserver and outputfile for your environment and this should do the trick or at least get you started.
Kind regards.
add 'output nameofyourfile.csv' to the end of your script
vghetto has a large script repository (William Lam)
http://communities.vmware.com/docs/DOC-9852
You can try the Export-Csv cmdlet.
Collect all your data in an array and the you can do
$array | Export-Csv "C:\myfile.csv" -NoTypeInformation
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
William has indeed a most impressive repository of scripts.
But I'm afraid most, if not all, are written in Perl
Now if you could convince William to start converting...
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
He's the reason I'm using vMA haha! Its good stuff..
Hi,
Apologies if you where just looking for the command to output in CSV format otherwise read on.
$vcserver = "vc-servername" $outputfile = "C:\VM-info.csv" Connect-VIServer -Server $vcserver $report = @() Get-VM | foreach { $vmv = Get-View $_.ID $row = " " | Select VMname, NumberofvCPU, RAMinGB, HDSizeGB, OperatingSystem, Hostname, IPAddress, MacAddress $row.VMname = $_.Name $row.NumberofvCPU = $_.NumCpu # Gets the guest memory size and then converts to GB $row.RAMinGB = ($_.MemoryMB)/1Kb # Gets the total size of all the harddisks connected to the guest and then converts this to GB $row.HDSizeGB = (($_.HardDisks | Measure-object -property CapacityKB -sum).Sum)/1Mb $row.OperatingSystem = $vmv.Guest.GuestFullName $row.Hostname = $_.Host # We are assuming here that there is a maximum of 1 network adapter in the guest. $row.IPAddress = $vmv.Guest.Net[0].IpAddress $row.MacAddress = $vmv.Guest.Net[0].MacAddress $report += $row } # Outputing the report in a CSV format, removing TypeInformation from the top $report | sort -property VMname | Export-Csv $outputfile -NoTypeInformation Disconnect-VIServer -confirm:$false
Modify the variables vcserver and outputfile for your environment and this should do the trick or at least get you started.
Kind regards.