VMware Cloud Community
AGFlora
Enthusiast
Enthusiast

Datastore Usage Report

Hi

The following code reports the datastore usage and writes to an html file using the Add-Content cmdlet.

I'm having trouble sorting by % free. Please help.

CODE:

Function GetDS
{
Get-DataCenter $DC | Get-Datastore | Where-Object {$_.Name -notlike "DataStore1*"} | %{
        $Store = {} | Select Name, CapacityGB, UsedGB, FreeSpace, PercFree
            $Name = $Store.Name = $_.name | Sort -Property PercFree
   $Capacity = $Store.CapacityGB = [math]::Round($_.capacityGB/1024,$digits)
   $Used = $Store.UsedGB = [math]::Round(($_.CapacityGB - $_.FreeSpaceGB)/1024,$digits)
   $PercFree = $Store.PercFree = [math]::Round(100*$_.FreeSpaceGB/$_.CapacityGB,$digits)  | Sort -Property PercFree
           
# Passing the 4 variables from this function to another function called writedata
writedata $Name $Capacity $Used $PercFree
  }
}

# Function will compile the data passed from the RPCInfo Function into HTML table format
Function writedata
{
param ($Name,$Capacity,$Used, $PercFree)


If ($Store.PercFree -gt $Warning)
{$tableEntry = "<tr><td><font face='Tahoma'>$Name</font></td><td><center><b><font face='Tahoma'>$Capacity</font></b></center></td><td><center><b><font face='Tahoma'>$Used</font></b></center></td><td><center><b><font face='Tahoma'color = 'darkblue'>$PercFree</font></b></center></td></tr>"
Add-Content $fileName $tableEntry
Write-Host $tableEntry}

ElseIf ($Store.PercFree -lt $Critical)
{$tableEntry = "<tr><td><font face='Tahoma'>$Name</font></td><td><center><b><font face='Tahoma'>$Capacity</font></b></center></td><td><center><b><font face='Tahoma'>$Used</font></b></center></td><td><center><b><font face='Tahoma'color = 'red'>$PercFree</font></b></center></td></tr>"
Add-Content $fileName $tableEntry
Write-Host $tableEntry}

ElseIf ($Store.PercFree -lt $Warning)
{$tableEntry = "<tr><td><font face='Tahoma'>$Name</font></td><td><center><b><font face='Tahoma'>$Capacity</font></b></center></td><td><center><b><font face='Tahoma'>$Used</font></b></center></td><td><center><b><font face='Tahoma'color = 'Orange'>$PercFree</font></b></center></td></tr>"
Add-Content $fileName $tableEntry
Write-Host $tableEntry}


}

End Code

Thanks

0 Kudos
7 Replies
LucD
Leadership
Leadership

Try something like this

Function GetDS {
    $report = Get-DataCenter $DC | Get-Datastore | 
        Where-Object {$_.Name -notlike "DataStore1*"} | %{
        New-Object PSObject -Property @{
            Name = $_.name
           
Capacity = [math]::Round($_.capacityGB/1024,$digits)             Used = [math]::Round(($_.CapacityGB - $_.FreeSpaceGB)/1024,$digits)             PercFree = [math]::Round(100 * $_.FreeSpaceGB/$_.CapacityGB,$digits)         }     } | Sort-Object -Property PercFree
    $report | %{         writedata $_.Name $_.Capacity $_.Used $_.PercFree
        } }
# Function will compile the data passed from the RPCInfo Function into HTML table format Function writedata {     param ($Name,$Capacity,$Used, $PercFree)     If ($Store.PercFree -gt $Warning)     {        $tableEntry = "<tr><td><font face='Tahoma'>$Name</font></td><td><center><b><font face='Tahoma'>$Capacity</font></b></center></td><td><center><b><font face='Tahoma'>$Used</font></b></center></td><td><center><b><font face='Tahoma'color = 'darkblue'>$PercFree</font></b></center></td></tr>"
        Add-Content $fileName $tableEntry
        Write-Host $tableEntry}     ElseIf ($Store.PercFree -lt $Critical)     {        $tableEntry = "<tr><td><font face='Tahoma'>$Name</font></td><td><center><b><font face='Tahoma'>$Capacity</font></b></center></td><td><center><b><font face='Tahoma'>$Used</font></b></center></td><td><center><b><font face='Tahoma'color = 'red'>$PercFree</font></b></center></td></tr>"
       
Add-Content $fileName $tableEntry
       
Write-Host $tableEntry}     ElseIf ($Store.PercFree -lt $Warning)     {        $tableEntry = "<tr><td><font face='Tahoma'>$Name</font></td><td><center><b><font face='Tahoma'>$Capacity</font></b></center></td><td><center><b><font face='Tahoma'>$Used</font></b></center></td><td><center><b><font face='Tahoma'color = 'Orange'>$PercFree</font></b></center></td></tr>"
        Add-Content $fileName $tableEntry
       
Write-Host $tableEntry} }


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

0 Kudos
AGFlora
Enthusiast
Enthusiast

Hi Luc

The ouput is only the html code but no data.

Here is the entire script:

#Load PowerCLI snapin
add-pssnapin VMware.VimAutomation.Core

#Remove previous HTML report
Remove-Item "HTML-Reports\DS-Report.htm"

##Connect to vCenter Server
###########################
$vC = "vCenterServer"
Connect-VIServer $vC
$date = ( Get-Date ).ToString('yyyy/MM/dd')
$Title = "Datastore Usage Report"
$Warning = "10"
$Critical = "5"
$digits = 2
$DC = "Datacenter1"

$DateStamp = get-date -uformat "%Y-%m-%d@%H-%M-%S"

#Create file for new run
New-Item -ItemType file "HTML-Reports\DS-Report.htm"
$fileName = "HTML-Reports\DS-Report.htm"

#HTML Header for Output
Function writeHtmlHeader
{
param($fileName)

Add-Content $fileName "<html>"
Add-Content $fileName "<head>"
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $fileName '<title>$Title</title>'
Add-Content $fileName '<STYLE TYPE="text/css">'
Add-Content $fileName  "<!--"
Add-Content $fileName  "td {"
Add-Content $fileName  "font-family: Calibri;"
Add-Content $fileName  "font-size: 13px;"
Add-Content $fileName  "border-top: 1px solid #999999;"
Add-Content $fileName  "border-right: 1px solid #999999;"
Add-Content $fileName  "border-bottom: 1px solid #999999;"
Add-Content $fileName  "border-left: 1px solid #999999;"
Add-Content $fileName  "padding-top: 0px;"
Add-Content $fileName  "padding-right: 0px;"
Add-Content $fileName  "padding-bottom: 0px;"
Add-Content $fileName  "padding-left: 0px;"
Add-Content $fileName  "}"
Add-Content $fileName  "body {"
Add-Content $fileName  "margin-left: 5px;"
Add-Content $fileName  "margin-top: 5px;"
Add-Content $fileName  "margin-right: 0px;"
Add-Content $fileName  "margin-bottom: 10px;"
Add-Content $fileName  ""
Add-Content $fileName  "table {"
Add-Content $fileName  "border: thin solid #000000;"
Add-Content $fileName  "}"
Add-Content $fileName  "-->"
Add-Content $fileName  "</style>"
Add-Content $fileName "</head>"
Add-Content $fileName "<body>"
Add-Content $fileName  "<table width='60%'>"
Add-Content $fileName  "<tr bgcolor='#FFFFFF'>"
Add-Content $fileName  "<td colspan='7' height='25' align='center'>"
Add-Content $fileName  "<font face='Tahoma' color='#003399' size='3'><strong>$Title - $Date</strong></font>"
Add-Content $fileName  "</td>"
Add-Content $fileName  "</tr>"
Add-Content $fileName  "</table>"
}

# Function to write the HTML Header to the file
# This Example will have 4 Columns
Function writeTableHeader
{
param($fileName)
Add-Content $fileName "<table width='60%'><tbody>"
Add-Content $fileName "<tr bgcolor=#003399>"
Add-Content $fileName "<td align='center'><font face='Tahoma' color='#FFFFFF'><b>Datastore Name</b></font></td>"
Add-Content $fileName "<td align='center'><font face='Tahoma' color='#FFFFFF'><b>Capacity (TB)</b></font></td>"
Add-Content $fileName "<td align='center'><font face='Tahoma' color='#FFFFFF'><b>Used (TB)</b></font></td>"
#Add-Content $fileName "<td align='center'><font face='Tahoma' color='#FFFFFF'><b>Critical - Below 5% Free / Warning - Below 10% Free</b></font></td>"
Add-Content $fileName "<td align='center'><font face='Tahoma' color='#FFFFFF'><b>% Free</b></font></td>"
Add-Content $fileName "</tr>"
}

