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
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
Thanks for your help!!. Now i got it.
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:
Thank you!!
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
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,
Are you the same guy connecting with 2 accounts?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No. I am just trying to make use of this. Thats it.
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.
And also i have attached the output report for your reference.
Thanks in Advance,
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
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
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
Hi LucD, Thank you for the kind support. Could you please explain the command. Thanks in advance, Vijay
Could you please suggest me powercli and powershell books for learning.
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
$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.
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
$_.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.
I could also have done: $p = [math]::Round($_.FreeSpaceGB/$_.CapacityGB*100,1)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD, Thank you so much!! Now, I understand. Have a great day!! Thanks, Vijay
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.
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