Hi,
this will be draggy, sorry for that.
my script below using $global:DefaultVIServers in foreach , but it did not listed all vcenters i want( unsure know why here it is not paste in fully colored as in powerCLI though).
It just stopped at 3rd vcenters when i expect this could call remainder vcenters
I tried instead with calling a txt file contain all vcenters , and use this with foreach instead, and it was turned disaster as output didnt showed like before( the output loop but stucked in the first vcenter, nothing less, as showed in 2nd snips below).
So, here are the script using $global:DefaultVIServers
#parameters
$currentdate = (Get-Date).tostring("dd-MM-yyyy")
#$VIServer = Get-Content -Path C:\Batch\Scripts\snapshots\vcenters.txt
$outputfilename = 'snapshot-report_' + $currentdate + '.html'
$outputfilepath = 'C:\scripts\Output\' + $outputfilename
Write-Host "Connecting to VCenters" -ForegroundColor Yellow
Connect-VIServer $VIServer -WarningAction SilentlyContinue
Write-Host " Complete" -ForegroundColor Green
#Format of HTML table via the ConvertTo-HTML cmdlet
$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;}
</style>
"@
$report = @()
$now = Get-Date
# Main section
Write-Host "Getting Snapshot" -ForegroundColor Yellow
foreach($vc in $global:DefaultVIServers){
$ReportVC = Get-VM -Server $vc | Get-Snapshot |
Select VM,Name,Description,
@{Label="Size";Expression={"{0:N2} GB" -f ($_.SizeGB)}},
@{N='User';E={
Get-VIEvent -Start $_.Created.AddMinutes(-1) -Finish $_.Created.AddMinutes(1) |
where{$_ -is [VMware.Vim.TaskEvent] -and $_.Info.DescriptionId -eq 'VirtualMachine.createSnapshot'} |
select -Last 1 -ExpandProperty UserName}},
@{Name="Days Old";Expression={ (New-TimeSpan -End (Get-Date) -Start $_.Created).Days }},
Created
If (-not $ReportVC)
{ $ReportVC = New-Object PSObject -Property @{
VM = "No snapshots found on any VM's in this VCenter"
Name = ""
Description = ""
Size = ""
Created = ""
}
}
$reportVCHtml = $ReportVC | ConvertTo-Html -Fragment
$xml = [xml]$reportVCHtml
$rows=$xml.table.selectNodes('//tr')
for($i=1;$i -lt $rows.count; $i++){
$value=$rows.Item($i).LastChild.'#text'
if($value.Length -gt 0 -and ($now - (Get-Date $value)).TotalHours -gt 72){
$attr=$xml.CreateAttribute('style')
$attr.Value='background-color: red;'
[void]$rows.Item($i).Attributes.Append($attr)
}
}
$reportVCHtml = "<p><h2>Snapshot Report -$($vc.Name)</h2></p><br>" + $xml.OuterXml
$report+= ConvertTo-Html -Head $Header -Body $reportVCHtml
}
Write-Host " Complete" -ForegroundColor Green
$sMail = @{
To= 'a@abc.com'
From= 'b@abc.com'
Subject= 'VM snapshots report ' + $currentdate
Body = ($report | Out-String)
BodyAsHtml = $true
smtpServer= 'a.b.local'
}
Write-Host "Sending Email" -ForegroundColor Yellow
#Try to send email
Send-MailMessage @sMail
OUTPUT from the script (i just snips part of the report but it only shows 3 vcenters instead of all other vcenters)
OUTPUT when i tried to call as foreach($vc in $VIServer){
many thanks for your time, appreciate your expertise!
The $global:defaultviservers variable contains all vCenter for which you did a Connect-VIServer, on the condition that your running PowerCLI in 'multiple' mode.
Check with Get-PowerCLIConfiguration, and eventually change with Set-PowerCLIConfiguration and the DefaultVIServerMode parameter..
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference