PowerCLI : Get existing VM snapshot list from vCenter/Host and Send an email

Dan's Logging·2021년 8월 13일
0

Powershell_VMware

목록 보기
1/2
post-thumbnail

The powershell script below is for getting VM snapshot list from vCenters or ESXi Hosts and also send email.

Functional Script

vCenter/Host Login

disconnect-viserver * -confirm:$false!
connect-viserver -server 192.168.1.1 -user "userinput" -password "password input"

Set output Location

$output = "C:\OUTPUTLOCATION\Snapshot_Usage_$((Get-Date).ToString('yyyy-MM-dd_hh-mm-ss'))"+'.csv'

Getting Current Date

$newdate = get-date

Main Script

Get-VM | Get-Snapshot |
    Select VM,
    Name,
    Description,
    @{Name="User"; Expression = { (Get-VIEvent -Entity $_.VM -Start $_.Created.AddSeconds(-10) | Where {$_.Info.DescriptionId -eq "VirtualMachine.createSnapshot"} | Sort-Object CreatedTime | Select-Object -First 1).UserName}},
    @{Name="SizeGB";Expression={ [math]::Round($_.SizeGB,2) }},
    Created,
    @{Name="Days Old";Expression={(New-TimeSpan -End (Get-Date) -Start $_.Created).Days }},
    @{Name="Datastore";Expression={Get-Datastore -Id($_.vm).datastoreidlist}},
    @{Name="CapacityGB"; e={Get-Datastore -Id($_.vm).datastoreidlist | %{$_.CapacityGB}}}, 
    @{Name="FreeSpaceGB"; e={Get-Datastore -Id($_.vm).datastoreidlist | %{$_.FreeSpaceGB}}} | Export-Csv $output!

Set additional Values

Convert output CSV to html

$csvHTML = Import-csv $outfile | ConvertTo-Html

Send the output via Email

$smtpServer = "YOUR SMTP SERVER"
$MailFrom = "MAILFROM@abc.com" 
$mailto  = "MAILTO@abc.com,MAILTO2@abc.com"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $MailFrom 
$msg.IsBodyHTML = $true
$msg.To.Add($Mailto)  
$msg.Subject = "VM Snapshot Report-$newdate" 
$msg.Attachments.add($outfile)
$MailTextT = $csvHTML
$msg.Body = $MailTextT 
$smtp.credentials = $cred
$smtp.Send($msg)

Script

disconnect-viserver * -confirm:$false!
connect-viserver -server 192.168.1.1 -user "userinput" -password "password input"
$output = "C:\OUTPUTLOCATION\Snapshot_Usage_$((Get-Date).ToString('yyyy-MM-dd_hh-mm-ss'))"+'.csv'
$newdate = get-date
Get-VM | Get-Snapshot |
    Select VM,
    Name,
    Description,
    @{Name="User"; Expression = { (Get-VIEvent -Entity $_.VM -Start $_.Created.AddSeconds(-10) | Where {$_.Info.DescriptionId -eq "VirtualMachine.createSnapshot"} | Sort-Object CreatedTime | Select-Object -First 1).UserName}},
    @{Name="SizeGB";Expression={ [math]::Round($_.SizeGB,2) }},
    Created,
    @{Name="Days Old";Expression={(New-TimeSpan -End (Get-Date) -Start $_.Created).Days }},
    @{Name="Datastore";Expression={Get-Datastore -Id($_.vm).datastoreidlist}},
    @{Name="CapacityGB"; e={Get-Datastore -Id($_.vm).datastoreidlist | %{$_.CapacityGB}}}, 
    @{Name="FreeSpaceGB"; e={Get-Datastore -Id($_.vm).datastoreidlist | %{$_.FreeSpaceGB}}} | Export-Csv $output!
$csvHTML = Import-csv $outfile | ConvertTo-Html
$smtpServer = "YOUR SMTP SERVER"
$MailFrom = "MAILFROM@abc.com" 
$mailto  = "MAILTO@abc.com,MAILTO2@abc.com"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $MailFrom 
$msg.IsBodyHTML = $true 
$msg.To.Add($Mailto)  
$msg.Subject = "VM Snapshot Report-$newdate" 
$msg.Attachments.add($outfile)
$MailTextT = $csvHTML
$msg.Body = $MailTextT 
$smtp.credentials = $cred
$smtp.Send($msg)
profile
Working at IT Industry

0개의 댓글