VMware Cloud Community
poncia23
Contributor
Contributor

Snapshots

Hello everyone,

I am looking for a script that will create a snapshot and will send me an email with any that failed and any that were successful.

I have a simple script but i need more than what i have to make this work. Take look below and if anyone know of any other place that i could get some help it will be appreciated.

Connect-VIServer -Server 192.168.1.9 -Protocol https


$vmlist = Get-Content C:\Computers.txt

$date = Get-Date


foreach($VM in $VMlist) {

    New-Snapshot -VM $vm -Name "Before Epic Upgrade" -description "Before Epic Upgrade Snapshot $date" -quiesce

}


Disconnect-VIServer -Confirm:$false 

Reply
0 Kudos
4 Replies
a_p_
Leadership
Leadership

Discussion moved from PowerShellers to VMware vSphere™ PowerCLI

Reply
0 Kudos
Tocano
Enthusiast
Enthusiast

Try something like this:

$connection = Connect-VIServer -Server 192.168.1.9 -Protocol https


$vmNameList = Import-Csv .\path\to\datafile.csv

$date = Get-Date


$vmObjs = Get-VM -Name $vmNameList


$output = @()

foreach($nextVM in $vmObjs) {


    $return = $null

    $return = $nextVM | New-Snapshot -Name "Before Epic Upgrade" -description "Before Epic Upgrade Snapshot $date" -quiesce

     if($return){ $result = "Success" }

     else { $result = "Failure" }

     $output += @($nextVM.Name,$result)

}


$smtpServer = [smtpServer]

$emailTo = [destEmailAddr]

$emailSubject = "Snapshot Results"

$emailBody = "$($output | ft -autosize)"


Send-MailMessage -To $emailTo -From 'scriptEmail@noreply.com' -Subject $emailSubject -Body $emailBody -SmtpServer $smtpServer


$connection | Disconnect-VIServer -Confirm:$false



Probably not the most efficient or elegant approach, but that should do most of what you need.

Reply
0 Kudos
poncia23
Contributor
Contributor

Thank you!, I made some changes to make it work in the environment, another quick question. Is there a way to get the datastores free space before taking the snapshots?

Reply
0 Kudos
Tocano
Enthusiast
Enthusiast

Are you talking about within the snapshot script or via a different script completely that you would run before you ran the Snapshot script?

Reply
0 Kudos