VMware Cloud Community
Vijay1996
Enthusiast
Enthusiast
Jump to solution

Generate the datastore report using powercli script send the report through email every 4 hours

Hi Team,

Could you please assist on this request. Generate the datastore report using powercli script  send the report through email every 4 hours with below condition.

1) datastore above 90% should highlighted with red color.

2) datastore above 70% to 90% should be highlighted with yellow color.

3) datastore below 70% should highlighted with green color.

Thanks

Vijay

Tags (2)
52 Replies
LucD
Leadership
Leadership
Jump to solution

Do you have a User and Password for an account that is allowed to send via the SmtpServer?


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

0 Kudos
Vijay1996
Enthusiast
Enthusiast
Jump to solution

Thanks for your help!!. Now i got it.

pastedImage_2.png

But can you please help to modify some changes like

1)  All those items are in table format.

2)  Please add the "Free percent" available in datastore.

3) Font color should only on black for all text ans numbers. And those red, green, yellow colors are only in fill Color of the " Free percent " datastore column in table. 

4) If datastore free space is below 10%. That should be marked as a top of the table.

For example:

powershell-script-health-check-exchange.png

Thank you!!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$body = Get-Datastore |

    Select @{N='Datastore';E={$_.Name}},

    @{N='CapacityGB';E={[math]::Round($_.CapacityGB,1)}},

    @{N='FreeSpaceGB';E={[math]::Round($_.FreeSpaceGB,1)}},

    @{N='UsedPercent';E={[math]::Round(($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*100,1)}},

    @{N='FreePercent';E={

        $p = [math]::Round((1 - ($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB)*100,1)

        if($p -lt 10){"#br#$($p)"}

        elseif($p -lt 30){"#by#$($p)"}

        else{$p}

    }} |

    Sort-Object -Property UsedPercent -Descending |

    ConvertTo-Html

$body = $body.Replace('>#br#',' bgcolor="red">').Replace('>#by#',' bgcolor="yellow">')

$sMail = @{

    To = 'my.name@domain'

    From = 'report@domain'

    Subject = 'Datastore Report'

    BodyAsHtml = $true

    Body = $body | Out-String

    SmtpServer = 'mail.domain'

}

Send-MailMessage @sMail


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

YuvarajSekar01
Enthusiast
Enthusiast
Jump to solution

Hi Lucd.

It work fine with me. Thanks for the help. how can i change the script if i want to get datastores report from the multiple vCenters. I want all to be in the single html sheet. Total count of the datastores in my environment will be 900. Also i would like to show individual datastores in one table and another table with Local datastores.

Thanks in Advance,

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you the same guy connecting with 2 accounts?


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

0 Kudos
YuvarajSekar01
Enthusiast
Enthusiast
Jump to solution

No. I am just trying to make use of this. Thats it.

0 Kudos
Vijay1996
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

Thank you so much for your help!!. I really like your script.

I need some changes in this report,

1) Can you please add those items into the table format

2) more than 30% freespace are in white color, could you please change in to green color.

report.PNG

And also i have attached the output report for your reference.

Thanks in Advance,

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Adding the green fill colour is not a big deal, but I'm not sure what you mean by 1) ... add those items

Which "items" do you mean?


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

Vijay1996
Enthusiast
Enthusiast
Jump to solution

I mean, In report borders are in white color. So can you please change in to all borders as black and please add green fill color to above 30% free space datastore

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$head = @"

<style>

TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}

TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;;background-color:thistle}

TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}

</style>

"@

$body = Get-Datastore |

    Select @{N='Datastore';E={$_.Name}},

    @{N='CapacityGB';E={[math]::Round($_.CapacityGB,1)}},

    @{N='FreeSpaceGB';E={[math]::Round($_.FreeSpaceGB,1)}},

    @{N='UsedPercent';E={[math]::Round(($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*100,1)}},

    @{N='FreePercent';E={

        $p = [math]::Round((1 - ($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB)*100,1)

        if($p -lt 10){"#br#$($p)"}

        elseif($p -lt 30){"#by#$($p)"}

        else{"#bg#$($p)"}

    }} |

    Sort-Object -Property UsedPercent -Descending |

    ConvertTo-Html -Head $head

$body = $body.Replace('>#br#',' bgcolor="red">').Replace('>#by#',' bgcolor="yellow">').Replace('>#bg#',' bgcolor="green">')

$sMail = @{

    To = 'my.name@domain'

    From = 'report@domain'

    Subject = 'Datastore Report'

    BodyAsHtml = $true

    Body = $body | Out-String

    SmtpServer = 'mail.domain'

}

Send-MailMessage @sMail


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

Vijay1996
Enthusiast
Enthusiast
Jump to solution

Hi LucD, Thank you for the kind support. Could you please explain the command. Thanks in advance, Vijay

Vijay1996
Enthusiast
Enthusiast
Jump to solution

Could you please suggest me powercli and powershell books for learning.

LucD
Leadership
Leadership
Jump to solution

Which command do you mean?

For learning, there are some posts in here that discuss learning resources.

New to PowerCLI. Book recommendations

6.  Re: vms deploy from template


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

0 Kudos
Vijay1996
Enthusiast
Enthusiast
Jump to solution

$body = Get-Datastore |     Select @{N='Datastore';E={$_.Name}},     @{N='CapacityGB';E={[math]::Round($_.CapacityGB,1)}},     @{N='FreeSpaceGB';E={[math]::Round($_.FreeSpaceGB,1)}},     @{N='UsedPercent';E={[math]::Round(($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*100,1)}},     @{N='FreePercent';E={         $p = [math]::Round((1 - ($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB)*100,1)         if($p -lt 10){"#br#$($p)"}         elseif($p -lt 30){"#by#$($p)"}         else{"#bg#$($p)"}     }} | Can you please explain above command and thanks for providing book details.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, with the Select-Object cmdlet I extract information from the Datastore objects, returned by the Get-Datastore cmdlet.

The properties on the Select are so-called "caluclated properties".

In such a property you have a N(ame) and E(xpression) part. The expression defines how the value for the property is obtained.

On the FreePercent property, I add markers that change depending on the value.

These markers are later replaced by actual HTML code.

The reason I do this in this way, is because this causes no problems with the ConvertTo-Html cmdlet later on.

That is also why the Replace of these markers is done after the ConvertTo-Html cmdlet.


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

0 Kudos
Vijay1996
Enthusiast
Enthusiast
Jump to solution

$_.FreeSpaceGB)/$_.CapacityGB*100,1)}},    @{N='FreePercent';E={        $p = [math]::Round((1 - ($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB)*100,1)        if($p -lt 10){"#br#$($p)"}        elseif($p -lt 30){"#by#$($p)"}        else{"#bg#$($p)"}  Could you please explain how the calculation actually works.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

  1. Capacity - Free, gives the Used space
  2. Divide this by Capacity, gives a ration between 0 and 1
  3. Subtract this ration from 1 to go back from Used to Free
  4. Multiply by 100 to get a percentage

I could also have done: $p = [math]::Round($_.FreeSpaceGB/$_.CapacityGB*100,1)


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

0 Kudos
Vijay1996
Enthusiast
Enthusiast
Jump to solution

LucD, Thank you so much!! Now, I understand. Have a great day!! Thanks, Vijay

0 Kudos
sureshasai
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

Am good with this script its very much useful me too.

Doubts:

1) I would like to add only top 10 list of data stores only on body of the email and others needs to be attached like an csv file or html file in that email including that top low disk space DS.  which is having low disk space. As per the script it marked as red color.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

May I suggest you open a new thread for this question.

Otherwise this thread risks becoming to cluttered.


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

0 Kudos