2020 | Scott W. Head  |  Sr. Systems Admin | Certified Microsoft Windows Server Administrator

PowerShell PowerCLI Snapshots

List existing snapshots where operating system name matches -Like "Microsoft Windows Server" in VMGuest property and send via email. 

#----------Assuming PowerCLI is loaded and connected to vCenter -------------  
    $LogFile = "C:\Temp\Snapshot-" + "_$(get-date -format "yyyyMMdd_hhmmsstt").txt"

    $VMList=get-vm | where-Object {$_.PowerState -eq "PoweredOn"} | Get-VMGuest | where-Object {$_.OSFUllName -like "Microsoft Windows Server*"} | select -ExpandProperty VM 

    Foreach($VMItem in $VMList){
    get-snapshot $VMItem | select VM, created, name, description | Out-File $LogFile -Append
    }

    # ----- Configure Your Mail Variables -------
    $SMTPServer = "Mail_Server_Here"
    $SMTPFrom = "VMwareAdmin@YourDomain.com"
    $SMTPTo = "Email@YourDomain.com"
    $SMTPSubject = "Server Only Snapshots - " + (Get-Date -DisplayHint Date)
    $SMTPBody = "PowerCLI Review Snapshots in VMware"


    #Send Mail and Attach Custom Named Log File
    Send-MailMessage -SmtpServer $SMTPServer -From $SMTPFrom -To $SMTPTo -Subject $SMTPSubject -Body $SMTPBody -Attachments $LogFile


    #Clear Variables
    Clear-Variable SMTPSubject
    Clear-Variable SMTPBody
    Clear-Variable SMTPTo
    Clear-Variable SMTPFrom
    Clear-Variable VMList
    Clear-Variable VMItem
    Clear-Variable LogFile        
        

Create snapshot on multiple servers with 10 second interval, sources from file.
Run as a scheduled task with credentials that have acess to vCenter.

 

 

 Function Create-Snapshot() {
    # \\ -------------Parameters Defined--------------------- \\
           [CmdletBinding()]
     
    Param(
        [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
        [string]$ServerName,
     
        [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
        [string]$SnapshotName,
     
        [Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
        [string]$SnapshotDescription          
     
    )
     
    #\\----------------------- Start Processing----------------------- \\
    Process {
     
     
    #\\ Remove White Space \\                     
    $SnapshotDescription=$_.SnapshotDescription.Trim()
    $SnapshotName=$_.SnapshotName.Trim()
    $ServerName=$_.ServerName.Trim()
     
    #\\ Creates the snapshot \\
    Get-VM $ServerName | New-Snapshot -name $SnapshotName -Description "Script Created - $SnapshotDescription" -RunAsync -ErrorAction Stop       
        
    #\\Notation to Log \\
    "vCenter Server $VCenterServer; Server Name $ServerName; Name of Snapshot $SnapshotName; Snappshot Description $SnapshotDescription" | Out-file C:\Temp\Snashot_Log_$TimeStamp.txt -Append
     
    #\\Set Sleep Counter \\      
    Start-Sleep -seconds "10"      
                
    }                       
    }
     
     
    #\\----------------------------!!! Declare vCetner Server Name !!!--------------------------------\\
    $VCenterServer = "vCenter"
     
    #\\ Load Snap-in ---- PowerCLI Must be Installed ---------\\          
    & 'C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'
     
    #\\ Connect to the vCenter server ------ Domain Creds are Used for Auth------ \\
    Connect-VIServer $VCenterServer -ErrorAction Stop
     
    #\\ Log File Name \\
    $TimeStamp = Get-Date -Format o | foreach {$_ -replace ":", "."}
     
     
     
            #\\ Error Trapping \\
        Try{
            #\\ Import CSV and Call Function\\
            Import-CSV C:\Temp\Server_Snashot_List.csv |  Create-Snapshot -ErrorAction Stop
            }
        catch
            {
            #\\ Write Out Error Message \\
            "Processing Halted  $_.Exception.Message" | Tee-Object -FilePath  C:\Temp\Snashot_Log_$TimeStamp.txt -Append    
            }
             
    # \\ Proper Cleanup \\
    Disconnect-VIServer -Server $vCenterServer -confirm:$false