Hi All,
How to get the snapshot report of multiple VC's in html file. I am attaching the out put HTML file and the script I used.
I am getting the consolidate SP report but missing the VC name. In the out put 1st row is the SP from Vcserver 1 & 2nd row is for VC serve 2
I need to get the VC name in corresponding Colum.
Thanks in advance.
Siv
You can try this:
# Adding PowerCLI Snapin
if(!(get-pssnapin | where {$_.name -eq "vmware.vimautomation.core"})) {
try {
add-pssnapin VMware.VimAutomation.Core| out-null
} catch {
throw "Could not load PowerCLI snapin"
}
}$VIServer = "vcenter1","vcenter2"
$Report = @()
foreach($VCserver in $VIserver){
$currentVC = Connect-VIServer -Server $VCserver -Protocol https -User administrator -Password Pass@123
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
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;}
.odd { background-color:#ffffff; }
.even { background-color:#dddddd; }
</style>
<title>Snapshot Report - $VcServer</title>
"@
$a = Get-VM -Server $currentVC | Get-Snapshot | Select VM,@{N="VCname";E={$currentVC.Name}},Name,Description,Created,SizeMB,SizeGB
If (-not $a){
$a = New-Object PSObject -Property @{
VCname = "No snapshots found on any VM's controlled by $($currentVC.Name)"
VM = ""
Name = ""
Description = ""
SizeGB = ""
Created = ""
}
}
Disconnect-VIServer $currentVC -Force -Confirm:$false
$Report += $a
}
$Report = $report | Select VCname,VM,Name,Description,SizeGB,Created | ConvertTo-Html -Head $Header -PreContent "<p><h2>Snapshot Report</h2></p><br>"
$Report | Out-File "C:\VM-operations\dailyreport\SnapShotReport.html"
If you change this line
$Report = Get-VM | Get-Snapshot | Select VM,Name,Description,Created,SizeMB,SizeGB
into this
$Report = Get-VM | Get-Snapshot | Select VM,@{N="vCenter";E={$VCserver}},Name,Description,Created,SizeMB,SizeGB
But watch out, if you are running in Multiple mode (see the Set-PowerCLICOnfiguration cmdlet), you will get all the VMs and snapshots from both vCenters.
You can avoid that by using the Server parameter on the Get-VM cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Your ForEach loop is not correct, you are overwriting the C:\SnapshotReport.html file on every iteration.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi sir,
I am looking for report of each loop so can you please correct my script.
thanks for you valuable time to guide us in this community.
So you want a report per vCenter, did I get that correctly ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Sir,
Yes your are correct. Instead of getting this report from individual Vcenter I need to get a consolidated snapshot report from all vCenters in one html file to avoid multiple emails. The report should be like vcname & snapshot information
once again thankful for your helping hands .
VCOPs is meant for giving you various reports across vCenter instances very nicely. I am not sure whether it covers @snapshots.
Howver, VCOPS is not only made for reports.
You can raise a feature request
Hi Sir,
I tried to get the out put in arry and print the report still it is not changed. Any help will really helpful
My Script
$VIServer = "vcenter1","Vcenter2"
foreach($VCserver in $VIserver)
{
Connect-VIServer -Server $VIServer -Protocol https -User administrator -Password Pass@123
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
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;}
.odd { background-color:#ffffff; }
.even { background-color:#dddddd; }
</style>
<title>
Snapshot Report - $VcServer
</title>
"@
$Report = @()
$a = Get-VM | Get-Snapshot | Select VM,@{N="vCname";E={$VCserver}},Name,Description,Created,SizeMB,SizeGB
If (-not $a)
{ $a = New-Object PSObject -Property @{
VM = "No snapshots found on any VM's controlled by $VIServer"
Name = ""
Description = ""
SizeGB = ""
Created = ""
}
$Report += $a
}
$Report = $report |
Select VCname ,VM,Name,Description,SizeGB,Created |
ConvertTo-Html -Head $Header -PreContent "<p><h2>Snapshot Report</h2></p><br>" |
Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd
$Report | Out-File "C:\VM-operations\dailyreport\SnapShotReport.html"
}
You can try this:
# Adding PowerCLI Snapin
if(!(get-pssnapin | where {$_.name -eq "vmware.vimautomation.core"})) {
try {
add-pssnapin VMware.VimAutomation.Core| out-null
} catch {
throw "Could not load PowerCLI snapin"
}
}$VIServer = "vcenter1","vcenter2"
$Report = @()
foreach($VCserver in $VIserver){
$currentVC = Connect-VIServer -Server $VCserver -Protocol https -User administrator -Password Pass@123
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
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;}
.odd { background-color:#ffffff; }
.even { background-color:#dddddd; }
</style>
<title>Snapshot Report - $VcServer</title>
"@
$a = Get-VM -Server $currentVC | Get-Snapshot | Select VM,@{N="VCname";E={$currentVC.Name}},Name,Description,Created,SizeMB,SizeGB
If (-not $a){
$a = New-Object PSObject -Property @{
VCname = "No snapshots found on any VM's controlled by $($currentVC.Name)"
VM = ""
Name = ""
Description = ""
SizeGB = ""
Created = ""
}
}
Disconnect-VIServer $currentVC -Force -Confirm:$false
$Report += $a
}
$Report = $report | Select VCname,VM,Name,Description,SizeGB,Created | ConvertTo-Html -Head $Header -PreContent "<p><h2>Snapshot Report</h2></p><br>"
$Report | Out-File "C:\VM-operations\dailyreport\SnapShotReport.html"
Plus, if you want a better approach, here it is:
# Adding PowerCLI Snapin
if(!(get-pssnapin | where {$_.name -eq "vmware.vimautomation.core"})) {
try {
add-pssnapin VMware.VimAutomation.Core| out-null
} catch {
throw "Could not load PowerCLI snapin"
}
}
# Set to multiple VC Mode
if(((Get-PowerCLIConfiguration -Scope Session).DefaultVIServerMode) -ne "Multiple") {
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope AllUsers -Confirm:$false | Out-Null
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -Confirm:$false | Out-Null
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope Session -Confirm:$false | Out-Null
}
#
function connectvCenters{
param($list)
$arrayConnectedVC =@()
foreach($itemList in $list){
try{
$sessionID = $global:defaultviservers | where {$_.Name -like "$itemList*"} |Select -First 1 -ExpandProperty SessionId
if($sessionId){
$vc = Connect-VIServer -server $itemList -Session $sessionId -wa 0 -ea stop
if($vc){
Write-Host "Successfully reconnected to '$vc'"
$arrayConnectedVC += $vc
}else{
Write-Host "Error connecting to '$vc'"
}
}else{
$vc = Connect-VIServer -server $itemList -wa 0 -ea stop
if($vc){
Write-Host "Successfully connected to '$vc'"
$arrayConnectedVC += $vc
}else{
Write-Host "Error connecting to '$vc'"
}
}
}catch{
$Errormessage = $_.Exception.MEssage
Write-Host "Error connecting to '$vc' `n `t$Errormessage"
}
}
return $arrayConnectedVC
}
#
function Get-Snaps(){
param($currentVC)
#Filter only Vms which contains Snapshots
Write-Host "`t Generating Report from Server: $($currentVC.name)"
#Filter Templates and non Snapshotteds Vms
$vmsWithSnaps = Get-view -viewType VirtualMachine -Property Name,Config.Template,Snapshot -Server $currentVC -Filter @{'Config.Template'='False'}|where{$_.snapshot -ne $null}
if($vmsWithSnaps){
try{
foreach($currVM in ($vmswithSnaps|Get-VIObjectByVIView)){
if($currVM){
Write-Host "`t`tSnapshots found Here: $($currVM.Name)"
$tempReport = $currVM | Get-Snapshot | Select VM,@{N="Vcenter";E={$currentVC.Name}},Name,@{N="PowerState";E={$currVM.PowerState}},Description,Created,SizeGB,UserName
#Get who created it
$SnapshotEvents = Get-VIEvent -Entity $currVM -type info -MaxSamples 1000 | Where {$_.FullFormattedMessage.contains("Create virtual machine snapshot")}
try{
$user = $SnapshotEvents[0].UserName
}catch [System.Exception] {
$user = $SnapshotEvents.UserName
}
$tempReport|%{
$_.SizeGB = "{0:N2}" -f $tempReport.SizeGB
$_.UserName = $user
}
$tempReport
}
}
}catch{
#do nothing, let it go
}
}else{
$tempReport = ""| Select VM,PowerState,Vcenter,Name,Description,Created,SizeGB,UserName
$tempReport.VM = "No Snapshots found on $($currentVC.Name)"
$tempReport
}
}
#
#Initialize Script
#
try{
#get Vcenters List from file
$VCStringlist = get-content ".\list.csv" -ErrorAction Stop
#or directly from script
# $VCStringlist = "scltz0009cld"
}catch{
$ErrorMessage = $_.Exception.Message
write-Host "Erro ao obter lista de vCenters do arquivo! `n $ErrorMessage"
}
#connect to Vcenters
$conns = connectvCenters -list $VCStringlist$Report = @()
#for each vcenter, generate snapshots list and add it to the main Report
$conns|%{
if($_){
$Report += Get-Snaps $_
Disconnect-VIServer $_ -Force -Confirm:$false|out-null
}
}
$htmlReport = @"
<style type='text/css'>
.heading {
color:#3366FF;
font-size:12.0pt;
font-weight:700;
font-family:Verdana, sans-serif;
text-align:left;
vertical-align:middle;
width:auto
}
.colnames {
color:white;
font-size:10.0pt;
font-weight:700;
font-family:Tahoma, sans-serif;
text-align:center;
vertical-align:middle;
border:.5pt solid windowtext;
background:#6495ED;
}
.text {
color:windowtext;
font-size:10.0pt;
font-family:Arial;
text-align:center;
vertical-align:middle;
border:.5pt solid windowtext;
background:#FFFFFF;
}
</style>
<title>Snapshot Report</title>
<table border=0 cellpadding=0 cellspacing=0 width=555
style='border-collapse:collapse;table-layout:fixed;width:600pt'>
<tr style='height:15.0pt'>
<th colspan=5 height=40 width=555 class="heading">
vSphere Snapshots Daily Report</th>
</tr>
<tr>
<th class="colnames">VCenter</th>
<th class="colnames">Virtual Machine</th>
<th class="colnames">Power State</th>
<th class="colnames">Description</th>
<th class="colnames">Size (GB)</th>
<th class="colnames">Date Created</th>
<th class="colnames">User Name</th>
</tr>
"@
foreach($snapshot in $Report){
$htmlReport = $htmlReport +
"<tr><td class='text'>" + $snapshot.Vcenter + "</td>" +
"<td class='text'>" + $snapshot.VM + "</td>" +
"<td class='text'>" + $snapshot.PowerState + "</td>" +
"<td class='text'>"+ $snapshot.Description + "</td>" +
"<td class='text'>" + $snapshot.SizeGB + "</td>" +
"<td class='text'>" + $snapshot.Created + "</td>"+
"<td class='text'>" + $snapshot.UserName + "</td></tr>"
}
$htmlReport = $htmlReport + "</table>"#Write Report to a html File
$dataTD = get-date
$dd = $dataTD.Day
$mm =$dataTD.Month
$yy = $dataTD.Year$hh = $dataTD.Hour
$mmm = $dataTD.Minute
$ss = $dataTD.Second
$TDformatted = "($dd-$mm-$yy) $hh"+"h"+"-$mmm"+"m-$ss"+"s"
$htmlReport | Out-File ".\SnapShotReport $TDformatted.html"
<# Generate the report and email it as a HTML body of an email
if($Report -ne ""){
$SmtpClient = New-Object system.net.mail.smtpClient
$SmtpClient.host = "my.smtp.host" #Change to a SMTP server in your environment$MailMessage = New-Object system.net.mail.mailmessage
$MailMessage.from = "System.Automation@example.com" #Change to email address you want emails to be coming from
$MailMessage.To.add("yomomma@example.com") #Change to email address you would like to receive emails.
$MailMessage.IsBodyHtml = 1
$MailMessage.Subject = "VMware Snapshots Report $TDformatted "
$MailMessage.Body = $htmlReport
$SmtpClient.Send($MailMessage)
}
#>
Now better than before. That's what i'm using on my environment.