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
YuvarajSekar01
Enthusiast
Enthusiast
Jump to solution

Hello Lucd,

Can you please make changes in the script to get the datastores report for the multiple vCenters.

Thanks in advance,

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this.

You need to connect to all the vCenters before you run the script.

$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 = @()

foreach($vc in $global:DefaultVIServers){

  $body += Get-Datastore -Server $vc |

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

    @{N='vCenter';E={$vc.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)"}

    }}

}

$body = $body |

    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

YuvarajSekar01
Enthusiast
Enthusiast
Jump to solution

Hi Lucd.

I am getting error on this script.

Error Details: Method invocation is failed. Does not contain method name "Replace"

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which PowerShell version are you using?

Check the content of $PSVersionTable


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

0 Kudos
YuvarajSekar01
Enthusiast
Enthusiast
Jump to solution

Hi,

I am using Powershell version 4.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I updated the code above, please try again.


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

0 Kudos
YuvarajSekar01
Enthusiast
Enthusiast
Jump to solution

Hi,

Thanks it works fine with Multiple vCenter.

I need one suggestion from you. Each and everytime its ask me login credentials for each vCenters. Is there is any command to do it automatically.

Thanks in Advance, 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Have a look at the New-VICredentialStoreItem.


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

sureshasai
Enthusiast
Enthusiast
Jump to solution

I think as per my searching it's not possible. I have tried with task scheduler also but its asking credential ....

0 Kudos
afhamsani
Enthusiast
Enthusiast
Jump to solution

hi lucD,

if i want to present the output from these existing lines of script, as each datastore is showed on each vcenters.

Datastore Report of VCenter A

DatastorevCenter
CapacityGBFreeSpaceGBUsedPercentGBFreePercent

is it correct to be this way?

#parameter

$vc = get-content -path C:\vcenters.txt

$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 = @()

foreach($vc in $global:DefaultVIServers){

  $body += Get-Datastore -Server $vc |

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

    @{N='vCenter';E={$vc.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)"}

      }

     }

$reportDS = "<p><h2>Datastore Report of -$($vc.Name)</h2></p><br>" + $xml.OuterXml

$body = $body |

    Sort-Object -Property UsedPercent -Descending |

    ConvertTo-Html -Head $head -Body $reportDS

$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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid that will not work.

You will only get output from 1 vCenter, since you are overwriting the $body variable inside the ForEach loop.

I would use the -Fragment option on the ConvertTo-Html cmdlet.

Something like in Re: Need help on my snapshot script

In there we collect the fragments, one for each vCenter, in the $report variable.

And its the $report variable that we send in the Body of the email


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

0 Kudos
afhamsani
Enthusiast
Enthusiast
Jump to solution

i see that is -fragment does eh?

thanks, got it.

i will check the link you attach and study more on this.

cheers!

0 Kudos
Polisetty
Contributor
Contributor
Jump to solution

Is this possible to code to get the data in table format with the complete background colour defined based on the thresholds.

0 Kudos