,

vCenter | Snapshot Operations

Hi Folks,

Today someone asked about the snapshot. Like if I need to take snapshot from PowerShell then how to? if I quiesce the snapshot in PowerShell then how-to etc. etc. so, I thought to summarize as many as I can snapshot operations in a single page and share with all. Here you go..

Connect-VIserver VC-IP

#To take a snapshot for a single VM without quiescing and memory
$VM = read-host "Enter the VM Name "
New-Snapshot -VM $VM -Name vcnotes_snap -Description "This is test snap"
#with quiesced on
New-Snapshot -VM $VM -Name vcnotes_snap -Description "This is test snap" -Quiesce
#With memory state
New-Snapshot -VM $VM -Name vcnotes_snap -Description "This is test snap" -Memory
#with Memory and quiesce both
New-Snapshot -VM $VM -Name vcnotes_snap -Description "This is test snap" -Memory -Quiesce

#take snapshot for mulitple VMs and with quiesced off and no memory state
foreach ($AVM in (Get-content -path C:\Temp\vmlist.txt)){New-Snapshot -VM $AVM -Name vsnap -Description "This is test"}
#with quiesced on
foreach ($AVM in (Get-content -path C:\Temp\vmlist.txt)){New-Snapshot -VM $AVM -Name vsnap -Description "This is test" -Quiesce}
#with memory state
foreach ($AVM in (Get-content -path C:\Temp\vmlist.txt)){New-Snapshot -VM $AVM -Name vsnap -Description "This is test" -memory}
#with memory and quiesce both
foreach ($AVM in (Get-content -path C:\Temp\vmlist.txt)){New-Snapshot -VM $AVM -Name vsnap -Description "This is test" -Quiesce -Memory}

#Snapshot Consolidation
#For all Vms which needs consolidation
Get-VM | Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded} | foreach {$_.ExtensionData.ConsolidateVMDisks_Task()}
#for any single VM
$vmname = Read-Host "Enter VM Name"
$vm = Get-VM -Name $vmname
$vm.ExtensionData.consolidateVMDisks_Task()

#snapshot deletion
#to delete all snapshot older than specific days for single VM. In order to delete all snapshot taken today just replace 10 with 0 in below command
$vmname = Read-Host "Enter VM Name"
Get-VM -Name $vmname | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-10)} | Remove-Snapshot

#delete snapshot on multiple VMs older than specific days
$VM = Get-Content -Path C:\temp\vmlist.txt
Get-VM -Name $VM | Get-Snapshot | Where {$_.created -lt (Get-Date).AddDays(-10)} | Remove-Snapshot

#delete all snapshot on a VM
$VM = Read-Host "Enter VM Name"
Get-VM -Name $VM  | Get-Snapshot | Remove-Snapshot
#delete specific snapshot on a VM
$VM = Read-Host "Enter VM Name"
Get-VM -name $VM | Get-Snapshot | Select VM,Name,Created,SizeGB | FT
Write-Host "Tell me the snapshot name from above list"
$snapname = Read-Host "enter name here"
Get-VM -Name $VM | Get-Snapshot -name $snapname | Remove-Snapshot



#Revert to the last snapshot
$VM = read-host "Enter VM Name" 
Get-snapshot -VM $VM | select name 
Write-host "Tell me the name of Snapshot from the above list which you want to revert to"
Get-Snapshot -VM $VM -Name Test1 | Set-VM $VM


Let me know if anyone wants me to add anything to the list.



Thank you,
vCloudNotes

3 comments:

  1. Hi Gautam Sir, In my environment, we are having 12 different different plant with different network and all are not connected with each other and while creating a VM from template we need to patch the template then create a VM so I want to know if there is any automation or script available to patch the templates on monthly basic automatically or on single single click it will MS patches will apply on templates.
    Regards,
    Ananta Nayak

    ReplyDelete
    Replies
    1. Below is my idea to automate it. What I have given below is an idea which answer your question. You might face some challenges to understand it if you don't
      know the powershell. In case, you face any issue or any further question then feel free to let me know.

      Considering lack of time, this is the best answer I have for your question. Probably, you will face some errors, if so then don't hesitate to share.

      Well, I think your meaning by plant is vcenter server. Always use generic terms rather than a organization specific.
      it help to understand faster ;)

      So, question is, how to automatically patch a template?

      Direct patching of a template is not possible

      1) you need to first convert it into a VM.
      2) Patch the VM
      3) Convert it back into template

      .............start her....................................
      #Below command will convert the template into VM
      Set-Template -Template Win2012-R2 -ToVM
      #Below command will check and install the update inside GuestOS automatically and will reboot it as well
      $VM = read-host "Enter the name of VM"
      $User = read-host "Enter the username to login"
      $pass = read-host "Enter the password to login"
      $Chpass = @"
      Get-WindowsUpdate -install -acceptall -autoreboot
      "@
      Invoke-VMScript -VM $VM -ScriptText $Chpass -GuestUser $user -GuestPassword $pass -ScriptType Powershell
      #this behaviour need to explore that how switching from above to below command behave.You need to check this by your own as I don't have this setup to check this.
      #I did mention sleep to overcome with any frequent changeover. You might need to increase the sleep time if patching take more than the mentioned seconds.
      sleep(15)
      #Below command will convert the VM back into template
      Set-VM Win2012-R2 -ToTemplate -Confirm:$false
      ...............End here.....................................

      What's next-

      Copy above script, save into .ps1 file and put it into task scheduler and run as per your need.


      Best Regards,
      Team vCloudNotes

      Delete
    2. I hope that how to automatically create VMs from template after patching is not a question here because it is lot easier and you will find many articles on the web.

      I answered what I found that can be challenging to achieve. Rest,if you want to know more then don't hesitate to ask.

      Delete