###########################vCenter Server Snapshots Report #################################
############################## Created By Shobhit Mathur ###################################
############################## MAR - Modify As Required ####################################
$VIServers= "xxx" ###MAR###
#$reportFile = Get-Date -UFormat "E:\Sho\Reports\$VIServers-Snapshot_Report-%Y-%b-%d @ %I-%M%p.html" ###MAR###
$today = get-Date
$Sho=@"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;Font-Size: 8pt;Font-Family: Tahoma, sans-serif;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
"@
If (-not (Get-PSSnapin VMware.VimAutomation.Core))
{ Try { Add-PSSnapin VMware.VimAutomation.Core -ErrorAction Stop }
Catch { Write-Host "Unable to load PowerCLI, is it installed?" -ForegroundColor Red; Break }
}
Add-PSsnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
foreach($viserver in $VIServers)
{
Connect-VIServer $VIServers -User ###MAR### -Password ###MAR###
}
$Report = Get-VM | Get-Snapshot | Select @{N="Age" ; E={((Get-Date) - $_.Created).days}},VM,@{Label="Size";Expression={"{0:N2}" -f ($_.SizeGB)}},Created,@{Label="Created by";Expression={''}},Name,Description,@{N="OS";E={$_.vm.Guest.OSFullName}}
foreach($snap in $Report)
{
$snap.'Created by'= Get-VIEvent -entity ($snap.vm) -type info -MaxSamples 1000 | Where { $_.FullFormattedMessage.contains("Create virtual machine snapshot")}| Select-Object -First 1 -ExpandProperty username
}
$Sho += $Report | Select Age,VM,OS,Size,"Created by",Created,Name,Description |Sort-Object -Property Age -Descending | ConvertTo-Html -Fragment
$Sho1 = $Report | Measure-Object -Property Size -Sum | select -ExpandProperty Sum
$From = "xxxx" ###MAR###
$To = "xxxx" ###MAR###
$Cc = "xxxx" ###MAR###
$BCC = "xxxx" ###MAR###
$Subject = "Snapshot Report of $VIServers"
$Body1 = "<p><h4>
Snapshot Report of $VIServers as on $today.<br>
</h4></p><br>" #| Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd
$Body3 = "<p>Total Size of all snapshots in $VIServers is $Sho1 GB </p><br><br>" #| Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd
$Body2 = "<p> <br><br>This is script generated report, hence for any discrapancies please contact 'XXXX'. ###MAR### <br><br>
Best Regards<br>
Sho</p><br>" #| Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd
$HTMLBody = $Body1 + $Body3 + $Sho + $Body2
$SMTPServer = "xxxx" ###MAR###
$SMTPPort = "25" ###MAR###
Send-MailMessage -From $From -to $To -cc $Cc -Bcc $BCC -Subject $Subject -BodyAsHTML -body $HTMLBody -SmtpServer $SMTPServer
Disconnect-VIServer $VIServers -Force -Confirm:$false
############################## Created By Shobhit Mathur ###################################
############################## MAR - Modify As Required ####################################
$VIServers= "xxx" ###MAR###
#$reportFile = Get-Date -UFormat "E:\Sho\Reports\$VIServers-Snapshot_Report-%Y-%b-%d @ %I-%M%p.html" ###MAR###
$today = get-Date
$Sho=@"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;Font-Size: 8pt;Font-Family: Tahoma, sans-serif;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
"@
If (-not (Get-PSSnapin VMware.VimAutomation.Core))
{ Try { Add-PSSnapin VMware.VimAutomation.Core -ErrorAction Stop }
Catch { Write-Host "Unable to load PowerCLI, is it installed?" -ForegroundColor Red; Break }
}
Add-PSsnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
foreach($viserver in $VIServers)
{
Connect-VIServer $VIServers -User ###MAR### -Password ###MAR###
}
$Report = Get-VM | Get-Snapshot | Select @{N="Age" ; E={((Get-Date) - $_.Created).days}},VM,@{Label="Size";Expression={"{0:N2}" -f ($_.SizeGB)}},Created,@{Label="Created by";Expression={''}},Name,Description,@{N="OS";E={$_.vm.Guest.OSFullName}}
foreach($snap in $Report)
{
$snap.'Created by'= Get-VIEvent -entity ($snap.vm) -type info -MaxSamples 1000 | Where { $_.FullFormattedMessage.contains("Create virtual machine snapshot")}| Select-Object -First 1 -ExpandProperty username
}
$Sho += $Report | Select Age,VM,OS,Size,"Created by",Created,Name,Description |Sort-Object -Property Age -Descending | ConvertTo-Html -Fragment
$Sho1 = $Report | Measure-Object -Property Size -Sum | select -ExpandProperty Sum
$From = "xxxx" ###MAR###
$To = "xxxx" ###MAR###
$Cc = "xxxx" ###MAR###
$BCC = "xxxx" ###MAR###
$Subject = "Snapshot Report of $VIServers"
$Body1 = "<p><h4>
Snapshot Report of $VIServers as on $today.<br>
</h4></p><br>" #| Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd
$Body3 = "<p>Total Size of all snapshots in $VIServers is $Sho1 GB </p><br><br>" #| Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd
$Body2 = "<p> <br><br>This is script generated report, hence for any discrapancies please contact 'XXXX'. ###MAR### <br><br>
Best Regards<br>
Sho</p><br>" #| Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd
$HTMLBody = $Body1 + $Body3 + $Sho + $Body2
$SMTPServer = "xxxx" ###MAR###
$SMTPPort = "25" ###MAR###
Send-MailMessage -From $From -to $To -cc $Cc -Bcc $BCC -Subject $Subject -BodyAsHTML -body $HTMLBody -SmtpServer $SMTPServer
Disconnect-VIServer $VIServers -Force -Confirm:$false
No comments:
Post a Comment