Mostrando postagens com marcador vCenter. Mostrar todas as postagens
Mostrando postagens com marcador vCenter. Mostrar todas as postagens

Creating VMware Snapshots with PowerCLI

This is - Getting started with PowerShell and VMware vSphere - Part 1



Creating a snapshot, is a trivial and needed task most of the time. It is a very good idea to have it prior some changes in a virtual host.

    To create it, the virtual machine can be on or off. Also the snapshot will be created using that state. This might be important because if you roll back a running virtual machine to a snapshot where the virtual machine was stopped, the virtual machine will be stopped.     You will use the New-Snapshot cmdlet.

    When we create the snapshot you will need to give it a name... Also a good idea to provide a description. If the virtual machine is running, we can also elect to save the memory state as well with the –Memory parameter. Moreover, if the it has virtual tools installed, we can opt for a quiescent snapshot with –Quiesce

    This has the effect of saving the virtual disk in a consistent state. That is, the snapshot occurs without the operating system in the middle of trying to write something to disk. Personally, I remember to have less problems restoring a snapshot with –Quiesce, although there is a slight trade-off....  time it takes to create the snapshot. In fact, because a snapshot can take a few minutes to create, you may want to create it in the background on the VMware host using –RunAsycn.

2 ways I have tested:

  • New-Snapshot -VM MACHINE-NAME -Name 'NAME OF SNAP' -Description 'This is a test powershell snapshot' -Quiesce -Memory
  • Get-Vm MACHINE-NAME | New-Snapshot -Name "NAME OF SNAP" -Description 'This is a test powershell snapshot' -Quiesce -Memory


    Now, the beauty of scripting.... Naturally, if we can do it for one VM, we can do it for many :-)

Get-Vm MACHI* | new-snapshot -name "NAME OF SNAP" -Description "Created $(Get-Date) powershell" -Quiesce -Memory –RunAsync

    This will trigger a snapshot of all virtual machines which have names starting with MACHI*

Listing Snapshots

    To enumerate snapshots, just use Get-Snapshot. The snapshots are associated with the virtual machine, so you’ll need to specify one or we can use wildcards:

Get-Snapshot -vm MACHINE-NAME


Now that we have names, we can also get relevant information about the snap:

 Get-Snapshot -vm MACHINE-NAME -name SNAP-NAME | select *


Restoring from a Snapshot

To restore, we will use the Set-VM cmdlet. 
1st thing to do, assing the snap you want to a variable:

$snap = Get-Snapshot -vm MACHINE-NAME -name SNAP-NAME

Now we can easily use Set-VM to revert:

set-vm -VM MACHINE-NAME -Snapshot $snap -whatif




*Use -whatif to discover guess what? "What if" you ran that command what actions will take course. 

Removing Snapshots 

    Periodically you may want to clean up old snapshots using Remove-Snapshot. The easiest way is to get the snapshot or snapshots and pipe them to Remove-Snapshot. Some snapshots may have child snapshots so use –RemoveChildren to clean as well.

Get-Snapshot -name SNAP-NAME -VM MACHINE-NAME | remove-snapshot


    Now, imagine, we have multiple virtual machines with snapshots on that use the same name. We can use wildcards of course... be aware that this action would take some time to complete.... so let's running it asynchronously :-)

Get-Snapshot -name SNAP-NAME -vm * | remove-snapshot -RemoveChildren -RunAsync





If you lost howto install click here



Next topic will be New-VM.


=======================================================================

"Do you already know my Linux training !?" Available in portuguese only.


"Você já conhece meu treimanento de linux!?"

Neste treinamento vamos abordar de forma simples, rápida e sem “enrolação”  como instalar o Linux CentOS 8 utilizando o VirtualBox e a nuvem da Amazon AWS do zero.



Getting started with PowerShell and VMware vSphere



VMware provides PowerCLI which is a set of modules for VMware vSphere. Now I bring to you old news.... you should know that PowerShell is a powerful scripting language.... Initially, PowerShell enabled to manage only Windows Workstation or Server, but since sometimes, a lot of vendors make their own modules to manage their solutions (such as Veeam, VMware and so on....). PowerShell is available on Linux as well.

Install or update PowerCLI

Very simple instalation... just install the module and go:

To install PowerCli from the PowerShell gallery you need Internet Access. Then run:

Install-Module -Name VMware.PowerCLI 


If you have already installed PowerCLI from the PowerShell gallery, you can run the following command:

Update-Module -Name VMware.PowerCLI


When PowerCLI is instalation is over, you can run: You can see that 922 cmdlet exists for VMware vSphere. That’s a lot!


Get-Command -module *VMware* | measure




Attention:
You need PowerShell v5 at least. If you are not using Windows 10 or Windows Server 2016, you can install the latest 
Windows Management Framework.


Connect to VMware vCenter


To manage VMware vSphere you have to connect to VMware vCenter. First, run a Get-Credential to store in variable credentials to connect to vCenter. Then run the following command:

Connect-VIServer -Server <vCenter FQDN> -Credential <Credential variable>


$admin_credential = Get-Credential
Connect-VIServer -Server your_server.you-domain.com -Credential $admin_credential



Also you can run for example:

Get-VM |ft Name, Guest, VMHost, PowerState



Of course you can combine versatilitie of powershell with 911  cmdlet exists for VMware vSphere to do a lot of things....

:-)