VMware Cloud Community
kcvman
Contributor
Contributor

Script for Daily Storage Report - Help Scripting Gurus!

VI Toolkit + PowerGUI + PowerGadgets = awesome

My knowledge of Powershell = minimal (so far, but I'm trying to learn!)

My question -

My storage folks are asking for a daily report on SAN usage. They want to know:

vmguestx on vmhostx is using datastore(s) x, which has a capacity of xxx and free space of xxx.

I've tried a few ways on my own, but having not much luck.

Can any of the scripting gods help out with a ps1 script for this?

Appreciate any help!

0 Kudos
21 Replies
halr9000
Commander
Commander

vmguestx on vmhostx is using datastore(s) x, which has a capacity of xxx and free space of xxx.

Get-Datastore answers much of what you want, but it doesn't do it by VM. Here's a neat trick which might do it for ya:

Get-VM | % { $_ | Get-Datastore | Add-Member -Name VM -Value $_ -MemberType NoteProperty -PassThru } | ft *

Add-Member here is literally inserting the VM object into the datastore objects which pass through the foreach-object (alias %) loop. That allows you to see the VM name in the output.

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
kcvman
Contributor
Contributor

Thanks for the help, but It's still only showing datastore - free space - capacity

The VM Guest names are not showing. I copied an pasted that line directly into the powershell window and it showed no errors. But no guest names either.

Any other ideas?

Apprecaite it and I cannot wait to buy your book!

0 Kudos
halr9000
Commander
Commander

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

My Fault!!!! The ending "*" did not copy/paste over when I pasted into the PS window.

It works great!!

Thanks alot for your help!

0 Kudos
kcvman
Contributor
Contributor

one last ? - how would I add the VMhost to the output of this so it matches what guest is displaying for each datastore? I tried just adding get-vmhost after the get-datastore (i.e. get-datastore | get-vmhost), but it errored out.

Output would be vmguest - running on vmhost - using these datastores - freeMB, capacityMB.

Thanks a million!

0 Kudos
rolandt
Contributor
Contributor

The in-line script does not work for me. I get the following error.

-


out-lineoutput : Object of type "Microsoft.PowerShell.Commands.Internal.Format. FormatStartData" is not legal or not in the correct sequence. This is likely caused by a user-specified "format-table" command which is conflicting with the default formatting.

-


0 Kudos
halr9000
Commander
Commander

The powershell output subsystem is really cool when it works. It does some automagic things which are hard to understand sometimes.

It should work if you either add "| out-default" to the end, or replace "ft" with "select-object".

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
0 Kudos
bjmoore
Enthusiast
Enthusiast

Hey Hal, how can I get the results of this script to output to a text file? I've tried script.ps1 > output.txt and > output.txt 2>&1 but the Get-VM command always returns on the console, not in the file.

Thanks!

0 Kudos
halr9000
Commander
Commander

This one is a bit awkward but it works:

Get-VM | ForEach-Object { 
	$_ | Get-Datastore | Add-Member -Name VM -Value $_ -MemberType NoteProperty -PassThru 
} | Format-Table * -autosize | Out-String -width 200 | Out-File c:\tmp\file.txt

And you ought to be able to replace out-file with >, did not try it.

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
0 Kudos
bjmoore
Enthusiast
Enthusiast

Thanks that worked great Smiley Happy I had figured out the out-file portion, but not the formatting.

0 Kudos
halr9000
Commander
Commander

Thanks that worked great Smiley Happy I had figured out the out-file portion, but not the formatting.

I actually had an issue like this one before where I had to figure out

the combination of format-table w/Autosize + out-string. It was not

very intuitive.

--

Author, Tech Prosaic blog (http://halr9000.com)

Webmaster, Psi (http://psi-im.org)

Community Director, PowerShellCommunity.org

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

Follow me on Twitter: http://twitter.com/halr9000

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

And how would you export this otherwise beautiful script's output to CSV?

0 Kudos
kwharrisit
Contributor
Contributor

I just started running this report on my environment, and I have a couple of questions:

First, here's what Ive got so far

Connect-VIServer testvc

Get-VM | ForEach-Object {

$_ | Get-Datastore | Add-Member -Name VM -Value $_ -MemberType NoteProperty -PassThru

} | Format-Table * -autosize | Out-String -width 200 | Out-File c:\VMToStorage.txt

What I would like to know is 1. Can you write this to csv, and 2. how can I run a report per Cluster?

0 Kudos
LucD
Leadership
Leadership

The beauty of PowerShell Smiley Wink

1) for a per-cluster report you can use the Get-Cluster cmdlet.

The Get-VM will in this case only return guests from that specific cluster.

2) PS has the Export-Csv cmdlet that creates CSV files.

Get-Cluster  <cluster-name> | Get-VM | ForEach-Object {
	$_ | Get-Datastore | Add-Member -Name VM -Value $_ -MemberType NoteProperty -PassThru
} | Export-Csv "C:\VMToStorage.csv" -NoTypeInformation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
kwharrisit
Contributor
Contributor

OK, So I can actually get it into a csv file (yeah) , Is there a way to take this and report how much free space (percentage and \or GB) is left on the LUN? Basically Im wanting this as a report that says "You have 300 machines. they are labeled xxx-xxy. They are on the following LUNS. The LUNS have x amount of free space.

thanks for you help as always !

0 Kudos
LucD
Leadership
Leadership

Have a look at .

That thread contains several examples on how to get that data.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
kwharrisit
Contributor
Contributor

Luc,

i took a look at this one -

or you can go like that:

$report = @( )

$sumfree = 0

$sumcap = 0

Get-Datastore | % {

$row = "" | Select-Object Name, FreeSpace, Capacity

$row.Name = $_.Name

$row.FreeSpace = $_.FreeSpaceMB

$row.Capacity = $_.CapacityMB

$sumfree += $_.FreeSpaceMB

$sumcap += $_.CapacityMB

$report += $row

}

$report

Write-Host "Total Free Cap: " $sumfree

Write-Host "Total Cap: " $sumcap

This does go through and sum up everything, but it there a way to get the SUM from only the shared storage?? We do not really care what's free from the local store, since everything is stored on 14 1 TB SAN LUNS.

0 Kudos
LucD
Leadership
Leadership

What exactly do you mean with "shared storage" ? Are that the VMFS datastores ?

If yes, then you can filter the objects produced by the Get-Datastore cmdlet like this

Get-Datastore | Where-Object {$_.Type -eq "VMFS"} | %{
....

Or do you mean something else ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
kwharrisit
Contributor
Contributor

Luc,

We format whatever local storage is left on the server (70GB) as VMFS, but we actually only use the EXternal SAN storage for our VM's. For example :

ESX server 1 has local vmfs (labeled local) and can see 14 SAN LUNS (labeled LUN 1-13)

Is there a way to not look at the "local" labeled storage and only look at the storage attached to the HBA ?

0 Kudos