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)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

A very simple colour-coded datastore report via email can be done like below.

To run this this every 4 hours you could use the Windows Task Scheduler on the station where you run the script.

You could also place the script in a loop, and run it every 4 hours.

Not sure how you want to tackle the scheduling.

$body = Get-Datastore |

    Select @{N='Datastore';E={

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

        if($p -lt 70){"#fg#$($_.Name)#fe#"}

        elseif($p -lt 90){"#fy#$($_.Name)#fe#"}

        else{"#fr#$($_.Name)#fe#"}}},

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

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

    @{N='UsedPercent';E={$script:p}} | ConvertTo-Html

$body = $body.Replace('#fg#','<font color=green>').Replace('#fy#','<font color=yellow>').Replace('#fr#','<font color=red>').Replace('#fe#','</font>')

$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

View solution in original post

52 Replies
LucD
Leadership
Leadership
Jump to solution

A very simple colour-coded datastore report via email can be done like below.

To run this this every 4 hours you could use the Windows Task Scheduler on the station where you run the script.

You could also place the script in a loop, and run it every 4 hours.

Not sure how you want to tackle the scheduling.

$body = Get-Datastore |

    Select @{N='Datastore';E={

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

        if($p -lt 70){"#fg#$($_.Name)#fe#"}

        elseif($p -lt 90){"#fy#$($_.Name)#fe#"}

        else{"#fr#$($_.Name)#fe#"}}},

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

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

    @{N='UsedPercent';E={$script:p}} | ConvertTo-Html

$body = $body.Replace('#fg#','<font color=green>').Replace('#fy#','<font color=yellow>').Replace('#fr#','<font color=red>').Replace('#fe#','</font>')

$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,

I have tried this script, but dint get any output. Can you please edit the script, atleast to save in the c drives. Also, Is it possible to have it for the multiple vCenter?

Thanks in Advance,

LucD
Leadership
Leadership
Jump to solution

This script sends an email.

Did you configure the email settings for your environment?


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

0 Kudos
YuvarajSekar01
Enthusiast
Enthusiast
Jump to solution

Yes, I have configured the mail settings. But in my inbox, i dont see any mails. Also, I am not getting any error in the Powershell screen. Looks strange to me Smiley Sad.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you check the content of the $body variable?


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

0 Kudos
YuvarajSekar01
Enthusiast
Enthusiast
Jump to solution

Hi,

Thanks, Its work fine now. I missed to configure the SMTP details.

We planning to create the vCenter Capacity Report in html format. I am looking for something similar like, color combination  above 80% red and 60<70 in Amber and others in green for across the Vcenter, Like RAM, CPU and Datastores.

If you have any scripts on the Capacity Report related Please let me know.

I have already tried Marc scripts, which i am not satisfied that much.

Thanks in Advance,

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Note that the method I used to get the colouring was rather primitive, there are better ways of doing this with style sheets.

Which Marc scripts are those?


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

0 Kudos
Vijay1996
Enthusiast
Enthusiast
Jump to solution

Is it possible to run the powercli script in powershell or else could you please help me convert the above script in to powershell...

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not quite sure what you mean, the above is a PowerShell/PowerCLI script.

Do you get any errors?

Can you show a screenshot of what you are getting?


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

0 Kudos
Vijay1996
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

Shall I execute script directly to powercli or else is there anything I want to add extra code to connect vcenter server with credentials like that.

I have to save the script in report.ps1 extension and run it through powercli correct ?

Please guide  me..

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It depends which PowerCLI version you are using.

Can you do a Get-PowerCLIVersion at the PowerCLI prompt?


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

0 Kudos
YuvarajSekar01
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

Thanks it works fine to me.

I have been struggling to get the available resource from the Vcenter. Basically i am trying to do the Capacity Management in the multiple vCenter. For example, I am looking like how many VMs can be provisioned with the available resources. Do you have any scripts/Report which will benefit me?

Thanks in advance,

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are several capacity report scripts in this community.

Did you already do a search?


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

0 Kudos
YuvarajSekar01
Enthusiast
Enthusiast
Jump to solution

I have tried my best, but i couldn't able to find one consolidated scripts which should cover all the areas. Can you please help me/Suggest me any script related to the Capacity Report.

Thanks

0 Kudos
Vijay1996
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

Greetings!!

I'm getting below error i have attached screenshot for your reference. Could you please let me know where i have done mistakes.

Thanks in advance!!

Thanks With Regards,

Vijay

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid I'm not seeing the error.


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

0 Kudos
Vijay1996
Enthusiast
Enthusiast
Jump to solution

Sorry, Now I have attached error screenshot. Please have a look on it and do the needful. And i will be waiting for your response.

Thanks!!

datastore report error.PNG

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like your SmtpServer requires authentication.

You can add the Credential parameter on the Send-MailMessage cmdlet, and of course provide a credential that has the permission to send emails via the Smtp gateway.


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

0 Kudos
Vijay1996
Enthusiast
Enthusiast
Jump to solution

Can you please guide me how to i add the Credential parameter on the Send-MailMessage cmdlet.

0 Kudos