Automation

 View Only
  • 1.  PowerCLI Scripting - SnapShot Create Date is older than 3 days

    Posted Oct 18, 2016 09:28 PM

    Hi All!

    I was tasked to create a script which queries all the snapshots created VMs in an eSXI host.   The main requirement is to generate a list of snapshots and VMs with create date of older than 3 days.   If there is a snapshot older than 3 days, create a ticket (internal ticketing system) - what I did was to send an email instead that will trigger the ticket creation.

    I'm new in scripting and doing my best to have the most simple script to get this information.   From my research over the internet (here‌ and some tweaking on SMTP config) I was able to use a script to get the list of snapshots and their creation date, and send the report via email.   I now need to trim down the report to those snapshots which are older than 3 days and send an email to me.

    Can anyone clarify what I need to only include the snapshots with a create date greater than 3 days?

    Below is the script I'm using

    # PowerShell script to check for VM snapshots and send Email report

    add-pssnapin VMware.VimAutomation.Core

    Connect-VIServer -Server <ip-or-host> -User <username> -Password <password>

    # HTML formatting

    $a = "<style>"

    $a = $a + "BODY{background-color:white;}"

    $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"

    $a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: LightBlue}"

    $a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: white}"

    $a = $a + "</style>"

    # Main section of check

    Write-Host "Checking VMs for for snapshots"

    $date = get-date

    $datefile = get-date -uformat '%m-%d-%Y-%H%M%S'

    $filename = "c:\dir\Snapshots_" + $datefile + ".htm"

    # Get list of VMs with snapshots

    # Note:  It may take some time for the  Get-VM cmdlet to enumerate VMs in larger environments

    $ss = Get-vm | Get-Snapshot

    Write-Host "   Complete " -ForegroundColor Green

    Write-Host "Generating VM snapshot report"

    #$ss | Select-Object vm, name, SizeGB, Created, powerstate | ConvertTo-HTML -head $a -body "<H2>VM Snapshot Report</H2>"| Out-File $filename

    $ss | Select-Object vm, name, SizeGB, Created, powerstate | ConvertTo-HTML -head $a -body "<H2>VM Snapshot Report </H2>"| Out-File $filename

    Write-Host "   Complete " -ForegroundColor Green

    Write-Host "Your snapshot report has been saved to:" $filename

    $SMTPServer = <smtp_server>

    $SMTPPort = 587

    $Username = "email@email.com"

    #Define the receiver of the report

    $to = "recipient.email@email.com"

    $subject = "VM Snapshot Report"

    $body = "VM Snapshot Report"

    $attachment = new-object Net.Mail.Attachment($filename)

    $message = New-Object System.Net.Mail.MailMessage

    $message.subject = $subject

    $message.body = $body

    $message.to.add($to)

    $message.from = $username

    $message.attachments.add($attachment)

    $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);

    $smtp.EnableSSL = $true

    #$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);

    $smtp.send($message)

    write-host "Mail Sent"



  • 2.  RE: PowerCLI Scripting - SnapShot Create Date is older than 3 days

    Posted Oct 18, 2016 10:14 PM

    Hi,

    Change this

    $ss = Get-vm | Get-Snapshot

    TO

    $ss = Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-3)}



  • 3.  RE: PowerCLI Scripting - SnapShot Create Date is older than 3 days

    Posted Oct 19, 2016 02:38 PM

    Great thanks!

    Yeah, it works!



  • 4.  RE: PowerCLI Scripting - SnapShot Create Date is older than 3 days

    Posted Jun 28, 2022 08:45 AM

    Hi Umesh, Similarly can you help with the code for getting  the details of the snapshots which are above or equal to 3000 GB please. 



  • 5.  RE: PowerCLI Scripting - SnapShot Create Date is older than 3 days

    Posted Jun 28, 2022 08:50 AM