Automation

 View Only
  • 1.  Add Snapshot Size to Report

    Posted Jul 30, 2022 05:01 AM

    I am looking for an easy way to add the size of the snapshot this report, any suggestions?

     


    #############################
    # Variables #
    #############################
    $date = Get-Date -format "yyyy-MMM-d"
    $datetime = Get-Date
    $filelocation = "\var\www\Snapshots\snapshot-$date.htm"

    #############################
    # Content #
    #############################

    $timeDiff = 2
    $report = Get-View -ViewType VirtualMachine -Filter @{'Runtime.PowerState'='poweredOn';'Snapshot'=".+"} |
    Select -First 50 Name,
    @{N='SnapShot';E={
    function Get-Snap{
    param([PSObject]$snap)
    $snap
    if($snap.ChildSnapshotList){
    $snap.ChildSnapshotList | %{
    Get-Snap -Snap $_
    }
    }
    }
    $script:snaps = $_.Snapshot.RootSnapshotList |
    where{$_.CreateTime -le (Get-Date).AddDays(-5) | % {
    Get-Snap -Snap $_ }
    }
    ($script:snaps | sort-Object -property Name).Name -join '|'}},
    @{N='SnapShot Created';E={($script:snaps | sort-Object -property Name).CreateTime -join '|'}},
    @{N="Days Old";E={
    $now = Get-Date
    ($script:snaps | %{(New-TimeSpan -End $now -Start $_.CreateTime).Days}) -join '|'}},
    @{N='Created By';E={
    $startEvents = $script:snaps.CreateTime
    $start = $startEvents | Sort-Object | Select -First 1
    (Get-VIEvent -Entity $_.Name -Start $start.AddSeconds(- $timeDiff) -MaxSamples ([int]::MaxValue) |
    Where-Object {$_ -is [VMware.Vim.TaskEvent] -and $_.Info.DescriptionId -eq 'VirtualMachine.createSnapshot'} |
    ForEach-Object -Process {
    $event = $_
    $startEvents | ForEach-Object -Process {
    if([math]::Abs((New-TimeSpan -Start $event.CreatedTime -End $_.ToLocalTime()).TotalSeconds) -lt $timeDiff){
    $event
    }
    }
    } | Select-Object -ExpandProperty UserName) -join '|'}}

    ## Build Count Var
    $count = $report | where{$_.Snapshot -ne ''}

    #############################
    # Add Text to the HTML file #
    #############################
    $report | where{$_.Snapshot -ne ''} | ConvertTo-Html –Title "VMware Snapshot Check " –Body "<H1>VMware Snapshot Check</H1>" -Head "<link rel='stylesheet' href='style.css' type='text/css' />" | Out-File $filelocation
    #$report | ConvertTo-Html –title "VMware Snapshot Check " -body "<H1>VMware Snapshot Check</H1>" -head "<link rel='stylesheet' href='style.css' type='text/css' />" | Out-File $filelocation
    ConvertTo-Html –title "VMware Snapshot Check " -body "<H4>Date and time</H4>",$datetime -head "<link rel='stylesheet' href='style.css' type='text/css' />" | Out-File -Append $filelocation
    ConvertTo-Html –title "VMware Snapshot Check " -body "<H4>VM Count</H4>",$count.Count -head "<link rel='stylesheet' href='style.css' type='text/css' />" | Out-File -Append $filelocation

     



  • 2.  RE: Add Snapshot Size to Report
    Best Answer

    Posted Jul 30, 2022 07:18 AM

    You could add a calculated property like this

    @{N='Snapshot size GB';E={
            ((Get-VM -Name $_.Name | Get-Snapshot -Name $script:snaps.Name).SizeGB | %{"{0:N1}" -f $_}) -join '|'
    }},