Automation

 View Only
  • 1.  Question on snapshot creator

    Posted Sep 07, 2018 02:17 AM

    Hi folks,

    Attached snips of my snapshot report (thanks to LucD​ for his tireless answers) and there is a question about the creator, as I wonder why some VMs listed the creator but some weren't.

    At first i reckon maybe the VM snapshots was created a while back, but then there are VMs with creator with snapshot has existed for 129 days.

    So this does not sits correctly here.

    Does anyone know about this?

    Appreciated your advise.



  • 2.  RE: Question on snapshot creator

    Posted Sep 07, 2018 04:37 AM

    Although the script you used is not included, I assume that it uses the events to determine which user account created the snapshot.

    If the corresponding event for the creation of the snapshot is not found, there will be no user in the report.

    There can be a few reasons why that event is not found:

    • the event that corresponds with the snapshot is not present/kept
    • the time between the datetime mentioned in the snapshot, and the creation datatime of the event is too big (the scripts I used to create this kind of report, mostly use 1 minute between the 2 timestamps). Perhaps making the allowed interval between the 2 timestamps will help (but I would have to see the script you are using).


  • 3.  RE: Question on snapshot creator

    Posted Sep 07, 2018 06:11 AM

    Hi LucD,

    here are the scripts which happened you helped me with too :smileyhappy:

    #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= 'relay.mail.com'

    }

    Write-Host "Sending Email" -ForegroundColor Yellow

    #Try to send email

    Send-MailMessage @sMail



  • 4.  RE: Question on snapshot creator

    Posted Sep 07, 2018 06:35 AM

    You can try changing the length of the interval by changing the 1 and -1 values in this line.

    For example

    Get-VIEvent -Start $_.Created.AddMinutes(-2) -Finish $_.Created.AddMinutes(2)