Automation

 View Only
Expand all | Collapse all

Using PowerCLI to take snapshots and then removing snapshots

jssidhu71

jssidhu71Aug 23, 2010 06:11 PM

  • 1.  Using PowerCLI to take snapshots and then removing snapshots

    Posted Aug 23, 2010 02:00 PM

    Hi - Wonder if someone has a script to do the following:

    Scenerio:

    I have 400 Virtual Desktops within a cluster, i need to create a snapshot at the beginning of the week for 100 VMs, after 2 weeks, the snapshots for these 100 VMS will be removed reverting back to the original image.

    Can this be done..? if so, can anyone assist please.

    Thanks



  • 2.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Aug 23, 2010 02:04 PM

    How would these 100 VMs be selected out of the 400 you have ?

    And is this a process that should be done every week, let's say on Monday ?

    And did I understand correcly that once a snapshot is 2 weeks old, it can be removed ?

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 3.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Aug 23, 2010 02:08 PM

    How would these 100 VMs be selected out of the 400 you have ?

    --> A list would be supplied listing VM names

    And is this a process that should be done every week, let's say on Monday ?

    --> Yes, every other week

    And did I understand correcly that once a snapshot is 2 weeks old, it can be removed ?

    --> Yes, snapshot to be removed completely



  • 4.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Aug 23, 2010 03:28 PM

    Throttling Snapshots

    This can give you an idea of how to throttle the removal and how to create the snapshots


    Maish - VCP - vExpert 2010

    VMware Communities User Moderator

    Virtualization Architect & Systems Administrator

    Twitter - @maishsk



  • 5.  RE: Using PowerCLI to take snapshots and then removing snapshots
    Best Answer

    Posted Aug 23, 2010 03:33 PM

    This should do the trick

    Import-Csv "C:\VMnames.csv" | %{
    	Get-VM $_.Name | New-Snapshot -Name "Weekly snapshots" -Quiesce:$true -Confirm:$false
    }
    $twoweeksago = (Get-Date).AddDays(-14)
    Get-VM | Get-Snapshot | %{
    	if($_.Created.CompareTo($twoweeksago) -eq -1){
    		Remove-Snapshot $_ -Confirm:$false
    	}
    }
    

    The CSV file should look like this

    Name
    vm1
    vm2
    ...
    

    I assumed that the snapshot removal had to run over all 400 VMs.

    If not the 2nd lookup has to be moved inside the Import-Csv loop.

    You can schedule the script with Windows Scheduler.

    Have a look at Alan's Running a PowerCLI Scheduled task post for the details.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 6.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Aug 23, 2010 04:46 PM

    Hey LucD.. many thanks for this..

    how would the script look if i wanted to list a number of VMs to be snapshotted, then remove the snapshot back to the original image without the duration .. As i may have to snapshot a number of vms then remove the snapshot for the said vms..

    Thanks Again !!



  • 7.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Aug 23, 2010 05:51 PM

    Not sure I understood that last question.

    Do you want to take a snapshot and then immediatly remove it ?

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 8.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Aug 23, 2010 05:59 PM

    Hey LucD..

    I want to list a 100 VMS to create a snapshot leaving for a few days, i now want to list the same 100 VMS to remove the snapshot regardless of how long the snapshot has been taken.. basically snapshot a list of vms, then run a script listing vms to remove snapshot.

    Thanks



  • 9.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Aug 23, 2010 06:09 PM

    That's quite easy.

    To create the snapshots

    Import-Csv "C:\VMnames.csv" | %{
         Get-VM $_.Name | New-Snapshot -Name "MySnapshot" -Quiesce:$true -Confirm:$false
    }
    

    To remove the snapshots

    Import-Csv "C:\VMnames.csv" | %{
       Get-VM $_.Name | Get-Snapshot -Name "MySnapshot" | Remove-Snapshot -Confirm:$false
    }
    

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 10.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Aug 23, 2010 06:11 PM

    Many Thanks !!



  • 11.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Jul 17, 2012 05:22 PM

    Sorry to resurrect an old post. Thanks  LucD - with your script, I am able to take snapshots from CSV list perfectly, however, I am unable to commit/delete them when I run this code and get error:

    Remove-Snapshot : Cannot bind parameter 'Snapshot'. Cannot convert the "@{Name=
    SERVER1}" value of type "System.Management.Automation.PSCustomObject" to t
    ype "VMware.VimAutomation.ViCore.Types.V1.VM.Snapshot".
    At C:\Support\RemoveSnaps.ps1:2 char:96
    +    Get-VM $_.Name | Get-Snapshot -Name "July Week 1 Patching CN000000099992"
    | Remove-Snapshot <<<<  $_ -Confirm:$false
        + CategoryInfo          : InvalidArgument: (:) [Remove-Snapshot], Paramete
       rBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat
       ion.ViCore.Cmdlets.Commands.RemoveSnapshot

    Not sure what could be causing this? Can you decipher? I am running VCenter 4.1.0



  • 12.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Jul 17, 2012 05:38 PM

    Actually got it working by removing  $_ in the Remove-Snapshot cmdlet:

    Import-Csv "C:\Support\Snapshotlist2.csv" | %{
       Get-VM $_.Name | Get-Snapshot -Name "July Week 1 Patching" | Remove-Snapshot -Confirm:$false
    }



  • 13.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Jul 17, 2012 05:51 PM

    Yes, that was a typo. I corrected the code.



  • 14.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Nov 13, 2014 07:35 AM

    Hi;

    What if we put ip addresses of VMs instead of names of them into the csv file,then how would the script be changed?

    Thank you.



  • 15.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Nov 13, 2014 07:45 AM

    It depends what the Displayname of the VMs are. Do they correspond with the hostname configured in the guest OS ?

    If yes, the script would first need to read all the IP addresses from the CSV file.

    Then do a name resolution, which would give the FQDN of the guest.

    The first qualifier of the FQDN should be the same as the Displayname of the VM.



  • 16.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Nov 13, 2014 07:53 AM

    Unfortunately display names and FQDN are not match in our env.

    If it is better,we can put FQDNs in the csv file.

    Actually the story is this:

    Windows team gave me the list of vms' IPs and FQDNs,and want me to take snapshot them before patching.



  • 17.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Nov 13, 2014 09:02 AM

    Ip or FQDN is not the issue, we can easily convert between the two from within a PowerShell script.

    Are the VMware Tools installed in each VM ?

    If yes, we can use the FQDN to find the VM.

    Something like this

    $vmTab = @{}

    # Set up hash table: key = FQDN

    Get-VM | %{

        $vmTab.Add($_.Guest.HostName,$_)

    }

    # Loop through CSV, lookup VM on FQDN

    Import-Csv vm.csv -UseCulture | %{

            Get-Snapshot -VM $vmTab[$_.fqdn]

    }



  • 18.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Nov 13, 2014 12:45 PM

    We got some error like:

    Exception calling "Add" with "2" argument(s): "Key cannot be null.

    Parameter name: key"

    Our csv file goes like this:

    test1,test2....



  • 19.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Nov 13, 2014 01:00 PM

    Was that by any chance for a VM that didn't have the VMware Tools installed ?



  • 20.  RE: Using PowerCLI to take snapshots and then removing snapshots

    Posted Nov 13, 2014 01:03 PM