# Function to write the HTML Footer
Function writeHtmlFooter
{
param($fileName)
Add-Content $fileName "</table>"
Add-Content $fileName "<left><font face='Tahoma' color='#003399' size='2'><weak>Send All Inqueries Regarding this Report to EmailAddress</left>"
Add-Content $fileName "</body>"
Add-Content $fileName "</html>"
}

#<#
# Function to perform the work for the data you need
# I left content in place to give example
# Server variable was derived from a Get-Content function which would have occurred at the initialization of this script (removed)

Function GetDS {
    $report = Get-DataCenter $DC | Get-Datastore |
        Where-Object {$_.Name -notlike "DataStore1*"} | %{
        $row = New-Object PSObject -Property @{
            Name = $_.name           
            Capacity = [math]::Round($_.capacityGB/1024,$digits)
            Used = [math]::Round(($_.CapacityGB - $_.FreeSpaceGB)/1024,$digits)
            PercFree = [math]::Round(100 * $_.FreeSpaceGB/$_.CapacityGB,$digits)
        }
    } | Sort-Object -Property PercFree   
    $report | %{
        writedata $_.Name $_.Capacity $_.Used $_.PercFree       
   }
}

# Function will compile the data passed from the RPCInfo Function into HTML table format
Function writedata {
    param ($Name,$Capacity,$Used, $PercFree)

    If($Store.PercFree -gt $Warning)
    {$tableEntry = "<tr><td><font face='Tahoma'>$Name</font></td><td><center><b><font face='Tahoma'>$Capacity</font></b></center></td><td><center><b><font face='Tahoma'>$Used</font></b></center></td><td><center><b><font face='Tahoma'color = 'darkblue'>$PercFree</font></b></center></td></tr>"       
    Add-Content $fileName $tableEntry       
    Write-Host $tableEntry}

    ElseIf($Store.PercFree -lt $Critical)
    {$tableEntry = "<tr><td><font face='Tahoma'>$Name</font></td><td><center><b><font face='Tahoma'>$Capacity</font></b></center></td><td><center><b><font face='Tahoma'>$Used</font></b></center></td><td><center><b><font face='Tahoma'color = 'red'>$PercFree</font></b></center></td></tr>"       
    Add-Content $fileName $tableEntry       
    Write-Host $tableEntry}

    ElseIf($Store.PercFree -lt $Warning)
    {        $tableEntry = "<tr><td><font face='Tahoma'>$Name</font></td><td><center><b><font face='Tahoma'>$Capacity</font></b></center></td><td><center><b><font face='Tahoma'>$Used</font></b></center></td><td><center><b><font face='Tahoma'color = 'Orange'>$PercFree</font></b></center></td></tr>"       
    Add-Content $fileName $tableEntry       
    Write-Host $tableEntry}

}

# Combine functions into orderly format for the output
writehtmlheader $fileName
writetableheader $fileName
GetDS
writehtmlfooter $fileName

##Disconnect from vCenter server
################################
Disconnect-VIServer -Confirm:$False

 


0 Kudos
LucD
Leadership
Leadership

There were some CR-LF missing in the script. I just corrected the code above, can you try again ?


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

0 Kudos
AGFlora
Enthusiast
Enthusiast

Same result.

Were you talking about the following being out of place?

Add-Content $fileName $tableEntry       
    Write-Host $tableEntry}

Before I put those two lines in the proper place the script wouldn't run but still the same result.

Thanks

0 Kudos
LucD
Leadership
Leadership

Yes, those were the lines I meant.

But I checked again and saw this line

$row = New-Object PSObject -Property @{

that should be

New-Object PSObject -Property @{

I corrected the code above.


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

0 Kudos
AGFlora
Enthusiast
Enthusiast

Excellent that was it!

At first the % numbers were all in red but it's perfect after I changed the fowllwing lines:

From:

If ($Store.PercFree -gt $Warning)

To:

If ($_.PercFree -gt $Warning)

BTW I bought your book. Excellent read so far. I like your writing style.

Thanks again for your help!

0 Kudos
LucD
Leadership
Leadership

Thanks, I hope you will find many useful scripts in the book.


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

0 Kudos