Skip to content

VirtuallyThatGuy

Anything VMware , PowerCLI, PowerShell, Automation and some Windows

Menu
  • Home
  • PowerCLI
  • VMware
  • Automation
  • Windows
  • About
Menu

Comprehensive Script of VM’s with Snapshots Running – Using PowerShell or Powercli – VirtuallyThatGuy

Posted on August 14, 2022December 6, 2022 by VirtuallyThatGuy

This script generates an email report of virtual machines that have snapshots running.

$OutputFile = "VM-Snapshot.csv"

function Log ($text) {
    [int]$duration = (New-TimeSpan $start (Get-Date)).TotalSeconds
    Write-Host "$duration secs | $text"
}

function Log-NoNewLine ($text) {
    [int]$duration = (New-TimeSpan $start (Get-Date)).TotalSeconds
    Write-Host "$duration secs | $text" -nonewline
}

$start = Get-Date

$table = New-Object system.Data.DataTable "Results"
$table.columns.add((New-Object system.Data.DataColumn Name,([string])))
$table.columns.add((New-Object system.Data.DataColumn Folder,([string])))
$table.columns.add((New-Object system.Data.DataColumn Snap,([string])))
$table.columns.add((New-Object system.Data.DataColumn Created,([datetime])))
$table.columns.add((New-Object system.Data.DataColumn Size_MB,([single])))
$table.columns.add((New-Object system.Data.DataColumn PowerState,([string])))

Log("Getting list of VMs to check...")
$VMs = Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Sort -Property Name
Log ("Got list of " + ($VMs.Count) + " VMs to check")

$VMno = 0
$VMtot = $VMs.Count

foreach ($vm in $VMs) {
    $VMno = $VMno + 1
    Log-NoNewLine "[$VMno/$VMtot] $vm - "
    
    $Snaps = Get-Snapshot -VM $vm
    if ($Snaps) {
        foreach ($snap in $snaps) {
            $row = $table.NewRow()
            $row.Name = $vm.Name 
            $row.Folder = Get-Folder -Id $vm.FolderId
            $row.Snap = $snap.Name
            $row.Created = $snap.Created
            $row.Size_MB = $snap.SizeMB
            $row.PowerState = $snap.PowerState
            Write-Host $row.Snap "| Created" $row.Created "| Size" $row.Size_MB "MB | " $row.PowerState
        }
    } else {
        Write-Host "No snapshot"
    }
}

if ($table.rows.Count) {
    Log "Completed, writing out table..."
     
    $table | Format-Table
    $table | Export-Csv -path $OutputFile
    
    #Send via email
    $smtp = New-Object Net.Mail.SmtpClient -arg "smtpserver"
    $msg = New-Object Net.Mail.MailMessage
    $attach = New-Object Net.Mail.Attachment($OutputFile)
    
    $msg.From = "from"
    $msg.To.Add = "to"
    $msg.Subject = "Snapshots found running!!"
    $msg.Body = "$table.rows.Count snapshots found to be running, see attachment for further info"
    
    $smtp.Send($msg)
    $attach.Dispose()
}

Log "Completed!"

save above and run as below

 .\getsnapshotsize.ps1

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • vROps: Management Pack Troubleshooting
  • Windows AD {Active Directory} (PowerShell) samples
  • Migrate VMs Between vCentres Using Powershell or PowerCLI
  • Set VM Tools to Update Automatically on VM Reboot using powershell
  • Windows Administrator Must Have Powershell Commands

Recent Comments

  • VirtuallyThatGuy on ESXi 8.x, 7.x, 6.x Service sfcbd-watchdog Not Running / Fails to Start – VirtuallyThatGuy
  • VirtuallyThatGuy on VMware ESXi CLI Cheat Sheet
  • Johan on ESXi 8.x, 7.x, 6.x Service sfcbd-watchdog Not Running / Fails to Start – VirtuallyThatGuy
  • Allen on VMware ESXi CLI Cheat Sheet
  • edward on Cluster Capacity Report for VMs, Cluster, Hosts, vCenter – Using PowerCLI – VirtuallyThatGuy

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017

Categories

  • Automation
  • PowerCLI
  • VMware
  • Windows
© 2023 VirtuallyThatGuy | Powered by Superbs Personal Blog theme